AlsaSound.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 
  2. // Use -lasound for linking to the winmm library in GCC/G++
  3. // Install on Arch: sudo pacman -S libasound-dev
  4. // Install on Debian: sudo apt-get install libasound-dev
  5. #include "../DFPSR/api/soundAPI.h"
  6. #include "../DFPSR/base/simd.h"
  7. #include <alsa/asoundlib.h>
  8. namespace dsr {
  9. static snd_pcm_t *pcm = nullptr;
  10. static Buffer outputBuffer, floatBuffer;
  11. static SafePointer<int16_t> outputData;
  12. static SafePointer<float> floatData;
  13. static void allocateBuffers(int neededElements) {
  14. outputBuffer = buffer_create(neededElements * sizeof(int16_t));
  15. floatBuffer = buffer_create(neededElements * sizeof(float));
  16. outputData = buffer_getSafeData<int16_t>(outputBuffer, "Output data");
  17. floatData = buffer_getSafeData<float>(floatBuffer, "Output data");
  18. }
  19. static void terminateSound() {
  20. if (pcm) {
  21. snd_pcm_drop(pcm);
  22. snd_pcm_drain(pcm);
  23. snd_pcm_close(pcm);
  24. pcm = nullptr;
  25. }
  26. }
  27. bool sound_streamToSpeakers(int32_t channels, int32_t sampleRate, std::function<bool(SafePointer<float> data, int32_t length)> soundOutput) {
  28. int errorCode;
  29. if ((errorCode = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
  30. terminateSound();
  31. sendWarning(U"Cannot open sound device. (", snd_strerror(errorCode), U")\n");
  32. return false;
  33. }
  34. snd_pcm_hw_params_t *hardwareParameters;
  35. snd_pcm_hw_params_alloca(&hardwareParameters);
  36. snd_pcm_hw_params_any(pcm, hardwareParameters);
  37. if ((errorCode = snd_pcm_hw_params_set_access(pcm, hardwareParameters, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
  38. terminateSound();
  39. sendWarning(U"Failed to select interleaved sound. (", snd_strerror(errorCode), U")\n");
  40. return false;
  41. }
  42. if ((errorCode = snd_pcm_hw_params_set_format(pcm, hardwareParameters, SND_PCM_FORMAT_S16_LE)) < 0) {
  43. terminateSound();
  44. sendWarning(U"Failed to select sound format. (", snd_strerror(errorCode), U")\n");
  45. return false;
  46. }
  47. if ((errorCode = snd_pcm_hw_params_set_channels(pcm, hardwareParameters, channels)) < 0) {
  48. terminateSound();
  49. sendWarning(U"Failed to select channel count. (", snd_strerror(errorCode), U")\n");
  50. return false;
  51. }
  52. if ((errorCode = snd_pcm_hw_params_set_buffer_size(pcm, hardwareParameters, 2048)) < 0) {
  53. terminateSound();
  54. sendWarning(U"Failed to select buffer size. (", snd_strerror(errorCode), U")\n");
  55. return false;
  56. }
  57. uint rate = sampleRate;
  58. if ((errorCode = snd_pcm_hw_params_set_rate_near(pcm, hardwareParameters, &rate, 0)) < 0) {
  59. terminateSound();
  60. sendWarning(U"Failed to select approximate sample rate. (", snd_strerror(errorCode), U")\n");
  61. return false;
  62. }
  63. if ((errorCode = snd_pcm_hw_params(pcm, hardwareParameters)) < 0) {
  64. terminateSound();
  65. sendWarning(U"Failed to select hardware parameters. (", snd_strerror(errorCode), U")\n");
  66. return false;
  67. }
  68. // Allocate a buffer for sending data to speakers
  69. snd_pcm_uframes_t samplesPerChannel;
  70. snd_pcm_hw_params_get_period_size(hardwareParameters, &samplesPerChannel, 0);
  71. // Allocate target buffers
  72. int totalSamples = samplesPerChannel * channels;
  73. allocateBuffers(totalSamples);
  74. while (true) {
  75. safeMemorySet(floatData, 0, totalSamples * sizeof(float));
  76. bool keepRunning = soundOutput(floatData, samplesPerChannel);
  77. // Convert to target format so that the sound can be played
  78. for (uint32_t t = 0; t < samplesPerChannel * channels; t+=8) {
  79. // SIMD vectorized sound conversion with scaling and clamping to signed 16-bit integers.
  80. F32x4 lowerFloats = F32x4::readAligned(floatData + t, "sound_streamToSpeakers: Reading lower floats");
  81. F32x4 upperFloats = F32x4::readAligned(floatData + t + 4, "sound_streamToSpeakers: Reading upper floats");
  82. I32x4 lowerInts = truncateToI32(clamp(F32x4(-32768.0f), lowerFloats * 32767.0f, F32x4(32767.0f)));
  83. I32x4 upperInts = truncateToI32(clamp(F32x4(-32768.0f), upperFloats * 32767.0f, F32x4(32767.0f)));
  84. // TODO: Create I16x8 SIMD vectors for processing sound as 16-bit integers?
  85. // Or just move unzip into simd.h with a fallback solution and remove simdExtra.h.
  86. // Or just implement reading and writing of 16-bit signed integers using multiple SIMD registers or smaller memory regions.
  87. IVector4D lower = lowerInts.get();
  88. IVector4D upper = upperInts.get();
  89. outputData[t+0] = (int16_t)lower.x;
  90. outputData[t+1] = (int16_t)lower.y;
  91. outputData[t+2] = (int16_t)lower.z;
  92. outputData[t+3] = (int16_t)lower.w;
  93. outputData[t+4] = (int16_t)upper.x;
  94. outputData[t+5] = (int16_t)upper.y;
  95. outputData[t+6] = (int16_t)upper.z;
  96. outputData[t+7] = (int16_t)upper.w;
  97. }
  98. errorCode = snd_pcm_writei(pcm, outputData.getUnsafe(), samplesPerChannel);
  99. if (errorCode == -EPIPE) {
  100. // Came too late! Not enough written samples to play.
  101. snd_pcm_prepare(pcm);
  102. } else if (errorCode < 0) {
  103. terminateSound();
  104. throwError(U"Failed writing data to PCM. (", snd_strerror(errorCode), U")\n");
  105. }
  106. if (!keepRunning) {
  107. break;
  108. }
  109. }
  110. terminateSound();
  111. return true;
  112. }
  113. }