sound.cpp 19 KB

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