soundAPI.cpp 19 KB

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