1. #include <alsa/asoundlib.h>
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <stdint.h>
  5. #include <vector>
  6. #include <iostream>
  7. snd_pcm_t *pcm_handle;
  8. using std::vector;
  9. typedef struct wavHeader
  10. {
  11. char RIFF[4]; /* RIFF Header */ //Magic header
  12. uint32_t ChunkSize; /* RIFF Chunk Size */
  13. char WAVE[4]; /* WAVE Header */
  14. char fmt[4]; /* FMT header */
  15. uint32_t Subchunk1Size; /* Size of the fmt chunk*/
  16. int16_t AudioFormat; /* Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM */
  17. int16_t NumOfChannels; /* Number of channels 1=Mono 2=Sterio */
  18. uint32_t SamplesPerSec; /* Sampling Frequency in Hz */
  19. uint32_t bytesPerSec; /* bytes per second */
  20. int16_t blockAlign; /* 2=16-bit mono, 4=16-bit stereo */
  21. int16_t bitsPerSample; /* Number of bits per sample */
  22. char Subchunk2ID[4]; /* "data" string */
  23. uint32_t Subchunk2Size; /* Sampled data length */
  24. char data[1];
  25. } wavHeader;
  26. vector<wavHeader*> loaded_sounds;
  27. bool sound_add(const char *filename) {
  28. FILE *file;
  29. if ((file = fopen(filename,"rb")) == NULL)
  30. printf("ERROR: Can't open: %s", filename);
  31. fseek(file, 0, SEEK_END);
  32. size_t flen = ftell(file);
  33. fseek(file, 0, SEEK_SET);
  34. char *wavfile = new char[flen];
  35. fread(wavfile, 1, flen, file);
  36. wavHeader *wavhead = (wavHeader*)wavfile;
  37. loaded_sounds.push_back(wavhead);
  38. return 0;
  39. }
  40. bool sound_play() {
  41. unsigned int pcm;
  42. int rate, channels, seconds;
  43. snd_pcm_t *pcm_handle;
  44. snd_pcm_hw_params_t *params;
  45. /* Open the PCM device in playback mode */
  46. if (pcm = snd_pcm_open(&pcm_handle, "default",SND_PCM_STREAM_PLAYBACK, 0) < 0)
  47. printf("ERROR: Can't open \"%s\" PCM device. %s\n","default", snd_strerror(pcm));
  48. /* Allocate parameters object and fill it with default values*/
  49. snd_pcm_hw_params_alloca(&params);
  50. snd_pcm_hw_params_any(pcm_handle, params);
  51. /* Set parameters */
  52. if (pcm = snd_pcm_hw_params_set_access(pcm_handle, params,SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
  53. printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
  54. if (pcm = snd_pcm_hw_params_set_format(pcm_handle, params,SND_PCM_FORMAT_S16_LE) < 0)
  55. printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));
  56. if (pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, 1) < 0)
  57. printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
  58. snd_pcm_hw_params_set_rate_near(pcm_handle, params, &loaded_sounds[0]->SamplesPerSec, NULL);
  59. /* Write parameters */
  60. if (pcm = snd_pcm_hw_params(pcm_handle, params) < 0)
  61. printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
  62. snd_pcm_writei(pcm_handle,loaded_sounds[0]->data, loaded_sounds[0]->Subchunk2Size / loaded_sounds[0]->bitsPerSample * 8);
  63. snd_pcm_prepare(pcm_handle);
  64. return 0;
  65. }
  66. int main() {
  67. sound_add("/home/greg/test.wav");
  68. sound_play();
  69. return 0;
  70. }