ALSAAudioSink.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "ALSAAudioSink.h"
  2. ALSAAudioSink::ALSAAudioSink() : Task("", 0, 0, 0) {
  3. /* Open the PCM device in playback mode */
  4. if (pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0) <
  5. 0) {
  6. printf("ERROR: Can't open \"%s\" PCM device. %s\n", PCM_DEVICE,
  7. snd_strerror(pcm));
  8. }
  9. /* Allocate parameters object and fill it with default values*/
  10. snd_pcm_hw_params_alloca(&params);
  11. snd_pcm_hw_params_any(pcm_handle, params);
  12. /* Set parameters */
  13. if (pcm = snd_pcm_hw_params_set_access(pcm_handle, params,
  14. SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
  15. printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
  16. if (pcm = snd_pcm_hw_params_set_format(pcm_handle, params,
  17. SND_PCM_FORMAT_S16_LE) < 0)
  18. printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));
  19. if (pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, 2) < 0)
  20. printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
  21. unsigned int rate = 44100;
  22. if (pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0)
  23. printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));
  24. unsigned int periodTime = 800;
  25. int dir = -1;
  26. snd_pcm_hw_params_set_period_time_near(pcm_handle, params, &periodTime, &dir);
  27. /* Write parameters */
  28. if (pcm = snd_pcm_hw_params(pcm_handle, params) < 0)
  29. printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
  30. /* Resume information */
  31. printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));
  32. printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));
  33. unsigned int tmp;
  34. snd_pcm_hw_params_get_channels(params, &tmp);
  35. printf("channels: %i ", tmp);
  36. if (tmp == 1)
  37. printf("(mono)\n");
  38. else if (tmp == 2)
  39. printf("(stereo)\n");
  40. snd_pcm_hw_params_get_period_time(params, &tmp, NULL);
  41. printf("period_time = %d\n", tmp);
  42. snd_pcm_hw_params_get_period_size(params, &frames, 0);
  43. this->buff_size = frames * 2 * 2 /* 2 -> sample size */;
  44. printf("required buff_size: %d\n", buff_size);
  45. this->startTask();
  46. }
  47. ALSAAudioSink::~ALSAAudioSink() {
  48. snd_pcm_drain(pcm_handle);
  49. snd_pcm_close(pcm_handle);
  50. }
  51. void ALSAAudioSink::runTask() {
  52. std::unique_ptr<std::vector<uint8_t>> dataPtr;
  53. while (true) {
  54. if (!this->ringbuffer.pop(dataPtr)) {
  55. usleep(100);
  56. continue;
  57. }
  58. if (pcm = snd_pcm_writei(pcm_handle, dataPtr->data(), this->frames) ==
  59. -EPIPE) {
  60. snd_pcm_prepare(pcm_handle);
  61. } else if (pcm < 0) {
  62. printf("ERROR. Can't write to PCM device. %s\n", snd_strerror(pcm));
  63. }
  64. }
  65. }
  66. void ALSAAudioSink::feedPCMFrames(const uint8_t* buffer, size_t bytes) {
  67. buff.insert(buff.end(), buffer, buffer + bytes);
  68. while (buff.size() > this->buff_size) {
  69. auto ptr = std::make_unique<std::vector<uint8_t>>(
  70. this->buff.begin(), this->buff.begin() + this->buff_size);
  71. this->buff = std::vector<uint8_t>(this->buff.begin() + this->buff_size,
  72. this->buff.end());
  73. while (!this->ringbuffer.push(ptr)) {
  74. usleep(100);
  75. };
  76. }
  77. }