THREAD(FallingAsleep, arg) { printf("falling asleep started\n"); for(;;) { int volumeGoal = 254; //Dont start yet if no sound is playing while(VsGetStatus() != VS_STATUS_RUNNING) NutSleep(50); //Get the amount of seconds from the fallingAsleepTime minutes long totalSeconds = (long)fallingAsleepTime * 1000 * 60; printf("totalseconds = %lu\n", totalSeconds); //Get the time to sleep before "increasing" the volume. Divide the totalSeconds by the volumeGoal to get a smooth transition long sleepTime = totalSeconds / volumeGoal; printf("sleeptime = %lu\n", sleepTime); int currentVolume = VsGetVolume(); //"Increase" the volume until it hits the goal while(currentVolume < volumeGoal) { NutSleep(sleepTime); VsSetVolume(currentVolume + 1, currentVolume + 1); printf("Falling Asleep Mode tick now. Volume lowered %d", VsGetVolume()); currentVolume++; } //Call stopStream in network.c stopStream(); //Turn off falling asleep mode fallingAsleepMode = 0; //Reset volume VsSetVolume(0, 0); //Exit the thread NutThreadExit(); } }
/* * \brief Play MP3 stream. * * \param stream Socket stream to read MP3 data from. */ void PlayMp3Stream(FILE *stream, u_long metaint) { size_t rbytes; u_char *mp3buf; u_char ief; int got = 0; u_long last; u_long mp3left = metaint; /* * Initialize the MP3 buffer. The NutSegBuf routines provide a global * system buffer, which works with banked and non-banked systems. */ if (NutSegBufInit(8192) == 0) { puts("Error: MP3 buffer init failed"); return; } /* * Initialize the MP3 decoder hardware. */ if (VsPlayerInit() || VsPlayerReset(0)) { puts("Error: MP3 hardware init failed"); return; } VsSetVolume(0, 0); /* * Reset the MP3 buffer. */ ief = VsPlayerInterrupts(0); NutSegBufReset(); VsPlayerInterrupts(ief); last = NutGetSeconds(); for (; ;) { if(STOP_THREAD){ STOP_THREAD = 0; return; } /* * Query number of byte available in MP3 buffer. */ ief = VsPlayerInterrupts(0); mp3buf = NutSegBufWriteRequest(&rbytes); VsPlayerInterrupts(ief); /* * If the player is not running, kick it. */ if (VsGetStatus() != VS_STATUS_RUNNING) { if (rbytes < 1024 || NutGetSeconds() - last > 4UL) { last = NutGetSeconds(); puts("Kick player"); VsPlayerKick(); } } /* * Do not read pass metadata. */ if (metaint && rbytes > mp3left) { rbytes = mp3left; } /* * Read data directly into the MP3 buffer. */ while (rbytes) { if ((got = fread(mp3buf, 1, rbytes, stream)) > 0) { ief = VsPlayerInterrupts(0); mp3buf = NutSegBufWriteCommit(got); VsPlayerInterrupts(ief); if (metaint) { mp3left -= got; if (mp3left == 0) { ProcessMetaData(stream); mp3left = metaint; } } if (got < rbytes && got < 512) { printf("%lu buffered\n", NutSegBufUsed()); NutSleep(250); } else { NutThreadYield(); } } else { break; } rbytes -= got; } if (got <= 0) { break; } NutSleep(100); } }
THREAD(StreamPlayer, arg) { FILE *stream = (FILE *)arg; size_t rbytes = 0; char *mp3buf; int result = NOK; int nrBytesRead = 0; unsigned char iflag; // // Init MP3 buffer. NutSegBuf is een globale, systeem buffer // if (0 != NutSegBufInit(8192)) { // Reset global buffer iflag = VsPlayerInterrupts(0); NutSegBufReset(); VsPlayerInterrupts(iflag); result = OK; } // Init the Vs1003b hardware if (OK == result) { if (-1 == VsPlayerInit()) { if (-1 == VsPlayerReset(0)) { result = NOK; } } } for (;;) { /* * Query number of byte available in MP3 buffer. */ iflag = VsPlayerInterrupts(0); mp3buf = NutSegBufWriteRequest(&rbytes); VsPlayerInterrupts(iflag); // Bij de eerste keer: als player niet draait maak player wakker (kickit) if (VS_STATUS_RUNNING != VsGetStatus()) { if (rbytes < 1024) { printf("VsPlayerKick()\n"); VsPlayerKick(); } } while (rbytes) { // Copy rbytes (van 1 byte) van stream naar mp3buf. nrBytesRead = fread(mp3buf, 1, rbytes, stream); if (nrBytesRead > 0) { iflag = VsPlayerInterrupts(0); mp3buf = NutSegBufWriteCommit(nrBytesRead); VsPlayerInterrupts(iflag); if (nrBytesRead < rbytes && nrBytesRead < 512) { NutSleep(250); } } else { break; } rbytes -= nrBytesRead; if (nrBytesRead <= 0) { break; } } } }