sound.cpp 19 KB

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