audio.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* MegaZeux
  2. *
  3. * Copyright (C) 2016 Adrian Siekierka <asiekierka@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "../../src/event.h"
  20. #include "../../src/platform.h"
  21. #include "../../src/graphics.h"
  22. #include "../../src/audio/audio.h"
  23. #include <3ds.h>
  24. #include <string.h>
  25. #include "platform.h"
  26. #ifdef CONFIG_AUDIO
  27. static u8 *audio_buffer;
  28. static ndspWaveBuf ndsp_buffer[2];
  29. static int buffer_size;
  30. static bool soundFillBlock = false;
  31. static void ndsp_callback(void *dud)
  32. {
  33. if(ndsp_buffer[soundFillBlock].status == NDSP_WBUF_DONE)
  34. {
  35. audio_callback(ndsp_buffer[soundFillBlock].data_pcm16, buffer_size);
  36. DSP_FlushDataCache(ndsp_buffer[soundFillBlock].data_pcm8,
  37. audio.buffer_samples * 4);
  38. ndspChnWaveBufAdd(0, &ndsp_buffer[soundFillBlock]);
  39. soundFillBlock = !soundFillBlock;
  40. }
  41. }
  42. void init_audio_platform(struct config_info *conf)
  43. {
  44. float mix[12];
  45. audio.buffer_samples = conf->audio_buffer_samples & ~7;
  46. if(!audio.buffer_samples)
  47. audio.buffer_samples = 2048;
  48. buffer_size = sizeof(Sint16) * 2 * audio.buffer_samples;
  49. audio.mix_buffer = cmalloc(buffer_size * 2);
  50. ndspInit();
  51. memset(mix, 0, sizeof(mix));
  52. mix[0] = mix[1] = 1.0f;
  53. ndspSetOutputMode(NDSP_OUTPUT_STEREO);
  54. ndspSetOutputCount(1);
  55. ndspSetMasterVol(1.0f);
  56. audio_buffer = clinearAlloc(buffer_size * 2, 0x80);
  57. memset(audio_buffer, 0, buffer_size * 2);
  58. ndspSetCallback(ndsp_callback, audio_buffer);
  59. memset(&ndsp_buffer[0], 0, sizeof(ndspWaveBuf));
  60. memset(&ndsp_buffer[1], 0, sizeof(ndspWaveBuf));
  61. ndspChnReset(0);
  62. ndspChnSetInterp(0, NDSP_INTERP_LINEAR);
  63. ndspChnSetRate(0, audio.output_frequency);
  64. ndspChnSetFormat(0, NDSP_CHANNELS(2) | NDSP_ENCODING(NDSP_ENCODING_PCM16));
  65. ndspChnSetMix(0, mix);
  66. ndsp_buffer[0].data_vaddr = &audio_buffer[0];
  67. ndsp_buffer[0].nsamples = audio.buffer_samples;
  68. ndsp_buffer[1].data_vaddr = &audio_buffer[buffer_size];
  69. ndsp_buffer[1].nsamples = audio.buffer_samples;
  70. ndspChnWaveBufAdd(0, &ndsp_buffer[0]);
  71. ndspChnWaveBufAdd(0, &ndsp_buffer[1]);
  72. }
  73. void quit_audio_platform(void)
  74. {
  75. // stub
  76. linearFree(audio_buffer);
  77. ndspExit();
  78. }
  79. #endif // CONFIG_AUDIO