sound.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. 
  2. #include "sound.h"
  3. #include "../../soundManagers/soundManagers.h"
  4. using namespace dsr;
  5. #include <future>
  6. #include <atomic>
  7. static const int outputChannels = 2;
  8. static const int outputSampleRate = 44100;
  9. double outputSoundStep = 1.0 / (double)outputSampleRate;
  10. double shortestTime = outputSoundStep * 0.01;
  11. std::future<void> soundFuture;
  12. static std::atomic<bool> soundRunning{true};
  13. static std::mutex soundMutex;
  14. static int soundFormatSize(int soundFormat) {
  15. if (soundFormat == soundFormat_I16) {
  16. return 2;
  17. } else if (soundFormat == soundFormat_F32) {
  18. return 4;
  19. } else {
  20. throwError("Cannot get size of unknown sound format!\n");
  21. return 0;
  22. }
  23. }
  24. static void minMax(float &minimum, float &maximum, float value) {
  25. if (value < minimum) { minimum = value; }
  26. if (value > maximum) { maximum = value; }
  27. }
  28. struct Sound {
  29. String name;
  30. bool fromFile;
  31. int sampleCount;
  32. int sampleRate;
  33. Buffer samples;
  34. int channelCount;
  35. int soundFormat;
  36. Sound(const ReadableString &name, bool fromFile, int sampleCount, int sampleRate, int channelCount, int soundFormat)
  37. : name(name), fromFile(fromFile), sampleCount(sampleCount), sampleRate(sampleRate), samples(buffer_create(sampleCount * channelCount * soundFormatSize(soundFormat))), channelCount(channelCount), soundFormat(soundFormat) {}
  38. float sampleLinear(int64_t floor, int64_t ceiling, double ratio, int channel) {
  39. int bufferIndexF = floor * this->channelCount + channel;
  40. int bufferIndexC = ceiling * this->channelCount + channel;
  41. float a = 0.0, b = 0.0;
  42. if (this->soundFormat == soundFormat_I16) {
  43. SafePointer<int16_t> source = buffer_getSafeData<int16_t>(this->samples, "I16 source sound buffer in sampleLinear");
  44. a = sound_convertI16ToF32(source[bufferIndexF]);
  45. b = sound_convertI16ToF32(source[bufferIndexC]);
  46. } else if (this->soundFormat == soundFormat_F32) {
  47. SafePointer<float> source = buffer_getSafeData<float>(this->samples, "F32 source sound buffer in sampleLinear");
  48. a = source[bufferIndexF];
  49. b = source[bufferIndexC];
  50. }
  51. return b * ratio + a * (1.0 - ratio);
  52. }
  53. float sampleLinear_cyclic(double location, int channel) {
  54. int64_t truncated = (int64_t)location;
  55. int64_t floor = truncated % this->sampleCount;
  56. int64_t ceiling = floor + 1; if (ceiling == sampleCount) { ceiling = 0; }
  57. double ratio = location - truncated;
  58. return this->sampleLinear(floor, ceiling, ratio, channel);
  59. }
  60. float sampleLinear_clamped(double location, int channel) {
  61. int64_t truncated = (int64_t)location;
  62. int64_t floor = truncated; if (floor >= sampleCount) { floor = sampleCount - 1; }
  63. int64_t ceiling = floor + 1; if (ceiling >= sampleCount) { ceiling = sampleCount - 1; }
  64. double ratio = location - truncated;
  65. return this->sampleLinear(floor, ceiling, ratio, channel);
  66. }
  67. void sampleMinMax(float &minimum, float &maximum, int startSample, int endSample, int channel) {
  68. if (startSample < 0) { startSample = 0; }
  69. if (endSample >= this->sampleCount) { endSample = this->sampleCount - 1; }
  70. if (channel < 0) { channel = 0; }
  71. if (channel >= this->channelCount) { channel = this->channelCount - 1; }
  72. int bufferIndex = startSample * this->channelCount + channel;
  73. if (this->soundFormat == soundFormat_I16) {
  74. SafePointer<int16_t> source = buffer_getSafeData<int16_t>(this->samples, "I16 source sound buffer in sampleMinMax");
  75. for (int s = startSample; s <= endSample; s++) {
  76. minMax(minimum, maximum, sound_convertI16ToF32(source[bufferIndex]));
  77. bufferIndex += this->channelCount;
  78. }
  79. } else if (this->soundFormat == soundFormat_F32) {
  80. SafePointer<float> source = buffer_getSafeData<float>(this->samples, "F32 source sound buffer in sampleMinMax");
  81. for (int s = startSample; s <= endSample; s++) {
  82. minMax(minimum, maximum, source[bufferIndex]);
  83. bufferIndex += this->channelCount;
  84. }
  85. }
  86. }
  87. };
  88. List<Sound> sounds;
  89. static int createEmptySoundBuffer(const ReadableString &name, bool fromFile, int sampleCount, int sampleRate, int channelCount, int soundFormat) {
  90. if (sampleCount < 1) { throwError("Cannot create sound buffer without and length!\n");}
  91. if (channelCount < 1) { throwError("Cannot create sound buffer without any channels!\n");}
  92. if (sampleRate < 1) { throwError("Cannot create sound buffer without any sample rate!\n");}
  93. sounds.pushConstruct(name, fromFile, sampleCount, sampleRate, channelCount, soundFormat);
  94. return sounds.length() - 1;
  95. }
  96. int generateMonoSoundBuffer(const ReadableString &name, int sampleCount, int sampleRate, int soundFormat, std::function<double(double time)> generator) {
  97. int result = createEmptySoundBuffer(name, false, sampleCount, sampleRate, 1, soundFormat);
  98. double time = 0.0;
  99. double soundStep = 1.0 / (double)sampleRate;
  100. if (soundFormat == soundFormat_I16) {
  101. SafePointer<int16_t> target = buffer_getSafeData<int16_t>(sounds.last().samples, "I16 target sound buffer");
  102. for (int s = 0; s < sampleCount; s++) {
  103. target[s] = sound_convertF32ToI16(generator(time));
  104. time += soundStep;
  105. }
  106. } else if (soundFormat == soundFormat_F32) {
  107. SafePointer<float> target = buffer_getSafeData<float>(sounds.last().samples, "F32 target sound buffer");
  108. for (int s = 0; s < sampleCount; s++) {
  109. target[s] = generator(time);
  110. time += soundStep;
  111. }
  112. }
  113. return result;
  114. }
  115. uint16_t readU16LE(const SafePointer<uint8_t> source, int firstByteIndex) {
  116. return ((uint16_t)source[firstByteIndex])
  117. | ((uint16_t)source[firstByteIndex + 1] << 8);
  118. }
  119. uint32_t readU32LE(const SafePointer<uint8_t> source, int firstByteIndex) {
  120. return ((uint32_t)source[firstByteIndex])
  121. | ((uint32_t)source[firstByteIndex + 1] << 8)
  122. | ((uint32_t)source[firstByteIndex + 2] << 16)
  123. | ((uint32_t)source[firstByteIndex + 3] << 24);
  124. }
  125. /*struct WaveHeader {
  126. char chunkId[4]; // @0 RIFF
  127. uint32_t chunkSize; //@ 4
  128. char format[4]; // @ 8 WAVE
  129. char subChunkId[4]; // @ 12 fmt
  130. uint32_t subChunkSize; // @ 16
  131. uint16_t audioFormat; // @ 20
  132. uint16_t numChannels; // @ 22
  133. uint32_t sampleRate; // @ 24
  134. uint32_t bytesPerSecond; // @ 28
  135. uint16_t blockAlign; // @ 32
  136. uint16_t bitsPerSample; // @ 34
  137. char dataChunkId[4]; // @ 36
  138. uint32_t dataSize; // @ 40
  139. };*/
  140. static const int waveFileHeaderOffset_chunkId = 0;
  141. static const int waveFileHeaderOffset_chunkSize = 4;
  142. static const int waveFileHeaderOffset_format = 8;
  143. static const int waveFileHeaderOffset_subChunkId = 12;
  144. static const int waveFileHeaderOffset_subChunkSize = 16;
  145. static const int waveFileHeaderOffset_audioFormat = 20;
  146. static const int waveFileHeaderOffset_numChannels = 22;
  147. static const int waveFileHeaderOffset_sampleRate = 24;
  148. static const int waveFileHeaderOffset_bytesPerSecond = 28;
  149. static const int waveFileHeaderOffset_blockAlign = 32;
  150. static const int waveFileHeaderOffset_bitsPerSample = 34;
  151. static const int waveFileHeaderOffset_dataChunkId = 36;
  152. static const int waveFileHeaderOffset_dataSize = 40;
  153. static const int waveFileDataOffset = 44;
  154. int loadWaveSoundFromBuffer(const ReadableString &name, Buffer buffer) {
  155. SafePointer<uint8_t> fileContent = buffer_getSafeData<uint8_t>(buffer, "Wave file buffer");
  156. //uint32_t chunkSize = readU32LE(fileContent, waveFileHeaderOffset_chunkSize);
  157. uint32_t subChunkSize = readU32LE(fileContent, waveFileHeaderOffset_subChunkSize);
  158. uint16_t audioFormat = readU16LE(fileContent, waveFileHeaderOffset_audioFormat);
  159. uint16_t numChannels = readU16LE(fileContent, waveFileHeaderOffset_numChannels);
  160. uint32_t sampleRate = readU32LE(fileContent, waveFileHeaderOffset_sampleRate);
  161. //uint32_t bytesPerSecond = readU32LE(fileContent, waveFileHeaderOffset_bytesPerSecond);
  162. //uint16_t blockAlign = readU16LE(fileContent, waveFileHeaderOffset_blockAlign);
  163. //uint16_t bitsPerSample = readU16LE(fileContent, waveFileHeaderOffset_bitsPerSample);
  164. uint32_t dataSize = readU32LE(fileContent, waveFileHeaderOffset_dataSize);
  165. if (audioFormat != 1) { // Only PCM format supported
  166. throwError(U"Unhandled audio format ", audioFormat, " in wave file.\n"); return -1;
  167. }
  168. int result = -1;
  169. if (subChunkSize == 16) {
  170. if (dataSize > (buffer_getSize(buffer) - waveFileDataOffset)) {
  171. throwError(U"Data size out of bound in wave file.\n"); return -1;
  172. }
  173. int totalSamples = dataSize / 2; // Safer to calculate length from the file's size
  174. result = createEmptySoundBuffer(name, true, totalSamples, sampleRate, numChannels, soundFormat_I16);
  175. SafePointer<int16_t> target = buffer_getSafeData<int16_t>(sounds.last().samples, "I16 target sound buffer");
  176. SafePointer<int16_t> waveContent = buffer_getSafeData<int16_t>(buffer, "Wave file buffer");
  177. waveContent.increaseBytes(waveFileDataOffset);
  178. for (int s = 0; s < totalSamples; s ++) {
  179. target[s] = waveContent[s]; // This part has to assume little endian because the value is signed. :(
  180. }
  181. } else {
  182. throwError(U"Unsupported bit depth ", audioFormat, " in wave file.\n"); return -1;
  183. }
  184. return result;
  185. }
  186. int loadSoundFromFile(const ReadableString &filename, bool mustExist) {
  187. // Try to reuse any previously instance of the file before accessing the file system
  188. for (int s = 0; s < sounds.length(); s++) {
  189. if (sounds[s].fromFile && string_match(sounds[s].name, filename)) {
  190. return s;
  191. }
  192. }
  193. // Assuming the wave format until more are supported.
  194. return loadWaveSoundFromBuffer(filename, file_loadBuffer(filename, mustExist));
  195. }
  196. int getSoundBufferCount() {
  197. return sounds.length();
  198. }
  199. EnvelopeSettings::EnvelopeSettings()
  200. : attack(0.0), decay(0.0), sustain(1.0), release(0.0), hold(0.0), rise(0.0), sustainedSmooth(0.0), releasedSmooth(0.0) {}
  201. EnvelopeSettings::EnvelopeSettings(double attack, double decay, double sustain, double release, double hold, double rise, double sustainedSmooth, double releasedSmooth)
  202. : attack(attack), decay(decay), sustain(sustain), release(release), hold(hold), rise(rise), sustainedSmooth(sustainedSmooth), releasedSmooth(releasedSmooth) {}
  203. static double closerLinear(double &ref, double goal, double maxStep) {
  204. double difference;
  205. if (ref + maxStep < goal) {
  206. difference = maxStep;
  207. ref += maxStep;
  208. } else if (ref - maxStep > goal) {
  209. difference = -maxStep;
  210. ref -= maxStep;
  211. } else {
  212. difference = goal - ref;
  213. ref = goal;
  214. }
  215. return difference;
  216. }
  217. struct Envelope {
  218. // Settings
  219. EnvelopeSettings envelopeSettings;
  220. // TODO: Add different types of smoothing filters and interpolation methods
  221. // Dynamic
  222. int state = 0;
  223. double currentVolume = 0.0, currentGoal = 0.0, releaseVolume = 0.0, timeSinceChange = 0.0;
  224. bool lastSustained = true;
  225. Envelope(const EnvelopeSettings &envelopeSettings)
  226. : envelopeSettings(envelopeSettings) {
  227. // Avoiding division by zero using very short fades
  228. if (this->envelopeSettings.attack < shortestTime) { this->envelopeSettings.attack = shortestTime; }
  229. if (this->envelopeSettings.hold < shortestTime) { this->envelopeSettings.hold = shortestTime; }
  230. if (this->envelopeSettings.decay < shortestTime) { this->envelopeSettings.decay = shortestTime; }
  231. if (this->envelopeSettings.release < shortestTime) { this->envelopeSettings.release = shortestTime; }
  232. }
  233. double getVolume(bool sustained, double seconds) {
  234. if (sustained) {
  235. if (state == 0) {
  236. // Attack
  237. this->currentGoal += seconds / this->envelopeSettings.attack;
  238. if (this->currentGoal > 1.0) {
  239. this->currentGoal = 1.0;
  240. state = 1; this->timeSinceChange = 0.0;
  241. }
  242. } else if (state == 1) {
  243. // Hold
  244. if (this->timeSinceChange < this->envelopeSettings.hold) {
  245. this->currentGoal = 1.0;
  246. } else {
  247. state = 2; this->timeSinceChange = 0.0;
  248. }
  249. } else if (state == 2) {
  250. // Decay
  251. this->currentGoal += (this->envelopeSettings.sustain - 1.0) * seconds / this->envelopeSettings.decay;
  252. if (this->currentGoal < this->envelopeSettings.sustain) {
  253. this->currentGoal = this->envelopeSettings.sustain;
  254. state = 3; this->timeSinceChange = 0.0;
  255. }
  256. } else if (state == 3) {
  257. // Sustain / rise
  258. this->currentGoal += this->envelopeSettings.rise * seconds / this->envelopeSettings.decay;
  259. if (this->currentGoal < 0.0) {
  260. this->currentGoal = 0.0;
  261. } else if (this->currentGoal > 1.0) {
  262. this->currentGoal = 1.0;
  263. }
  264. }
  265. } else {
  266. // Release
  267. if (this->lastSustained) {
  268. this->releaseVolume = this->currentGoal;
  269. }
  270. // Linear release, using releaseVolume to calculate the slope needed for the current release time
  271. this->currentGoal -= this->releaseVolume * seconds / this->envelopeSettings.release;
  272. if (this->currentGoal < 0.0) {
  273. this->currentGoal = 0.0;
  274. }
  275. this->lastSustained = false;
  276. }
  277. double smooth = sustained ? this->envelopeSettings.sustainedSmooth : this->envelopeSettings.releasedSmooth;
  278. if (smooth > 0.0) {
  279. // Move faster to the goal the further away it is
  280. double change = seconds / smooth;
  281. if (change > 1.0) { change = 1.0; }
  282. double keep = 1.0 - change;
  283. this->currentVolume = this->currentVolume * keep + this->currentGoal * change;
  284. // Move slowly towards the goal with a fixed speed to finally reach zero and stop sampling the sound
  285. closerLinear(this->currentVolume, this->currentGoal, seconds * 0.01);
  286. } else {
  287. this->currentVolume = this->currentGoal;
  288. }
  289. this->timeSinceChange += seconds;
  290. return this->currentVolume;
  291. }
  292. bool done() {
  293. return this->currentVolume <= 0.0000000001 && !this->lastSustained;
  294. }
  295. };
  296. // Currently playing sounds
  297. struct Player {
  298. // Unique identifier
  299. int64_t playerID;
  300. // Assigned from instrument
  301. int soundIndex;
  302. Envelope envelope;
  303. bool repeat;
  304. double leftVolume, rightVolume;
  305. double speed; // TODO: Use for playing with interpolation
  306. double location = 0; // Floating sample index
  307. bool sustained = true; // If the sound is still being generated
  308. Player(int64_t playerID, int soundIndex, bool repeat, double leftVolume, double rightVolume, double speed, const EnvelopeSettings &envelopeSettings)
  309. : playerID(playerID), soundIndex(soundIndex), envelope(envelopeSettings), repeat(repeat), leftVolume(leftVolume), rightVolume(rightVolume), speed(speed) {}
  310. };
  311. List<Player> players;
  312. int64_t nextPlayerID = 0;
  313. int playSound(int soundIndex, bool repeat, double leftVolume, double rightVolume, double speed, const EnvelopeSettings &envelopeSettings) {
  314. int result;
  315. soundMutex.lock();
  316. result = nextPlayerID;
  317. players.pushConstruct(nextPlayerID, soundIndex, repeat, leftVolume, rightVolume, speed, envelopeSettings);
  318. nextPlayerID++;
  319. soundMutex.unlock();
  320. return result;
  321. }
  322. int playSound(int soundIndex, bool repeat, double leftVolume, double rightVolume, double speed) {
  323. return playSound(soundIndex, repeat, leftVolume, rightVolume, speed, EnvelopeSettings());
  324. }
  325. static int findSound(int64_t playerID) {
  326. for (int p = 0; p < players.length(); p++) {
  327. if (players[p].playerID == playerID) {
  328. return p;
  329. }
  330. }
  331. return -1;
  332. }
  333. void releaseSound(int64_t playerID) {
  334. if (playerID != -1) {
  335. soundMutex.lock();
  336. int index = findSound(playerID);
  337. if (index > -1) {
  338. players[index].sustained = false;;
  339. }
  340. soundMutex.unlock();
  341. }
  342. }
  343. void stopSound(int64_t playerID) {
  344. if (playerID != -1) {
  345. soundMutex.lock();
  346. int index = findSound(playerID);
  347. if (index > -1) {
  348. players.remove(index);
  349. }
  350. soundMutex.unlock();
  351. }
  352. }
  353. void stopAllSounds() {
  354. soundMutex.lock();
  355. players.clear();
  356. soundMutex.unlock();
  357. }
  358. #define PREPARE_SAMPLE \
  359. double envelope = player->envelope.getVolume(player->sustained, outputSoundStep);
  360. #define NEXT_SAMPLE_CYCLIC \
  361. player->location += sampleStep; \
  362. if (player->location >= sourceSampleCount) { \
  363. player->location -= sourceSampleCount; \
  364. } \
  365. if (player->envelope.done()) { \
  366. players.remove(p); \
  367. break; \
  368. }
  369. #define NEXT_SAMPLE_ONCE \
  370. player->location += sampleStep; \
  371. if (player->location >= sourceSampleCount) { \
  372. players.remove(p); \
  373. break; \
  374. } \
  375. if (player->envelope.done()) { \
  376. players.remove(p); \
  377. break; \
  378. }
  379. void sound_initialize() {
  380. // Start a worker thread mixing sounds in realtime
  381. std::function<void()> task = []() {
  382. sound_streamToSpeakers(outputChannels, outputSampleRate, [](float *target, int requestedSamples) -> bool {
  383. // Anyone wanting to change the played sounds from another thread will have to wait until this section has finished processing
  384. soundMutex.lock();
  385. // TODO: Create a graph of filters for different instruments
  386. // TODO: Let the output buffer be just another sound buffer, so that a reusable function can stream to sections of larger sound buffers
  387. for (int p = players.length() - 1; p >= 0; p--) {
  388. Player *player = &(players[p]);
  389. int soundIndex = player->soundIndex;
  390. Sound *sound = &(sounds[soundIndex]);
  391. int sourceSampleCount = sound->sampleCount;
  392. double sampleStep = player->speed * sound->sampleRate * outputSoundStep;
  393. if (player->repeat) {
  394. if (sound->channelCount == 1) { // Mono source
  395. for (int t = 0; t < requestedSamples; t++) {
  396. PREPARE_SAMPLE
  397. float monoSource = sound->sampleLinear_cyclic(player->location, 0) * envelope;
  398. target[t * outputChannels + 0] += monoSource * player->leftVolume;
  399. target[t * outputChannels + 1] += monoSource * player->rightVolume;
  400. NEXT_SAMPLE_CYCLIC
  401. }
  402. } else if (sound->channelCount == 2) { // Stereo source
  403. for (int t = 0; t < requestedSamples; t++) {
  404. PREPARE_SAMPLE
  405. target[t * outputChannels + 0] += sound->sampleLinear_cyclic(player->location, 0) * envelope * player->leftVolume;
  406. target[t * outputChannels + 1] += sound->sampleLinear_cyclic(player->location, 1) * envelope * player->rightVolume;
  407. NEXT_SAMPLE_CYCLIC
  408. }
  409. }
  410. } else {
  411. if (sound->channelCount == 1) { // Mono source
  412. for (int t = 0; t < requestedSamples; t++) {
  413. PREPARE_SAMPLE
  414. float monoSource = sound->sampleLinear_clamped(player->location, 0) * envelope;
  415. target[t * outputChannels + 0] += monoSource * player->leftVolume;
  416. target[t * outputChannels + 1] += monoSource * player->rightVolume;
  417. NEXT_SAMPLE_ONCE
  418. }
  419. } else if (sound->channelCount == 2) { // Stereo source
  420. for (int t = 0; t < requestedSamples; t++) {
  421. PREPARE_SAMPLE
  422. target[t * outputChannels + 0] += sound->sampleLinear_clamped(player->location, 0) * envelope * player->leftVolume;
  423. target[t * outputChannels + 1] += sound->sampleLinear_clamped(player->location, 1) * envelope * player->rightVolume;
  424. NEXT_SAMPLE_ONCE
  425. }
  426. }
  427. }
  428. }
  429. soundMutex.unlock();
  430. return soundRunning;
  431. });
  432. };
  433. soundFuture = std::async(std::launch::async, task);
  434. }
  435. void sound_terminate() {
  436. if (soundRunning) {
  437. soundRunning = false;
  438. if (soundFuture.valid()) {
  439. soundFuture.wait();
  440. }
  441. }
  442. }