You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
8.1KB

  1. #include "../Bidoo/src/dep/waves.hpp"
  2. #include "../Bidoo/src/dep/AudioFile/AudioFile.h"
  3. // #define DR_WAV_IMPLEMENTATION
  4. #include "../cf/src/dr_wav.h"
  5. #include <dsp/resampler.hpp>
  6. #ifndef DRWAV_ASSERT
  7. #include <assert.h>
  8. #define DRWAV_ASSERT(expression) assert(expression)
  9. #endif
  10. #ifndef DRWAV_MALLOC
  11. #define DRWAV_MALLOC(sz) malloc((sz))
  12. #endif
  13. #ifndef DRWAV_FREE
  14. #define DRWAV_FREE(p) free((p))
  15. #endif
  16. #define drwav_assert DRWAV_ASSERT
  17. #if defined(SIZE_MAX)
  18. #define DRWAV_SIZE_MAX SIZE_MAX
  19. #else
  20. #if defined(_WIN64) || defined(_LP64) || defined(__LP64__)
  21. #define DRWAV_SIZE_MAX ((drwav_uint64)0xFFFFFFFFFFFFFFFF)
  22. #else
  23. #define DRWAV_SIZE_MAX 0xFFFFFFFF
  24. #endif
  25. #endif
  26. extern "C" {
  27. float* drwav_open_file_and_read_f32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drwav_uint64* totalSampleCount);
  28. }
  29. static float* drwav__read_and_close_f32(drwav* pWav, unsigned int* channels, unsigned int* sampleRate, drwav_uint64* totalSampleCount)
  30. {
  31. drwav_uint64 sampleDataSize;
  32. float* pSampleData;
  33. drwav_uint64 samplesRead;
  34. drwav_assert(pWav != NULL);
  35. sampleDataSize = pWav->totalSampleCount * sizeof(float);
  36. if (sampleDataSize > DRWAV_SIZE_MAX) {
  37. drwav_uninit(pWav);
  38. return NULL; /* File's too big. */
  39. }
  40. pSampleData = (float*)DRWAV_MALLOC((size_t)sampleDataSize); /* <-- Safe cast due to the check above. */
  41. if (pSampleData == NULL) {
  42. drwav_uninit(pWav);
  43. return NULL; /* Failed to allocate memory. */
  44. }
  45. samplesRead = drwav_read_f32(pWav, (size_t)pWav->totalSampleCount, pSampleData);
  46. if (samplesRead != pWav->totalSampleCount) {
  47. DRWAV_FREE(pSampleData);
  48. drwav_uninit(pWav);
  49. return NULL; /* There was an error reading the samples. */
  50. }
  51. drwav_uninit(pWav);
  52. if (sampleRate) {
  53. *sampleRate = pWav->sampleRate;
  54. }
  55. if (channels) {
  56. *channels = pWav->channels;
  57. }
  58. if (totalSampleCount) {
  59. *totalSampleCount = pWav->totalSampleCount;
  60. }
  61. return pSampleData;
  62. }
  63. float* drwav_open_file_and_read_f32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drwav_uint64* totalSampleCount)
  64. {
  65. drwav wav;
  66. if (sampleRate) {
  67. *sampleRate = 0;
  68. }
  69. if (channels) {
  70. *channels = 0;
  71. }
  72. if (totalSampleCount) {
  73. *totalSampleCount = 0;
  74. }
  75. if (!drwav_init_file(&wav, filename)) {
  76. return NULL;
  77. }
  78. return drwav__read_and_close_f32(&wav, channels, sampleRate, totalSampleCount);
  79. }
  80. namespace waves {
  81. std::vector<rack::dsp::Frame<1>> getMonoWav(const std::string path, const float currentSampleRate, std::string &waveFileName, std::string &waveExtension, int &sampleChannels, int &sampleRate, int &sampleCount) {
  82. waveFileName = rack::system::getFilename(path);
  83. waveExtension = rack::system::getExtension(waveFileName);
  84. std::vector<rack::dsp::Frame<1>> result;
  85. if (waveExtension == ".wav") {
  86. unsigned int c;
  87. unsigned int sr;
  88. drwav_uint64 sc;
  89. float* pSampleData;
  90. pSampleData = drwav_open_file_and_read_f32(path.c_str(), &c, &sr, &sc);
  91. if (pSampleData != NULL) {
  92. sampleChannels = c;
  93. sampleRate = sr;
  94. for (long long unsigned int i=0; i < sc; i = i + c) {
  95. rack::dsp::Frame<1> frame;
  96. if (sampleChannels == 2) {
  97. frame.samples[0] = (pSampleData[i] + pSampleData[i+1])/2.0f;
  98. }
  99. else {
  100. frame.samples[0] = pSampleData[i];
  101. }
  102. result.push_back(frame);
  103. }
  104. sampleCount = sc/c;
  105. drwav_free(pSampleData);
  106. }
  107. }
  108. else if (waveExtension == ".aiff") {
  109. AudioFile<float> audioFile;
  110. if (audioFile.load (path.c_str())) {
  111. sampleChannels = audioFile.getNumChannels();
  112. sampleRate = audioFile.getSampleRate();
  113. sampleCount = audioFile.getNumSamplesPerChannel();
  114. for (int i=0; i < sampleCount; i++) {
  115. rack::dsp::Frame<1> frame;
  116. if (sampleChannels == 2) {
  117. frame.samples[0] = (audioFile.samples[0][i] + audioFile.samples[1][i])/2.0f;
  118. }
  119. else {
  120. frame.samples[0] = audioFile.samples[0][i];
  121. }
  122. result.push_back(frame);
  123. }
  124. }
  125. }
  126. if ((sampleRate != currentSampleRate) && (sampleCount>0)) {
  127. rack::dsp::SampleRateConverter<1> conv;
  128. conv.setRates(currentSampleRate, sampleRate);
  129. int outCount = sampleCount;
  130. std::vector<rack::dsp::Frame<1>> subResult;
  131. for (int i=0;i<sampleCount;i++) {
  132. rack::dsp::Frame<1> frame;
  133. frame.samples[0]=0.0f;
  134. subResult.push_back(frame);
  135. }
  136. conv.process(&result[0], &sampleCount, &subResult[0], &outCount);
  137. sampleCount = outCount;
  138. return subResult;
  139. }
  140. return result;
  141. }
  142. std::vector<rack::dsp::Frame<2>> getStereoWav(const std::string path, const float currentSampleRate, std::string &waveFileName, std::string &waveExtension, int &sampleChannels, int &sampleRate, int &sampleCount) {
  143. waveFileName = rack::system::getFilename(path);
  144. waveExtension = rack::system::getExtension(waveFileName);
  145. std::vector<rack::dsp::Frame<2>> result;
  146. if (waveExtension == ".wav") {
  147. unsigned int c;
  148. unsigned int sr;
  149. drwav_uint64 sc;
  150. float* pSampleData;
  151. pSampleData = drwav_open_file_and_read_f32(path.c_str(), &c, &sr, &sc);
  152. if (pSampleData != NULL) {
  153. sampleChannels = c;
  154. sampleRate = sr;
  155. for (long long unsigned int i=0; i < sc; i = i + c) {
  156. rack::dsp::Frame<2> frame;
  157. frame.samples[0] = pSampleData[i];
  158. if (sampleChannels == 2)
  159. frame.samples[1] = (float)pSampleData[i+1];
  160. else
  161. frame.samples[1] = (float)pSampleData[i];
  162. result.push_back(frame);
  163. }
  164. sampleCount = sc/c;
  165. drwav_free(pSampleData);
  166. }
  167. }
  168. else if (waveExtension == ".aiff") {
  169. AudioFile<float> audioFile;
  170. if (audioFile.load (path.c_str())) {
  171. sampleChannels = audioFile.getNumChannels();
  172. sampleRate = audioFile.getSampleRate();
  173. sampleCount = audioFile.getNumSamplesPerChannel();
  174. for (int i=0; i < sampleCount; i++) {
  175. rack::dsp::Frame<2> frame;
  176. frame.samples[0] = audioFile.samples[0][i];
  177. if (sampleChannels == 2)
  178. frame.samples[1] = audioFile.samples[1][i];
  179. else
  180. frame.samples[1] = audioFile.samples[0][i];
  181. result.push_back(frame);
  182. }
  183. }
  184. }
  185. if ((sampleRate != currentSampleRate) && (sampleCount>0)) {
  186. rack::dsp::SampleRateConverter<2> conv;
  187. conv.setRates(sampleRate, currentSampleRate);
  188. conv.setQuality(SPEEX_RESAMPLER_QUALITY_DESKTOP);
  189. int outCount = 16*sampleCount;
  190. std::vector<rack::dsp::Frame<2>> subResult;
  191. for (int i=0;i<outCount;i++) {
  192. rack::dsp::Frame<2> frame;
  193. frame.samples[0]=0.0f;
  194. frame.samples[1]=0.0f;
  195. subResult.push_back(frame);
  196. }
  197. conv.process(&result[0], &sampleCount, &subResult[0], &outCount);
  198. sampleCount = outCount;
  199. return subResult;
  200. }
  201. return result;
  202. }
  203. void saveWave(std::vector<rack::dsp::Frame<2>> &sample, int sampleRate, std::string path) {
  204. drwav_data_format format;
  205. format.container = drwav_container_riff;
  206. format.format = DR_WAVE_FORMAT_PCM;
  207. format.channels = 2;
  208. format.sampleRate = sampleRate;
  209. format.bitsPerSample = 32;
  210. int *pSamples = (int*)calloc(2*sample.size(),sizeof(int));
  211. memset(pSamples, 0, 2*sample.size()*sizeof(int));
  212. for (unsigned int i = 0; i < sample.size(); i++) {
  213. *(pSamples+2*i)= floor(sample[i].samples[0]*2147483647);
  214. *(pSamples+2*i+1)= floor(sample[i].samples[1]*2147483647);
  215. }
  216. drwav* pWav = drwav_open_file_write(path.c_str(), &format);
  217. drwav_write(pWav, 2*sample.size(), pSamples);
  218. drwav_close(pWav);
  219. free(pSamples);
  220. }
  221. }