MusicPlayer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. #include <stdlib.h>
  5. #include <memory.h>
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <assert.h>
  9. #include "MusicPlayer.h"
  10. /**
  11. * Simple music player which plays either MP3 (mpg123) or OGG (stb_vorbis)
  12. */
  13. MusicPlayer::MusicPlayer()
  14. {
  15. // playing = false;
  16. rate = AUDIO_FREQUENCY;
  17. channels = 2;
  18. error = 0;
  19. #ifdef USE_MP3
  20. error = mpg123_init();
  21. mh = mpg123_new(NULL, &error);
  22. #endif
  23. #ifdef USE_OGG
  24. v=0;
  25. #endif
  26. }
  27. MusicPlayer::~MusicPlayer()
  28. {
  29. #ifdef USE_OGG
  30. stb_vorbis_close(v);
  31. #endif
  32. #ifdef USE_MP3
  33. mpg123_close(mh);
  34. mpg123_delete(mh);
  35. mpg123_exit();
  36. #endif
  37. }
  38. void MusicPlayer::play(const char *filename)
  39. {
  40. #ifdef USE_OGG
  41. stb_vorbis_close(v);
  42. stb_vorbis_alloc t;
  43. t.alloc_buffer=this->buf;
  44. t.alloc_buffer_length_in_bytes=STB_BUFSIZE;
  45. // stb_vorbis has char* pointer even though it is not really writing to buffer
  46. v = stb_vorbis_open_filename((char *)filename, &error, &t);
  47. #endif
  48. #ifdef USE_MP3
  49. mpg123_close(mh);
  50. error = mpg123_open(mh, filename);
  51. if (!error)
  52. {
  53. error = mpg123_getformat(mh, &rate, &channels, &encoding);
  54. rate = AUDIO_FREQUENCY;
  55. channels = 2;
  56. mpg123_format_none(mh);
  57. mpg123_format(mh, rate, channels, encoding);
  58. }
  59. #endif
  60. }
  61. void MusicPlayer::play(const char *mem, int length )
  62. {
  63. #ifdef USE_OGG
  64. stb_vorbis_close(v);
  65. stb_vorbis_alloc t;
  66. t.alloc_buffer=this->buf;
  67. t.alloc_buffer_length_in_bytes=STB_BUFSIZE;
  68. // stb_vorbis has char* pointer even though it is not really writing to buffer
  69. v = stb_vorbis_open_memory( (unsigned char*)mem, length, &error, &t);
  70. #endif
  71. #ifdef USE_MP3
  72. #warning MusicPlayer::play(const char *mem, int length ) not implemented
  73. #endif
  74. }
  75. int MusicPlayer::pullAudio(AUDIO_SAMPLE_TYPE *target, int sampleCount)
  76. {
  77. memset(target, 0, sampleCount * sizeof(AUDIO_SAMPLE_TYPE));
  78. #ifdef USE_OGG
  79. if(v && error==0)
  80. {
  81. int samples = stb_vorbis_get_samples_short_interleaved(v, channels, target, sampleCount);
  82. if(samples==0)
  83. {
  84. stb_vorbis_seek_start(v);
  85. stb_vorbis_get_samples_short_interleaved(v, channels, target, sampleCount);
  86. // error=-1;
  87. }
  88. }
  89. #endif
  90. #ifdef USE_MP3
  91. if (!error)
  92. {
  93. size_t done;
  94. error = mpg123_read(mh, (unsigned char *) target, sampleCount
  95. * sizeof(AUDIO_SAMPLE_TYPE), &done);
  96. // read will return MPG123_DONE eventually...
  97. if (error == MPG123_DONE)
  98. {
  99. mpg123_seek(mh, 0, SEEK_SET);
  100. error = mpg123_read(mh, (unsigned char *) target, sampleCount
  101. * sizeof(AUDIO_SAMPLE_TYPE), &done);
  102. }
  103. // ignore format change
  104. if (error == MPG123_NEW_FORMAT)
  105. error = MPG123_OK;
  106. }
  107. #endif
  108. return sampleCount;
  109. }