base.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include "base.h"
  6. int sp_create(sp_data **spp)
  7. {
  8. *spp = (sp_data *) malloc(sizeof(sp_data));
  9. sp_data *sp = *spp;
  10. sprintf(sp->filename, "test.wav");
  11. sp->nchan = 1;
  12. SPFLOAT *out = malloc(sizeof(SPFLOAT) * sp->nchan);
  13. *out = 0;
  14. sp->out = out;
  15. sp->sr = 44100;
  16. sp->len = 5 * sp->sr;
  17. sp->pos = 0;
  18. sp->rand = 0;
  19. return 0;
  20. }
  21. int sp_createn(sp_data **spp, int nchan)
  22. {
  23. *spp = (sp_data *) malloc(sizeof(sp_data));
  24. sp_data *sp = *spp;
  25. sprintf(sp->filename, "test.wav");
  26. sp->nchan = nchan;
  27. SPFLOAT *out = malloc(sizeof(SPFLOAT) * sp->nchan);
  28. *out = 0;
  29. sp->out = out;
  30. sp->sr = 44100;
  31. sp->len = 5 * sp->sr;
  32. sp->pos = 0;
  33. sp->rand = 0;
  34. return 0;
  35. }
  36. int sp_destroy(sp_data **spp)
  37. {
  38. sp_data *sp = *spp;
  39. free(sp->out);
  40. free(*spp);
  41. return 0;
  42. }
  43. #ifndef NO_LIBSNDFILE
  44. int sp_process(sp_data *sp, void *ud, void (*callback)(sp_data *, void *))
  45. {
  46. SNDFILE *sf[sp->nchan];
  47. char tmp[140];
  48. SF_INFO info;
  49. memset(&info, 0, sizeof(SF_INFO));
  50. SPFLOAT buf[sp->nchan][SP_BUFSIZE];
  51. info.samplerate = sp->sr;
  52. info.channels = 1;
  53. info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_24;
  54. int numsamps, i, chan;
  55. if(sp->nchan == 1) {
  56. sf[0] = sf_open(sp->filename, SFM_WRITE, &info);
  57. } else {
  58. for(chan = 0; chan < sp->nchan; chan++) {
  59. sprintf(tmp, "%02d_%s", chan, sp->filename);
  60. sf[chan] = sf_open(tmp, SFM_WRITE, &info);
  61. }
  62. }
  63. while(sp->len > 0){
  64. if(sp->len < SP_BUFSIZE) {
  65. numsamps = sp->len;
  66. }else{
  67. numsamps = SP_BUFSIZE;
  68. }
  69. for(i = 0; i < numsamps; i++){
  70. callback(sp, ud);
  71. for(chan = 0; chan < sp->nchan; chan++) {
  72. buf[chan][i] = sp->out[chan];
  73. }
  74. sp->pos++;
  75. }
  76. for(chan = 0; chan < sp->nchan; chan++) {
  77. #ifdef USE_DOUBLE
  78. sf_write_double(sf[chan], buf[chan], numsamps);
  79. #else
  80. sf_write_float(sf[chan], buf[chan], numsamps);
  81. #endif
  82. }
  83. sp->len -= numsamps;
  84. }
  85. for(i = 0; i < sp->nchan; i++) {
  86. sf_close(sf[i]);
  87. }
  88. return 0;
  89. }
  90. #endif
  91. int sp_process_raw(sp_data *sp, void *ud, void (*callback)(sp_data *, void *))
  92. {
  93. int chan;
  94. if(sp->len == 0) {
  95. while(1) {
  96. callback(sp, ud);
  97. for (chan = 0; chan < sp->nchan; chan++) {
  98. fwrite(&sp->out[chan], sizeof(SPFLOAT), 1, stdout);
  99. }
  100. sp->len--;
  101. }
  102. } else {
  103. while(sp->len > 0) {
  104. callback(sp, ud);
  105. for (chan = 0; chan < sp->nchan; chan++) {
  106. fwrite(&sp->out[chan], sizeof(SPFLOAT), 1, stdout);
  107. }
  108. sp->len--;
  109. sp->pos++;
  110. }
  111. }
  112. return SP_OK;
  113. }
  114. #ifdef USE_SPA
  115. int sp_process_spa(sp_data *sp, void *ud, void (*callback)(sp_data *, void *))
  116. {
  117. sp_audio spa;
  118. if(spa_open(sp, &spa, sp->filename, SPA_WRITE) == SP_NOT_OK) {
  119. fprintf(stderr, "Error: could not open file %s.\n", sp->filename);
  120. }
  121. while(sp->len > 0) {
  122. callback(sp, ud);
  123. spa_write_buf(sp, &spa, sp->out, sp->nchan);
  124. sp->len--;
  125. sp->pos++;
  126. }
  127. spa_close(&spa);
  128. return SP_OK;
  129. }
  130. #endif
  131. int sp_process_plot(sp_data *sp, void *ud, void (*callback)(sp_data *, void *))
  132. {
  133. int chan;
  134. fprintf(stdout, "sp_out = [ ... \n");
  135. while(sp->len > 0) {
  136. callback(sp, ud);
  137. for (chan = 0; chan < sp->nchan; chan++) {
  138. /* fwrite(&sp->out[chan], sizeof(SPFLOAT), 1, stdout); */
  139. fprintf(stdout, "%g ", sp->out[chan]);
  140. }
  141. fprintf(stdout, "; ...\n");
  142. sp->len--;
  143. sp->pos++;
  144. }
  145. fprintf(stdout, "];\n");
  146. fprintf(stdout, "plot(sp_out);\n");
  147. fprintf(stdout, "title('Plot generated by Soundpipe');\n");
  148. fprintf(stdout, "xlabel('Time (samples)');\n");
  149. fprintf(stdout, "ylabel('Amplitude');\n");
  150. return SP_OK;
  151. }
  152. int sp_auxdata_alloc(sp_auxdata *aux, size_t size)
  153. {
  154. aux->ptr = malloc(size);
  155. aux->size = size;
  156. memset(aux->ptr, 0, size);
  157. return SP_OK;
  158. }
  159. int sp_auxdata_free(sp_auxdata *aux)
  160. {
  161. free(aux->ptr);
  162. return SP_OK;
  163. }
  164. SPFLOAT sp_midi2cps(SPFLOAT nn)
  165. {
  166. return pow(2, (nn - 69.0) / 12.0) * 440.0;
  167. }
  168. int sp_set(sp_param *p, SPFLOAT val) {
  169. p->state = 1;
  170. p->val = val;
  171. return SP_OK;
  172. }
  173. int sp_out(sp_data *sp, uint32_t chan, SPFLOAT val)
  174. {
  175. if(chan > sp->nchan - 1) {
  176. fprintf(stderr, "sp_out: Invalid channel\n");
  177. return SP_NOT_OK;
  178. }
  179. sp->out[chan] = val;
  180. return SP_OK;
  181. }
  182. /*
  183. uint32_t sp_rand(sp_data *sp)
  184. {
  185. uint32_t val = (1103515245 * sp->rand + 12345) % SP_RANDMAX;
  186. sp->rand = val;
  187. return val;
  188. }
  189. */
  190. void sp_srand(sp_data *sp, uint32_t val)
  191. {
  192. sp->rand = val;
  193. }