iron_audio.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. #include "iron_audio.h"
  2. #ifdef IRON_A1
  3. #include <iron_audio.h>
  4. #include <iron_math.h>
  5. #include <iron_thread.h>
  6. #include <iron_video.h>
  7. #include <assert.h>
  8. #include <stdlib.h>
  9. #include <stdbool.h>
  10. #include <stdint.h>
  11. struct iron_a1_channel {
  12. iron_a1_sound_t *sound;
  13. float position;
  14. bool loop;
  15. volatile float volume;
  16. volatile float pitch;
  17. };
  18. struct iron_a1_stream_channel {
  19. iron_a1_sound_stream_t *stream;
  20. int position;
  21. };
  22. struct iron_internal_video_channel {
  23. struct iron_internal_video_sound_stream *stream;
  24. int position;
  25. };
  26. static iron_mutex_t mutex;
  27. #define CHANNEL_COUNT 16
  28. static iron_a1_channel_t channels[CHANNEL_COUNT];
  29. static iron_a1_stream_channel_t streamchannels[CHANNEL_COUNT];
  30. static iron_internal_video_channel_t videos[CHANNEL_COUNT];
  31. static float sampleLinear(int16_t *data, float position) {
  32. int pos1 = (int)position;
  33. int pos2 = (int)(position + 1);
  34. float sample1 = data[pos1] / 32767.0f;
  35. float sample2 = data[pos2] / 32767.0f;
  36. float a = position - pos1;
  37. return sample1 * (1 - a) + sample2 * a;
  38. }
  39. static void iron_a2_on_a1_mix(iron_a2_buffer_t *buffer, uint32_t samples, void *userdata) {
  40. iron_a1_mix(buffer, samples);
  41. }
  42. void iron_a1_mix(iron_a2_buffer_t *buffer, uint32_t samples) {
  43. for (uint32_t i = 0; i < samples; ++i) {
  44. float left_value = 0.0f;
  45. float right_value = 0.0f;
  46. iron_mutex_lock(&mutex);
  47. for (int i = 0; i < CHANNEL_COUNT; ++i) {
  48. if (channels[i].sound != NULL) {
  49. left_value += sampleLinear(channels[i].sound->left, channels[i].position) * channels[i].volume * channels[i].sound->volume;
  50. right_value = iron_max(iron_min(right_value, 1.0f), -1.0f);
  51. right_value += sampleLinear(channels[i].sound->right, channels[i].position) * channels[i].volume * channels[i].sound->volume;
  52. left_value = iron_max(iron_min(left_value, 1.0f), -1.0f);
  53. channels[i].position += channels[i].pitch / channels[i].sound->sample_rate_pos;
  54. // channels[i].position += 2;
  55. if (channels[i].position + 1 >= channels[i].sound->size) {
  56. if (channels[i].loop) {
  57. channels[i].position = 0;
  58. }
  59. else {
  60. channels[i].sound = NULL;
  61. }
  62. }
  63. }
  64. }
  65. for (int i = 0; i < CHANNEL_COUNT; ++i) {
  66. if (streamchannels[i].stream != NULL) {
  67. float *samples = iron_a1_sound_stream_next_frame(streamchannels[i].stream);
  68. left_value += samples[0] * iron_a1_sound_stream_volume(streamchannels[i].stream);
  69. left_value = iron_max(iron_min(left_value, 1.0f), -1.0f);
  70. right_value += samples[1] * iron_a1_sound_stream_volume(streamchannels[i].stream);
  71. right_value = iron_max(iron_min(right_value, 1.0f), -1.0f);
  72. if (iron_a1_sound_stream_ended(streamchannels[i].stream)) {
  73. streamchannels[i].stream = NULL;
  74. }
  75. }
  76. }
  77. for (int i = 0; i < CHANNEL_COUNT; ++i) {
  78. if (videos[i].stream != NULL) {
  79. float *samples = iron_internal_video_sound_stream_next_frame(videos[i].stream);
  80. left_value += samples[0];
  81. left_value = iron_max(iron_min(left_value, 1.0f), -1.0f);
  82. right_value += samples[1];
  83. right_value = iron_max(iron_min(right_value, 1.0f), -1.0f);
  84. if (iron_internal_video_sound_stream_ended(videos[i].stream)) {
  85. videos[i].stream = NULL;
  86. }
  87. }
  88. }
  89. iron_mutex_unlock(&mutex);
  90. assert(buffer->channel_count >= 2);
  91. buffer->channels[0][buffer->write_location] = left_value;
  92. buffer->channels[1][buffer->write_location] = right_value;
  93. buffer->write_location += 1;
  94. if (buffer->write_location >= buffer->data_size) {
  95. buffer->write_location = 0;
  96. }
  97. }
  98. }
  99. void iron_a1_init(void) {
  100. for (int i = 0; i < CHANNEL_COUNT; ++i) {
  101. channels[i].sound = NULL;
  102. channels[i].position = 0;
  103. }
  104. for (int i = 0; i < CHANNEL_COUNT; ++i) {
  105. streamchannels[i].stream = NULL;
  106. streamchannels[i].position = 0;
  107. }
  108. iron_mutex_init(&mutex);
  109. iron_a2_init();
  110. iron_a2_set_callback(iron_a2_on_a1_mix, NULL);
  111. }
  112. iron_a1_channel_t *iron_a1_play_sound(iron_a1_sound_t *sound, bool loop, float pitch, bool unique) {
  113. iron_a1_channel_t *channel = NULL;
  114. iron_mutex_lock(&mutex);
  115. bool found = false;
  116. for (int i = 0; i < CHANNEL_COUNT; ++i) {
  117. if (channels[i].sound == sound) {
  118. found = true;
  119. break;
  120. }
  121. }
  122. if (!found || !unique) {
  123. for (int i = 0; i < CHANNEL_COUNT; ++i) {
  124. if (channels[i].sound == NULL) {
  125. channels[i].sound = sound;
  126. channels[i].position = 0;
  127. channels[i].loop = loop;
  128. channels[i].pitch = pitch;
  129. channels[i].volume = sound->volume;
  130. channel = &channels[i];
  131. break;
  132. }
  133. }
  134. }
  135. iron_mutex_unlock(&mutex);
  136. return channel;
  137. }
  138. void iron_a1_stop_sound(iron_a1_sound_t *sound) {
  139. iron_mutex_lock(&mutex);
  140. for (int i = 0; i < CHANNEL_COUNT; ++i) {
  141. if (channels[i].sound == sound) {
  142. channels[i].sound = NULL;
  143. channels[i].position = 0;
  144. break;
  145. }
  146. }
  147. iron_mutex_unlock(&mutex);
  148. }
  149. void iron_a1_play_sound_stream(iron_a1_sound_stream_t *stream) {
  150. iron_mutex_lock(&mutex);
  151. for (int i = 0; i < CHANNEL_COUNT; ++i) {
  152. if (streamchannels[i].stream == stream) {
  153. streamchannels[i].stream = NULL;
  154. streamchannels[i].position = 0;
  155. break;
  156. }
  157. }
  158. for (int i = 0; i < CHANNEL_COUNT; ++i) {
  159. if (streamchannels[i].stream == NULL) {
  160. streamchannels[i].stream = stream;
  161. streamchannels[i].position = 0;
  162. break;
  163. }
  164. }
  165. iron_mutex_unlock(&mutex);
  166. }
  167. void iron_a1_stop_sound_stream(iron_a1_sound_stream_t *stream) {
  168. iron_mutex_lock(&mutex);
  169. for (int i = 0; i < CHANNEL_COUNT; ++i) {
  170. if (streamchannels[i].stream == stream) {
  171. streamchannels[i].stream = NULL;
  172. streamchannels[i].position = 0;
  173. break;
  174. }
  175. }
  176. iron_mutex_unlock(&mutex);
  177. }
  178. void iron_internal_play_video_sound_stream(struct iron_internal_video_sound_stream *stream) {
  179. iron_mutex_lock(&mutex);
  180. for (int i = 0; i < CHANNEL_COUNT; ++i) {
  181. if (videos[i].stream == NULL) {
  182. videos[i].stream = stream;
  183. videos[i].position = 0;
  184. break;
  185. }
  186. }
  187. iron_mutex_unlock(&mutex);
  188. }
  189. void iron_internal_stop_video_sound_stream(struct iron_internal_video_sound_stream *stream) {
  190. iron_mutex_lock(&mutex);
  191. for (int i = 0; i < CHANNEL_COUNT; ++i) {
  192. if (videos[i].stream == stream) {
  193. videos[i].stream = NULL;
  194. videos[i].position = 0;
  195. break;
  196. }
  197. }
  198. iron_mutex_unlock(&mutex);
  199. }
  200. float iron_a1_channel_get_volume(iron_a1_channel_t *channel) {
  201. return channel->volume;
  202. }
  203. void iron_a1_channel_set_volume(iron_a1_channel_t *channel, float volume) {
  204. IRON_ATOMIC_EXCHANGE_FLOAT(&channel->volume, volume);
  205. }
  206. void iron_a1_channel_set_pitch(iron_a1_channel_t *channel, float pitch) {
  207. IRON_ATOMIC_EXCHANGE_FLOAT(&channel->pitch, pitch);
  208. }
  209. #define STB_VORBIS_HEADER_ONLY
  210. #include <stb_vorbis.c>
  211. struct WaveData {
  212. uint16_t audioFormat;
  213. uint16_t numChannels;
  214. uint32_t sampleRate;
  215. uint32_t bytesPerSecond;
  216. uint16_t bitsPerSample;
  217. uint32_t dataSize;
  218. uint8_t *data;
  219. };
  220. static void checkFOURCC(uint8_t **data, const char *fourcc) {
  221. for (int i = 0; i < 4; ++i) {
  222. ++*data;
  223. }
  224. }
  225. static void readFOURCC(uint8_t **data, char *fourcc) {
  226. for (int i = 0; i < 4; ++i) {
  227. fourcc[i] = **data;
  228. ++*data;
  229. }
  230. fourcc[4] = 0;
  231. }
  232. static void readChunk(uint8_t **data, struct WaveData *wave) {
  233. char fourcc[5];
  234. readFOURCC(data, fourcc);
  235. uint32_t chunksize = iron_read_u32le(*data);
  236. *data += 4;
  237. if (strcmp(fourcc, "fmt ") == 0) {
  238. wave->audioFormat = iron_read_u16le(*data + 0);
  239. wave->numChannels = iron_read_u16le(*data + 2);
  240. wave->sampleRate = iron_read_u32le(*data + 4);
  241. wave->bytesPerSecond = iron_read_u32le(*data + 8);
  242. wave->bitsPerSample = iron_read_u16le(*data + 14);
  243. *data += chunksize;
  244. }
  245. else if (strcmp(fourcc, "data") == 0) {
  246. wave->dataSize = chunksize;
  247. wave->data = (uint8_t *)malloc(chunksize * sizeof(uint8_t));
  248. memcpy(wave->data, *data, chunksize);
  249. *data += chunksize;
  250. }
  251. else {
  252. *data += chunksize;
  253. }
  254. }
  255. static int16_t convert8to16(uint8_t sample) {
  256. return (sample - 127) << 8;
  257. }
  258. static void splitStereo8(uint8_t *data, int size, int16_t *left, int16_t *right) {
  259. for (int i = 0; i < size; ++i) {
  260. left[i] = convert8to16(data[i * 2 + 0]);
  261. right[i] = convert8to16(data[i * 2 + 1]);
  262. }
  263. }
  264. static void splitStereo16(int16_t *data, int size, int16_t *left, int16_t *right) {
  265. for (int i = 0; i < size; ++i) {
  266. left[i] = data[i * 2 + 0];
  267. right[i] = data[i * 2 + 1];
  268. }
  269. }
  270. static void splitMono8(uint8_t *data, int size, int16_t *left, int16_t *right) {
  271. for (int i = 0; i < size; ++i) {
  272. left[i] = convert8to16(data[i]);
  273. right[i] = convert8to16(data[i]);
  274. }
  275. }
  276. static void splitMono16(int16_t *data, int size, int16_t *left, int16_t *right) {
  277. for (int i = 0; i < size; ++i) {
  278. left[i] = data[i];
  279. right[i] = data[i];
  280. }
  281. }
  282. #define MAXIMUM_SOUNDS 1024
  283. static iron_a1_sound_t sounds[MAXIMUM_SOUNDS] = {0};
  284. static iron_a1_sound_t *find_sound(void) {
  285. for (int i = 0; i < MAXIMUM_SOUNDS; ++i) {
  286. if (!sounds[i].in_use) {
  287. return &sounds[i];
  288. }
  289. }
  290. return NULL;
  291. }
  292. iron_a1_sound_t *iron_a1_sound_create(const char *filename) {
  293. iron_a1_sound_t *sound = find_sound();
  294. assert(sound != NULL);
  295. sound->in_use = true;
  296. sound->volume = 1.0f;
  297. sound->size = 0;
  298. sound->left = NULL;
  299. sound->right = NULL;
  300. size_t filenameLength = strlen(filename);
  301. uint8_t *data = NULL;
  302. if (strncmp(&filename[filenameLength - 4], ".ogg", 4) == 0) {
  303. iron_file_reader_t file;
  304. if (!iron_file_reader_open(&file, filename, IRON_FILE_TYPE_ASSET)) {
  305. sound->in_use = false;
  306. return NULL;
  307. }
  308. uint8_t *filedata = (uint8_t *)malloc(iron_file_reader_size(&file));
  309. iron_file_reader_read(&file, filedata, iron_file_reader_size(&file));
  310. iron_file_reader_close(&file);
  311. int channels, sample_rate;
  312. int samples = stb_vorbis_decode_memory(filedata, (int)iron_file_reader_size(&file), &channels, &sample_rate, (short **)&data);
  313. sound->channel_count = (uint8_t)channels;
  314. sound->samples_per_second = (uint32_t)sample_rate;
  315. sound->size = samples * 2 * sound->channel_count;
  316. sound->bits_per_sample = 16;
  317. free(filedata);
  318. }
  319. else if (strncmp(&filename[filenameLength - 4], ".wav", 4) == 0) {
  320. struct WaveData wave = {0};
  321. {
  322. iron_file_reader_t file;
  323. if (!iron_file_reader_open(&file, filename, IRON_FILE_TYPE_ASSET)) {
  324. sound->in_use = false;
  325. return NULL;
  326. }
  327. uint8_t *filedata = (uint8_t *)malloc(iron_file_reader_size(&file));
  328. iron_file_reader_read(&file, filedata, iron_file_reader_size(&file));
  329. iron_file_reader_close(&file);
  330. uint8_t *data = filedata;
  331. checkFOURCC(&data, "RIFF");
  332. uint32_t filesize = iron_read_u32le(data);
  333. data += 4;
  334. checkFOURCC(&data, "WAVE");
  335. while (data + 8 - filedata < (intptr_t)filesize) {
  336. readChunk(&data, &wave);
  337. }
  338. free(filedata);
  339. }
  340. sound->bits_per_sample = (uint8_t)wave.bitsPerSample;
  341. sound->channel_count = (uint8_t)wave.numChannels;
  342. sound->samples_per_second = wave.sampleRate;
  343. data = wave.data;
  344. sound->size = wave.dataSize;
  345. }
  346. else {
  347. assert(false);
  348. }
  349. if (sound->channel_count == 1) {
  350. if (sound->bits_per_sample == 8) {
  351. sound->left = (int16_t *)malloc(sound->size * sizeof(int16_t));
  352. sound->right = (int16_t *)malloc(sound->size * sizeof(int16_t));
  353. splitMono8(data, sound->size, sound->left, sound->right);
  354. }
  355. else if (sound->bits_per_sample == 16) {
  356. sound->size /= 2;
  357. sound->left = (int16_t *)malloc(sound->size * sizeof(int16_t));
  358. sound->right = (int16_t *)malloc(sound->size * sizeof(int16_t));
  359. splitMono16((int16_t *)data, sound->size, sound->left, sound->right);
  360. }
  361. }
  362. else {
  363. // Left and right channel are in s16 audio stream, alternating.
  364. if (sound->bits_per_sample == 8) {
  365. sound->size /= 2;
  366. sound->left = (int16_t *)malloc(sound->size * sizeof(int16_t));
  367. sound->right = (int16_t *)malloc(sound->size * sizeof(int16_t));
  368. splitStereo8(data, sound->size, sound->left, sound->right);
  369. }
  370. else if (sound->bits_per_sample == 16) {
  371. sound->size /= 4;
  372. sound->left = (int16_t *)malloc(sound->size * sizeof(int16_t));
  373. sound->right = (int16_t *)malloc(sound->size * sizeof(int16_t));
  374. splitStereo16((int16_t *)data, sound->size, sound->left, sound->right);
  375. }
  376. }
  377. sound->sample_rate_pos = 44100 / (float)sound->samples_per_second;
  378. free(data);
  379. return sound;
  380. }
  381. void iron_a1_sound_destroy(iron_a1_sound_t *sound) {
  382. free(sound->left);
  383. free(sound->right);
  384. sound->left = NULL;
  385. sound->right = NULL;
  386. sound->in_use = false;
  387. }
  388. float iron_a1_sound_volume(iron_a1_sound_t *sound) {
  389. return sound->volume;
  390. }
  391. void iron_a1_sound_set_volume(iron_a1_sound_t *sound, float value) {
  392. sound->volume = value;
  393. }
  394. static iron_a1_sound_stream_t streams[256];
  395. static int nextStream = 0;
  396. static uint8_t buffer[1024 * 10];
  397. static int bufferIndex;
  398. iron_a1_sound_stream_t *iron_a1_sound_stream_create(const char *filename, bool looping) {
  399. iron_a1_sound_stream_t *stream = &streams[nextStream];
  400. stream->myLooping = looping;
  401. stream->myVolume = 1;
  402. stream->rateDecodedHack = false;
  403. stream->end = false;
  404. iron_file_reader_t file;
  405. iron_file_reader_open(&file, filename, IRON_FILE_TYPE_ASSET);
  406. stream->buffer = &buffer[bufferIndex];
  407. bufferIndex += (int)iron_file_reader_size(&file);
  408. uint8_t *filecontent = (uint8_t *)malloc(iron_file_reader_size(&file));
  409. iron_file_reader_read(&file, filecontent, iron_file_reader_size(&file));
  410. iron_file_reader_close(&file);
  411. memcpy(stream->buffer, filecontent, iron_file_reader_size(&file));
  412. free(filecontent);
  413. stream->vorbis = stb_vorbis_open_memory(buffer, (int)iron_file_reader_size(&file), NULL, NULL);
  414. if (stream->vorbis != NULL) {
  415. stb_vorbis_info info = stb_vorbis_get_info(stream->vorbis);
  416. stream->chans = info.channels;
  417. stream->rate = info.sample_rate;
  418. }
  419. else {
  420. stream->chans = 2;
  421. stream->rate = 22050;
  422. }
  423. ++nextStream;
  424. return stream;
  425. }
  426. int iron_a1_sound_stream_channels(iron_a1_sound_stream_t *stream) {
  427. return stream->chans;
  428. }
  429. int iron_a1_sound_stream_sample_rate(iron_a1_sound_stream_t *stream) {
  430. return stream->rate;
  431. }
  432. bool iron_a1_sound_stream_looping(iron_a1_sound_stream_t *stream) {
  433. return stream->myLooping;
  434. }
  435. void iron_a1_sound_stream_set_looping(iron_a1_sound_stream_t *stream, bool loop) {
  436. stream->myLooping = loop;
  437. }
  438. float iron_a1_sound_stream_volume(iron_a1_sound_stream_t *stream) {
  439. return stream->myVolume;
  440. }
  441. void iron_a1_sound_stream_set_volume(iron_a1_sound_stream_t *stream, float value) {
  442. stream->myVolume = value;
  443. }
  444. bool iron_a1_sound_stream_ended(iron_a1_sound_stream_t *stream) {
  445. return stream->end;
  446. }
  447. float iron_a1_sound_stream_length(iron_a1_sound_stream_t *stream) {
  448. if (stream->vorbis == NULL)
  449. return 0;
  450. return stb_vorbis_stream_length_in_seconds(stream->vorbis);
  451. }
  452. float iron_a1_sound_stream_position(iron_a1_sound_stream_t *stream) {
  453. if (stream->vorbis == NULL)
  454. return 0;
  455. return stb_vorbis_get_sample_offset(stream->vorbis) / stb_vorbis_stream_length_in_samples(stream->vorbis) * iron_a1_sound_stream_length(stream);
  456. }
  457. void iron_a1_sound_stream_reset(iron_a1_sound_stream_t *stream) {
  458. if (stream->vorbis != NULL)
  459. stb_vorbis_seek_start(stream->vorbis);
  460. stream->end = false;
  461. stream->rateDecodedHack = false;
  462. }
  463. float *iron_a1_sound_stream_next_frame(iron_a1_sound_stream_t *stream) {
  464. if (stream->vorbis == NULL) {
  465. for (int i = 0; i < stream->chans; ++i) {
  466. stream->samples[i] = 0;
  467. }
  468. return stream->samples;
  469. }
  470. if (stream->rate == 22050) {
  471. if (stream->rateDecodedHack) {
  472. stream->rateDecodedHack = false;
  473. return stream->samples;
  474. }
  475. }
  476. float left, right;
  477. float *samples_array[2] = {&left, &right};
  478. int read = stb_vorbis_get_samples_float(stream->vorbis, stream->chans, samples_array, 1);
  479. if (read == 0) {
  480. if (iron_a1_sound_stream_looping(stream)) {
  481. stb_vorbis_seek_start(stream->vorbis);
  482. stb_vorbis_get_samples_float(stream->vorbis, stream->chans, samples_array, 1);
  483. }
  484. else {
  485. stream->end = true;
  486. for (int i = 0; i < stream->chans; ++i) {
  487. stream->samples[i] = 0;
  488. }
  489. return stream->samples;
  490. }
  491. }
  492. stream->samples[0] = samples_array[0][0];
  493. stream->samples[1] = samples_array[1][0];
  494. stream->rateDecodedHack = true;
  495. return stream->samples;
  496. }
  497. #endif
  498. #define IRON_IMPLEMENTATION_AUDIO2
  499. #include "iron_audio.h"