2
0

soundAPI.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. 
  2. #include "soundAPI.h"
  3. #include "fileAPI.h"
  4. #include "../settings.h"
  5. #include "../base/format.h"
  6. #include "../base/noSimd.h"
  7. namespace dsr {
  8. // See the Source/soundManagers folder for implementations of sound_streamToSpeakers for different operating systems.
  9. SoundBuffer::SoundBuffer(uint32_t samplesPerChannel, uint32_t channelCount, uint32_t sampleRate) {
  10. this->impl_samplesPerChannel = samplesPerChannel;
  11. if (this->impl_samplesPerChannel < 1) this->impl_samplesPerChannel = 1;
  12. this->impl_channelCount = channelCount;
  13. if (this->impl_channelCount < 1) this->impl_channelCount = 1;
  14. this->impl_sampleRate = sampleRate;
  15. if (this->impl_sampleRate < 1) this->impl_sampleRate = 1;
  16. this->impl_samples = buffer_create(this->impl_samplesPerChannel * this->impl_channelCount * sizeof(float));
  17. }
  18. // scaleOffset 0.0 preserves the mantissa better using power of two multiplications.
  19. // scaleOffset 1.0 allows using the full -1.0 to +1.0 range to prevent hard clipping of high values.
  20. static double scaleOffset = 1.0f;
  21. static double toIntegerScaleU8 = 128.0 - scaleOffset;
  22. static double toIntegerScaleI16 = 32768.0 - scaleOffset;
  23. static double toIntegerScaleI24 = 8388608.0 - scaleOffset;
  24. static double toIntegerScaleI32 = 2147483648.0 - scaleOffset;
  25. static double fromIntegerScaleU8 = 1.0 / toIntegerScaleU8;
  26. static double fromIntegerScaleI16 = 1.0 / toIntegerScaleI16;
  27. static double fromIntegerScaleI24 = 1.0 / toIntegerScaleI24;
  28. static double fromIntegerScaleI32 = 1.0 / toIntegerScaleI32;
  29. // TODO: Create a folder for implementations of sound formats.
  30. static const int fmtOffset_audioFormat = 0;
  31. static const int fmtOffset_channelCount = 2;
  32. static const int fmtOffset_sampleRate = 4;
  33. static const int fmtOffset_bytesPerSecond = 8;
  34. static const int fmtOffset_blockAlign = 12;
  35. static const int fmtOffset_bitsPerSample = 14;
  36. static uint32_t getSampleBits(RiffWaveFormat format) {
  37. if (format == RiffWaveFormat::RawU8) {
  38. return 8;
  39. } else if (format == RiffWaveFormat::RawI16) {
  40. return 16;
  41. } else if (format == RiffWaveFormat::RawI24) {
  42. return 24;
  43. } else {
  44. return 32;
  45. }
  46. }
  47. static inline int64_t roundTo(double value, RoundingMethod roundingMethod) {
  48. if (roundingMethod == RoundingMethod::Nearest){
  49. return int64_t(value + (value > 0.0 ? 0.5 : -0.5));
  50. } else { // RoundingMethod::Truncate
  51. return int64_t(value);
  52. }
  53. }
  54. static inline uint8_t floatToNormalizedU8(float value, RoundingMethod roundingMethod) {
  55. int64_t closest = roundTo((double(value) * toIntegerScaleU8) + 128.0, roundingMethod);
  56. if (closest < 0) closest = 0;
  57. if (closest > 255) closest = 255;
  58. return (uint8_t)closest;
  59. }
  60. static inline int16_t floatToNormalizedI16(float value, RoundingMethod roundingMethod) {
  61. int64_t closest = roundTo(double(value) * toIntegerScaleI16, roundingMethod);
  62. if (closest < -32768) closest = -32768;
  63. if (closest > 32767) closest = 32767;
  64. return (int16_t)closest;
  65. }
  66. static inline int32_t floatToNormalizedI24(float value, RoundingMethod roundingMethod) {
  67. int64_t closest = roundTo(double(value) * toIntegerScaleI24, roundingMethod);
  68. if (closest < -8388608) closest = -8388608;
  69. if (closest > 8388607) closest = 8388607;
  70. return (int32_t)closest;
  71. }
  72. static inline int32_t floatToNormalizedI32(float value, RoundingMethod roundingMethod) {
  73. int64_t closest = roundTo(double(value) * toIntegerScaleI32, roundingMethod);
  74. if (closest < -2147483648) closest = -2147483648;
  75. if (closest > 2147483647) closest = 2147483647;
  76. return (int32_t)closest;
  77. }
  78. static inline float floatFromNormalizedU8(uint8_t value) {
  79. return float((double(value) - 128.0) * fromIntegerScaleU8);
  80. }
  81. static inline float floatFromNormalizedI16(int16_t value) {
  82. return float(double(value) * fromIntegerScaleI16);
  83. }
  84. static inline float floatFromNormalizedI24(int32_t value) {
  85. return float(double(value) * fromIntegerScaleI24);
  86. }
  87. static inline float floatFromNormalizedI32(int32_t value) {
  88. return float(double(value) * fromIntegerScaleI32);
  89. }
  90. struct Chunk {
  91. String name;
  92. SafePointer<const uint8_t> chunkStart;
  93. intptr_t chunkSize = 0;
  94. Chunk(const ReadableString &name, const Buffer &buffer)
  95. : name(name), chunkStart(buffer_getSafeData<uint8_t>(buffer, "Chunk buffer")), chunkSize(buffer_getSize(buffer)) {}
  96. Chunk(const ReadableString &name, SafePointer<const uint8_t> chunkStart, intptr_t chunkSize)
  97. : name(name), chunkStart(chunkStart), chunkSize(chunkSize) {}
  98. Chunk() {}
  99. };
  100. static Buffer combineRiffChunks(List<Chunk> subChunks) {
  101. uintptr_t payloadSize = 4u; // "WAVE"
  102. for (intptr_t s = 0; s < subChunks.length(); s++) {
  103. payloadSize += 8 + subChunks[s].chunkSize;
  104. }
  105. uintptr_t totalSize = payloadSize + 8u; // RIFF size
  106. Buffer result = buffer_create(totalSize);
  107. SafePointer<uint8_t> targetBytes = buffer_getSafeData<uint8_t>(result, "RIFF encoding target buffer");
  108. targetBytes[0] = 'R';
  109. targetBytes[1] = 'I';
  110. targetBytes[2] = 'F';
  111. targetBytes[3] = 'F';
  112. targetBytes += 4;
  113. format_writeU32_LE(targetBytes, payloadSize);
  114. targetBytes += 4;
  115. targetBytes[0] = 'W';
  116. targetBytes[1] = 'A';
  117. targetBytes[2] = 'V';
  118. targetBytes[3] = 'E';
  119. targetBytes += 4;
  120. for (intptr_t s = 0; s < subChunks.length(); s++) {
  121. uintptr_t subChunkSize = subChunks[s].chunkSize;
  122. targetBytes[0] = char(subChunks[s].name[0]);
  123. targetBytes[1] = char(subChunks[s].name[1]);
  124. targetBytes[2] = char(subChunks[s].name[2]);
  125. targetBytes[3] = char(subChunks[s].name[3]);
  126. targetBytes += 4;
  127. format_writeU32_LE(targetBytes, subChunkSize);
  128. targetBytes += 4;
  129. safeMemoryCopy(targetBytes, subChunks[s].chunkStart, subChunkSize);
  130. targetBytes += subChunkSize;
  131. }
  132. return result;
  133. }
  134. Buffer sound_encode_RiffWave(const SoundBuffer &sound, RiffWaveFormat format, RoundingMethod roundingMethod) {
  135. uint32_t bitsPerSample = getSampleBits(format);
  136. uint32_t bytesPerSample = bitsPerSample / 8;
  137. uint32_t channelCount = sound_getChannelCount(sound);
  138. uint32_t samplesPerChannel = sound_getSamplesPerChannel(sound);
  139. uint32_t blockAlign = channelCount * bytesPerSample;
  140. uint32_t dataBytes = blockAlign * samplesPerChannel;
  141. uint32_t sampleRate = sound_getSampleRate(sound);
  142. uint32_t bytesPerSecond = blockAlign * sampleRate;
  143. Buffer fmt = buffer_create(16);
  144. SafePointer<uint8_t> formatBytes = buffer_getSafeData<uint8_t>(fmt, "RIFF encoding format buffer");
  145. format_writeU16_LE(formatBytes + fmtOffset_audioFormat, 1); // PCM
  146. format_writeU16_LE(formatBytes + fmtOffset_channelCount, channelCount);
  147. format_writeU32_LE(formatBytes + fmtOffset_sampleRate, sampleRate);
  148. format_writeU32_LE(formatBytes + fmtOffset_bytesPerSecond, bytesPerSecond);
  149. format_writeU16_LE(formatBytes + fmtOffset_blockAlign, blockAlign);
  150. format_writeU16_LE(formatBytes + fmtOffset_bitsPerSample, bitsPerSample);
  151. Buffer data = buffer_create(dataBytes);
  152. SafePointer<uint8_t> target = buffer_getSafeData<uint8_t>(data, "RIFF encoding data buffer");
  153. SafePointer<const float> source = sound_getSafePointer(sound);
  154. uintptr_t totalSamples = channelCount * samplesPerChannel;
  155. if (format == RiffWaveFormat::RawU8) {
  156. for (uintptr_t s = 0; s < totalSamples; s++) {
  157. target[s] = floatToNormalizedU8(source[s], roundingMethod);
  158. }
  159. } else if (format == RiffWaveFormat::RawI16) {
  160. for (uintptr_t s = 0; s < totalSamples; s++) {
  161. format_writeI16_LE(target + s * bytesPerSample, floatToNormalizedI16(source[s], roundingMethod));
  162. }
  163. } else if (format == RiffWaveFormat::RawI24) {
  164. for (uintptr_t s = 0; s < totalSamples; s++) {
  165. format_writeI24_LE(target + s * bytesPerSample, floatToNormalizedI24(source[s], roundingMethod));
  166. }
  167. } else if (format == RiffWaveFormat::RawI32) {
  168. for (uintptr_t s = 0; s < totalSamples; s++) {
  169. format_writeI32_LE(target + s * bytesPerSample, floatToNormalizedI32(source[s], roundingMethod));
  170. }
  171. }
  172. return combineRiffChunks(List<Chunk>(Chunk(U"fmt ", fmt), Chunk(U"data", data)));
  173. }
  174. static String readChar4(SafePointer<const uint8_t> nameStart) {
  175. String name;
  176. for (uintptr_t b = 0; b < 4; b++) {
  177. string_appendChar(name, DsrChar(nameStart[b]));
  178. }
  179. return name;
  180. }
  181. static void getRiffChunks(const Chunk &parentChunk, std::function<void(const ReadableString &name, const Chunk &chunk)> returnChunk) {
  182. SafePointer<const uint8_t> chunkStart = parentChunk.chunkStart;
  183. SafePointer<const uint8_t> chunkEnd = chunkStart + parentChunk.chunkSize;
  184. while (chunkStart.getUnchecked() + 8 <= chunkEnd.getUnchecked()) {
  185. String name = readChar4(chunkStart);
  186. uint32_t chunkSize = format_readU32_LE(chunkStart + 4);
  187. SafePointer<const uint8_t> chunkPayload = chunkStart + 8;
  188. if (chunkPayload.getUnchecked() + chunkSize > chunkEnd.getUnchecked()) {
  189. sendWarning(U"Not enough space remaining (", uint64_t((uintptr_t)chunkEnd.getUnchecked() - (uintptr_t)chunkPayload.getUnchecked()), U" bytes) in the RIFF wave file to read the ", name, U" chunk of ", chunkSize, U" bytes!\n");
  190. return;
  191. }
  192. returnChunk(name, Chunk(name, chunkPayload, chunkSize));
  193. chunkStart = chunkStart + 8 + chunkSize;
  194. }
  195. }
  196. static void getRiffChunks(const Buffer &fileBuffer, std::function<void(const ReadableString &name, const Chunk &chunk)> returnChunk) {
  197. Chunk rootChunk = Chunk(U"RIFF", fileBuffer);
  198. getRiffChunks(rootChunk, [&returnChunk](const ReadableString &name, const Chunk &chunk) {
  199. if (string_match(name, U"RIFF")) {
  200. if (!string_match(readChar4(chunk.chunkStart), U"WAVE")) {
  201. throwError(U"WAVE format expected in RIFF file!\n");
  202. }
  203. getRiffChunks(Chunk(name, chunk.chunkStart + 4, chunk.chunkSize - 4), returnChunk);
  204. }
  205. });
  206. }
  207. SoundBuffer sound_decode_RiffWave(const Buffer &fileBuffer) {
  208. Chunk fmtChunk;
  209. Chunk dataChunk;
  210. bool hasFmt = false;
  211. bool hasData = false;
  212. SafePointer<uint8_t> bufferStart = buffer_getSafeData<uint8_t>(fileBuffer, "File buffer");
  213. getRiffChunks(fileBuffer, [&bufferStart, &fmtChunk, &hasFmt, &dataChunk, &hasData](const ReadableString &name, const Chunk &chunk) {
  214. intptr_t byteOffset = intptr_t(chunk.chunkStart.getUnchecked()) - intptr_t(bufferStart.getUnchecked());
  215. if (string_match(name, U"fmt ")) {
  216. fmtChunk = chunk;
  217. hasFmt = true;
  218. } else if (string_match(name, U"data")) {
  219. dataChunk = chunk;
  220. hasData = true;
  221. }
  222. });
  223. if (!hasFmt || !hasData) {
  224. if (!hasFmt) {
  225. sendWarning(U"Failed to find any fmt chunk in the RIFF wave file!\n");
  226. }
  227. if (!hasData) {
  228. sendWarning(U"Failed to find any data chunk in the RIFF wave file!\n");
  229. }
  230. return SoundBuffer();
  231. }
  232. if (fmtChunk.chunkSize < 16) {
  233. sendWarning(U"The fmt chunk of ", fmtChunk.chunkSize, U" bytes is not large enough in the RIFF wave file!\n");
  234. return SoundBuffer();
  235. }
  236. uintptr_t audioFormat = format_readU16_LE(fmtChunk.chunkStart + fmtOffset_audioFormat);
  237. uintptr_t channelCount = format_readU16_LE(fmtChunk.chunkStart + fmtOffset_channelCount);
  238. uintptr_t sampleRate = format_readU32_LE(fmtChunk.chunkStart + fmtOffset_sampleRate);
  239. uintptr_t bytesPerSecond = format_readU32_LE(fmtChunk.chunkStart + fmtOffset_bytesPerSecond);
  240. uintptr_t blockAlign = format_readU16_LE(fmtChunk.chunkStart + fmtOffset_blockAlign);
  241. uintptr_t bitsPerSample = format_readU16_LE(fmtChunk.chunkStart + fmtOffset_bitsPerSample);
  242. uintptr_t bytesPerSample = bitsPerSample / 8;
  243. uintptr_t dataSize = dataChunk.chunkSize;
  244. uintptr_t blockCount = dataSize / blockAlign;
  245. SoundBuffer result = SoundBuffer(blockCount, channelCount, sampleRate);
  246. SafePointer<float> target = sound_getSafePointer(result);
  247. SafePointer<const uint8_t> waveContent = dataChunk.chunkStart;
  248. if (audioFormat == 1 && bitsPerSample == 8) {
  249. for (uintptr_t b = 0; b < blockCount; b++) {
  250. for (uintptr_t c = 0; c < channelCount; c++) {
  251. *target = floatFromNormalizedU8(waveContent[c]);
  252. target += 1;
  253. }
  254. waveContent += blockAlign;
  255. }
  256. return result;
  257. } else if (audioFormat == 1 && bitsPerSample == 16) {
  258. for (uintptr_t b = 0; b < blockCount; b++) {
  259. for (uintptr_t c = 0; c < channelCount; c++) {
  260. *target = floatFromNormalizedI16(format_readI16_LE(waveContent + c * bytesPerSample));
  261. target += 1;
  262. }
  263. waveContent += blockAlign;
  264. }
  265. return result;
  266. } else if (audioFormat == 1 && bitsPerSample == 24) {
  267. for (uintptr_t b = 0; b < blockCount; b++) {
  268. for (uintptr_t c = 0; c < channelCount; c++) {
  269. *target = floatFromNormalizedI24(format_readI24_LE(waveContent + c * bytesPerSample));
  270. target += 1;
  271. }
  272. waveContent += blockAlign;
  273. }
  274. return result;
  275. } else if (audioFormat == 1 && bitsPerSample == 32) {
  276. for (uintptr_t b = 0; b < blockCount; b++) {
  277. for (uintptr_t c = 0; c < channelCount; c++) {
  278. *target = floatFromNormalizedI32(format_readI32_LE(waveContent + c * bytesPerSample));
  279. target += 1;
  280. }
  281. waveContent += blockAlign;
  282. }
  283. return result;
  284. } else if (audioFormat == 3 && bitsPerSample == 32) {
  285. for (uintptr_t b = 0; b < blockCount; b++) {
  286. for (uintptr_t c = 0; c < channelCount; c++) {
  287. *target = format_bitsToF32_IEEE754(format_readU32_LE(waveContent + c * bytesPerSample));
  288. target += 1;
  289. }
  290. waveContent += blockAlign;
  291. }
  292. return result;
  293. } else if (audioFormat == 3 && bitsPerSample == 64) {
  294. for (uintptr_t b = 0; b < blockCount; b++) {
  295. for (uintptr_t c = 0; c < channelCount; c++) {
  296. *target = format_bitsToF64_IEEE754(format_readU64_LE(waveContent + c * bytesPerSample));
  297. target += 1;
  298. }
  299. waveContent += blockAlign;
  300. }
  301. return result;
  302. } else {
  303. sendWarning(U"Unsupported sound format ", audioFormat, U" of ", bitsPerSample, U" bits in RIFF wave file.\n");
  304. // Returning an empty buffer because of the failure.
  305. return SoundBuffer();
  306. }
  307. return SoundBuffer();
  308. }
  309. enum class SoundFileFormat {
  310. Unknown,
  311. WAV
  312. };
  313. static SoundFileFormat detectSoundFileExtension(const ReadableString& filename) {
  314. SoundFileFormat result = SoundFileFormat::Unknown;
  315. int lastDotIndex = string_findLast(filename, U'.');
  316. if (lastDotIndex != -1) {
  317. String extension = string_upperCase(file_getExtension(filename));
  318. if (string_match(extension, U"WAV")) {
  319. result = SoundFileFormat::WAV;
  320. }
  321. }
  322. return result;
  323. }
  324. SoundBuffer sound_load(const ReadableString& filename, bool mustExist) {
  325. SoundFileFormat extension = detectSoundFileExtension(filename);
  326. Buffer fileContent = file_loadBuffer(filename, mustExist);
  327. SoundBuffer result;
  328. if (buffer_exists(fileContent)) {
  329. if (extension == SoundFileFormat::WAV) {
  330. result = sound_decode_RiffWave(fileContent);
  331. if (mustExist && !sound_exists(result)) {
  332. throwError(U"sound_load: Failed to load the sound at ", filename, U".\n");
  333. }
  334. }
  335. }
  336. return result;
  337. }
  338. bool sound_save(const ReadableString& filename, const SoundBuffer &sound, bool mustWork) {
  339. SoundFileFormat extension = detectSoundFileExtension(filename);
  340. if (extension == SoundFileFormat::WAV) {
  341. Buffer fileContent = sound_encode_RiffWave(sound, RiffWaveFormat::RawI16);
  342. return file_saveBuffer(filename, fileContent, mustWork);
  343. // TODO: Add more sound formats.
  344. } else {
  345. if (mustWork) {
  346. throwError(U"The extension of \"", filename, U"\" did not match any supported sound format!\n");
  347. }
  348. return false;
  349. }
  350. }
  351. bool sound_save_RiffWave(const ReadableString& filename, const SoundBuffer &sound, RiffWaveFormat format, RoundingMethod roundingMethod, bool mustWork) {
  352. SoundFileFormat extension = detectSoundFileExtension(filename);
  353. if (extension == SoundFileFormat::WAV) {
  354. Buffer fileContent = sound_encode_RiffWave(sound, format, roundingMethod);
  355. return file_saveBuffer(filename, fileContent, mustWork);
  356. } else {
  357. if (mustWork) {
  358. throwError(U"The extension of \"", filename, U"\" did not match RIFF wave's extension of *.wav!\n");
  359. }
  360. return false;
  361. }
  362. }
  363. }