playmp3.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00059 #include <dev/board.h>
00060 #include <dev/vs1001k.h>
00061 #include <dev/debug.h>
00062 #include <dev/urom.h>
00063
00064 #include <sys/version.h>
00065 #include <sys/heap.h>
00066 #include <sys/event.h>
00067 #include <sys/timer.h>
00068 #include <sys/thread.h>
00069 #include <sys/bankmem.h>
00070
00071 #include <stdlib.h>
00072 #include <string.h>
00073 #include <stdio.h>
00074 #include <io.h>
00075 #include <fcntl.h>
00076 #include <errno.h>
00077
00078 #if defined(__AVR__)
00079 static int PlayMp3File(char *path);
00080 #endif
00081
00105 int main(void)
00106 {
00107
00108 u_long baud = 115200;
00109
00110
00111
00112
00113 NutRegisterDevice(&devUrom, 0, 0);
00114 NutRegisterDevice(&DEV_DEBUG, 0, 0);
00115
00116
00117
00118
00119 freopen(DEV_DEBUG_NAME, "w", stdout);
00120 _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00121
00122
00123
00124
00125 printf("\n\nPlay MP3 files on Nut/OS %s\n", NutVersionString());
00126
00127 #if defined(__AVR__)
00128
00129
00130
00131
00132
00133 if (NutSegBufInit(8192) == 0) {
00134 puts("NutSegBufInit: Fatal error");
00135 }
00136
00137
00138
00139
00140 if (VsPlayerInit() || VsPlayerReset(0)) {
00141 puts("VsPlayer: Fatal error");
00142 }
00143
00144
00145
00146
00147
00148
00149 for (;;) {
00150 VsSetVolume(0, 254);
00151 PlayMp3File("UROM:sound1a.mp3");
00152 NutSleep(1000);
00153
00154 VsSetVolume(0, 0);
00155 PlayMp3File("UROM:sound2a.mp3");
00156 NutSleep(1000);
00157
00158 VsSetVolume(254, 0);
00159 PlayMp3File("UROM:sound3a.mp3");
00160 NutSleep(1000);
00161
00162 VsSetVolume(0, 0);
00163 PlayMp3File("UROM:sound4a.mp3");
00164 NutSleep(1000);
00165 }
00166 #else
00167 for (;;);
00168 #endif
00169 return 0;
00170 }
00171
00172 #if defined(__AVR__)
00173
00174
00175
00176
00177
00178
00179
00180
00181 static int PlayMp3File(char *path)
00182 {
00183 int fd;
00184 size_t rbytes;
00185 char *mp3buf;
00186 int got;
00187 u_char ief;
00188
00189
00190
00191
00192 printf("Play %s: ", path);
00193 if ((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
00194 printf("Error %d\n", errno);
00195 return -1;
00196 }
00197 puts("OK");
00198
00199
00200
00201
00202 printf("[B.RST]");
00203 ief = VsPlayerInterrupts(0);
00204 NutSegBufReset();
00205 VsPlayerInterrupts(ief);
00206
00207 for (;;) {
00208
00209
00210
00211 ief = VsPlayerInterrupts(0);
00212 mp3buf = NutSegBufWriteRequest(&rbytes);
00213 VsPlayerInterrupts(ief);
00214
00215
00216
00217
00218 if (rbytes) {
00219 printf("[B.RD%d]", rbytes);
00220 if ((got = _read(fd, mp3buf, rbytes)) > 0) {
00221 printf("[B.CMT%d]", got);
00222 ief = VsPlayerInterrupts(0);
00223 mp3buf = NutSegBufWriteCommit(got);
00224 VsPlayerInterrupts(ief);
00225 } else {
00226 printf("[EOF]");
00227 break;
00228 }
00229 }
00230
00231
00232
00233
00234 if (VsGetStatus() != VS_STATUS_RUNNING) {
00235 printf("[P.KICK]");
00236 VsPlayerKick();
00237 }
00238
00239
00240
00241
00242 NutThreadYield();
00243 }
00244
00245 _close(fd);
00246
00247
00248
00249
00250 printf("[P.FLUSH]");
00251 VsPlayerFlush();
00252 while (VsGetStatus() != VS_STATUS_EMPTY) {
00253 NutSleep(1);
00254 }
00255
00256
00257
00258
00259 printf("[P.RST]");
00260 VsPlayerReset(0);
00261
00262 printf("\nDone, %u bytes free\n", NutHeapAvailable());
00263 return 0;
00264 }
00265
00266 #endif