void FeedReaders ( struct AHIDevUnit *iounit, struct AHIBase *AHIBase ) { struct AHIRequest *ioreq; ObtainSemaphore(&iounit->ListLock); for(ioreq = (struct AHIRequest *)iounit->ReadList.mlh_Head; ioreq->ahir_Std.io_Message.mn_Node.ln_Succ; ioreq = (struct AHIRequest *)ioreq->ahir_Std.io_Message.mn_Node.ln_Succ) { FillReadBuffer(ioreq, iounit, AHIBase); } // Check if Reader-list is empty. If so, stop recording (after a small delay). if( ! iounit->ReadList.mlh_Head->mln_Succ ) { if(--iounit->RecordOffDelay == 0) { AHI_ControlAudio(iounit->AudioCtrl, AHIC_Record,FALSE, TAG_DONE); iounit->IsRecording = FALSE; } } else { iounit->RecordOffDelay = 2; } ReleaseSemaphore(&iounit->ListLock); }
int main(int argc, char **argv) { int bytesLeft, nRead, err, offset, outOfData, eofReached; unsigned char readBuf[READBUF_SIZE], *readPtr; short outBuf[MAX_NCHAN * MAX_NGRAN * MAX_NSAMP]; FILE *infile, *outfile; MP3FrameInfo mp3FrameInfo; HMP3Decoder hMP3Decoder; int startTime, endTime, diffTime, totalDecTime, nFrames; #ifdef ARM_ADS float audioSecs; #endif if (argc != 3) { printf("usage: mp3dec infile.mp3 outfile.pcm\n"); return -1; } infile = fopen(argv[1], "rb"); if (!infile) { printf("file open error\n"); return -1; } if (strcmp(argv[2], "nul")) { outfile = fopen(argv[2], "wb"); if (!outfile) { printf("file open error\n"); return -1; } } else { outfile = 0; /* nul output */ } DebugMemCheckInit(); InitTimer(); DebugMemCheckStartPoint(); if ( (hMP3Decoder = MP3InitDecoder()) == 0 ) return -2; DebugMemCheckEndPoint(); bytesLeft = 0; outOfData = 0; eofReached = 0; readPtr = readBuf; nRead = 0; totalDecTime = 0; nFrames = 0; do { /* somewhat arbitrary trigger to refill buffer - should always be enough for a full frame */ if (bytesLeft < 2*MAINBUF_SIZE && !eofReached) { nRead = FillReadBuffer(readBuf, readPtr, READBUF_SIZE, bytesLeft, infile); bytesLeft += nRead; readPtr = readBuf; if (nRead == 0) eofReached = 1; } /* find start of next MP3 frame - assume EOF if no sync found */ offset = MP3FindSyncWord(readPtr, bytesLeft); if (offset < 0) { outOfData = 1; break; } readPtr += offset; bytesLeft -= offset; /* decode one MP3 frame - if offset < 0 then bytesLeft was less than a full frame */ startTime = ReadTimer(); err = MP3Decode(hMP3Decoder, &readPtr, &bytesLeft, outBuf, 0); nFrames++; endTime = ReadTimer(); diffTime = CalcTimeDifference(startTime, endTime); totalDecTime += diffTime; #if defined ARM_ADS && defined MAX_ARM_FRAMES printf("frame %5d start = %10d, end = %10d elapsed = %10d ticks\r", nFrames, startTime, endTime, diffTime); fflush(stdout); #endif if (err) { /* error occurred */ switch (err) { case ERR_MP3_INDATA_UNDERFLOW: outOfData = 1; break; case ERR_MP3_MAINDATA_UNDERFLOW: /* do nothing - next call to decode will provide more mainData */ break; case ERR_MP3_FREE_BITRATE_SYNC: default: outOfData = 1; break; } } else { /* no error */ MP3GetLastFrameInfo(hMP3Decoder, &mp3FrameInfo); if (outfile) fwrite(outBuf, mp3FrameInfo.bitsPerSample / 8, mp3FrameInfo.outputSamps, outfile); } #if defined ARM_ADS && defined MAX_ARM_FRAMES if (nFrames >= MAX_ARM_FRAMES) break; #endif } while (!outOfData); #ifdef ARM_ADS MP3GetLastFrameInfo(hMP3Decoder, &mp3FrameInfo); audioSecs = ((float)nFrames * mp3FrameInfo.outputSamps) / ( (float)mp3FrameInfo.samprate * mp3FrameInfo.nChans); printf("\nTotal clock ticks = %d, MHz usage = %.2f\n", totalDecTime, ARMULATE_MUL_FACT * (1.0f / audioSecs) * totalDecTime * GetClockDivFactor() / 1e6f); printf("nFrames = %d, output samps = %d, sampRate = %d, nChans = %d\n", nFrames, mp3FrameInfo.outputSamps, mp3FrameInfo.samprate, mp3FrameInfo.nChans); #endif MP3FreeDecoder(hMP3Decoder); fclose(infile); if (outfile) fclose(outfile); FreeTimer(); DebugMemCheckFree(); return 0; }
static void ReadCmd ( struct AHIRequest *ioreq, struct AHIBase *AHIBase ) { struct AHIDevUnit *iounit; ULONG error,mixfreq = 0; if(AHIBase->ahib_DebugLevel >= AHI_DEBUG_HIGH) { KPrintF("CMD_READ\n"); } iounit = (struct AHIDevUnit *) ioreq->ahir_Std.io_Unit; /* Start recording if neccessary */ if( ! iounit->IsRecording) { if( (! iounit->FullDuplex) && iounit->IsPlaying) { error = AHIE_HALFDUPLEX; // FIXIT! } else { error = AHI_ControlAudio(iounit->AudioCtrl, AHIC_Record,TRUE, TAG_DONE); } if( ! error) { iounit->IsRecording = TRUE; } } if(iounit->IsRecording) { AHI_ControlAudio(iounit->AudioCtrl, AHIC_MixFreq_Query, (ULONG) &mixfreq, TAG_DONE); ioreq->ahir_Std.io_Actual = 0; /* Initialize ahir_Frequency for the assembler record routines */ if(ioreq->ahir_Frequency && mixfreq) ioreq->ahir_Frequency = ((mixfreq << 15) / ioreq->ahir_Frequency) << 1; else ioreq->ahir_Frequency = 0x00010000; // Fixed 1.0 ObtainSemaphore(&iounit->ListLock); /* Add the request to the list of readers */ AddTail((struct List *) &iounit->ReadList,(struct Node *) ioreq); /* Copy the current buffer contents */ FillReadBuffer(ioreq, iounit, AHIBase); ReleaseSemaphore(&iounit->ListLock); } else { ioreq->ahir_Std.io_Error = error; TermIO(ioreq, AHIBase); } }