playmp3.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (C) 2003-2006 by egnite Software GmbH
  3. * Copyright (C) 2011 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*!
  36. * $Id: playmp3.c 4640 2012-09-24 12:05:56Z u_bonnes $
  37. */
  38. #include <dev/board.h>
  39. #include <dev/vscodec.h>
  40. #include <dev/urom.h>
  41. #include <sys/version.h>
  42. #include <stdio.h>
  43. #include <io.h>
  44. #include <fcntl.h>
  45. #include <errno.h>
  46. #ifndef DEV_FS
  47. #define DEV_FS devUrom
  48. #endif
  49. #ifndef DEV_FS_NAME
  50. #define DEV_FS_NAME "UROM"
  51. #endif
  52. #ifndef DEV_AUDIO_CODEC
  53. #define DEV_AUDIO_CODEC devSpiVsCodec0
  54. #endif
  55. #ifndef DEV_AUDIO_CODEC_NAME
  56. #define DEV_AUDIO_CODEC_NAME "audio0"
  57. #endif
  58. static char mp3buf[512];
  59. static void PlayMp3File(char *path, int gain);
  60. /*!
  61. * \example playmp3/playmp3.c
  62. *
  63. * This sample application plays MP3 files from the UROM filesystem. It
  64. * demonstrates how to use an audio decoder with Nut/OS. It can provide
  65. * a basis for talking equipment, alarm sound output etc.
  66. *
  67. * The code will run out of the box on the Elektor Internet Radio.
  68. *
  69. * To run this example code on Ethernut 1 or 2, you need to attach the
  70. * Medianut Board to the expansion port or use a similar hardware design
  71. * with a hardware based MP3 decoder.
  72. *
  73. * To run on the AT91SAM7X-EK or the AT91SAM9260-EK, you need to attach
  74. * Calypso Board or any similar hardware design providing an audio DAC.
  75. * In addition, you must accept the RealNetworks RPSL/RCSL license by
  76. * enabling this item in the Configurator. Note, that this license is
  77. * different from the BSD license of Nut/OS.
  78. *
  79. * The UROM filesystem is located in program memory. No external file
  80. * storage device is required. Use the crurom utility to create a C source
  81. * file named urom.c from the MP3 files located in subdirectory sounds.
  82. * Here's how to call crurom:
  83. *
  84. * crurom -r -ourom.c sounds
  85. *
  86. * The created file will then be compiled and linked to the application
  87. * code.
  88. */
  89. int main(void)
  90. {
  91. /* Assign stdout to the default console device. */
  92. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  93. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  94. printf("\n\nPlay MP3 files on Nut/OS %s\n", NutVersionString());
  95. /* Initialize the file system driver. */
  96. if (NutRegisterDevice(&DEV_FS, 0, 0)) {
  97. puts("File system not available");
  98. }
  99. /* Initialize the audio decoder. */
  100. if (NutRegisterSpiDevice(&DEV_AUDIO_CODEC, &DEV_SPIBUS, 1)) {
  101. puts("Audio codec not available");
  102. }
  103. /* Play audio files in an endless loop. */
  104. for (;;) {
  105. PlayMp3File(DEV_FS_NAME ":sound1a.mp3", -40);
  106. PlayMp3File(DEV_FS_NAME ":sound2a.mp3", -30);
  107. PlayMp3File(DEV_FS_NAME ":sound3a.mp3", -20);
  108. PlayMp3File(DEV_FS_NAME ":sound4a.mp3", -10);
  109. }
  110. return 0;
  111. }
  112. /*
  113. * Play audio file with specified volume.
  114. */
  115. static void PlayMp3File(char *path, int gain)
  116. {
  117. int fh;
  118. int dh;
  119. int got;
  120. printf("Play %s on %s: ", path, DEV_AUDIO_CODEC_NAME);
  121. /* Open audio codec. */
  122. dh = _open(DEV_AUDIO_CODEC_NAME, _O_WRONLY | _O_BINARY);
  123. if (dh == -1) {
  124. printf("Error %d, can't open audio codec\n", errno);
  125. return;
  126. }
  127. /* Set volume. */
  128. _ioctl(dh, AUDIO_SET_PLAYGAIN, &gain);
  129. /* Open audio file. */
  130. fh = _open(path, _O_RDONLY | _O_BINARY);
  131. if (fh == -1) {
  132. printf("Error %d, can't open audio file\n", errno);
  133. return;
  134. } else {
  135. /* Transfer audio data from file to codec. */
  136. for (;;) {
  137. got = _read(fh, mp3buf, sizeof(mp3buf));
  138. if (got <= 0) {
  139. break;
  140. }
  141. _write(dh, mp3buf, got);
  142. }
  143. /* Close audio file. */
  144. _close(fh);
  145. }
  146. /* Close audio codec. Will not return until all buffered data has
  147. been played. */
  148. _close(dh);
  149. puts("Done");
  150. return;
  151. }