From e2de64d6f1beb9e492daf5b886e19933c1fa41dd Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdemultimedia@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- mpeglib/example/splay/mp3framing.cpp | 274 +++++++++++++++++++++++++++++++++++ 1 file changed, 274 insertions(+) create mode 100644 mpeglib/example/splay/mp3framing.cpp (limited to 'mpeglib/example/splay/mp3framing.cpp') diff --git a/mpeglib/example/splay/mp3framing.cpp b/mpeglib/example/splay/mp3framing.cpp new file mode 100644 index 00000000..586899a3 --- /dev/null +++ b/mpeglib/example/splay/mp3framing.cpp @@ -0,0 +1,274 @@ +/* + + Example how frame creation works. + + For the input we have std. file input. + We convert it to mpeg I encoded frames (mpegAudioFrame's job) + + The decoded pcmFrames are stores in a queue. + + If the queue if full, we "stream" the packets to /dev/dsp. + (The difference between directly playing the packets and "streaming" + is, that while streaming you can say: I want x bytes I don't care + about frame boundaries) + This is audioFrameQueue's job. + + + */ + + +#include "../../lib/splay/mpegAudioFrame.h" +#include "../../lib/splay/splayDecoder.h" +#include "../../lib/util/audio/dspWrapper.h" +#include "../../lib/frame/audioFrameQueue.h" + +#include + +using namespace std; + +// used for getopt +#include + +#define INPUT_SIZE 8192 +#define _QUEUE_FLOAT 1 +#define _QUEUE_INT 2 + + +void usage() { + printf("Usage:\n\n"); + printf("mp3framing [hf] filename\n\n"); + printf(" -f use float queue\n"); + printf(" -c [samples] checkout samples\n"); +} + + +void initDSP(DSPWrapper* dsp) { + if (dsp->openDevice()==false) { + cout << "cannot open /dev/dsp"<= argc ) { + usage(); + exit(-1); + } + FILE* file=fopen(argv[optind],"r"); + if (file == NULL) { + cout << "cannot open:"<dataQueueCanWrite() == true) ) { + + // + // This switch/case statement "partitions" the fileinput + // into mp3frames. We directly decode them to pcmFrames. + // + + // + // There are three states. One is "I want Input, give me input" + // The other is "I have enough input, dont' bother me and let + // me do my work" + // The last ist "I have a frame here, take care of this, or + // I will continue with my work" + + int state=frame->getState(); + switch(state) { + case FRAME_NEED: { + int bytes=frame->canStore(); + int read=fread(inputbuffer,1,bytes,file); + if (read != bytes) { + // read error. reset framer + frame->reset(); + continue; + } + frame->store(inputbuffer,bytes); + break; + } + case FRAME_WORK: + frame->work(); + break; + case FRAME_HAS:{ + AudioFrame* emptyFrame= frameQueue->emptyQueueDequeue(); + splay->decode(frame->outdata(),frame->len(),emptyFrame); + frameQueue->dataQueueEnqueue(emptyFrame); + cout << "storing decodec frame:"<getCurrent(); + dsp->audioSetup(audioFrame); + } + + if (queueType == _QUEUE_FLOAT) { + AudioFrame* audioFrame=frameQueue->getCurrent(); + dsp->audioSetup(audioFrame->getStereo(),16, + audioFrame->getSigned(), + audioFrame->getBigEndian(), + audioFrame->getFrequenceHZ()); + outFrame->setFrameFormat(audioFrame->getStereo(), + audioFrame->getFrequenceHZ()); + } + + + + // + // read from stream demo: + // + + while(frameQueue->getLen() > 0) { + int hasLen= frameQueue->getLen(); + + cout << "hasLen:"<copy(outFrame->getData(),samples); + dsp->audioPlay((char*)outFrame->getData(),hasRead*sizeof(short int)); + frameQueue->forwardStreamSingle(hasRead); + } + + + if (queueType == _QUEUE_FLOAT) { + int n; + + hasRead=frameQueue->copy(left,right,samples); + frameQueue->forwardStreamDouble(hasRead); + n=hasRead; + + /* + FloatFrame* floatFrame=( FloatFrame*) frameQueue->dataQueueDequeue(); + float* left=floatFrame->getData(); + n=floatFrame->getLen(); + */ + + int i=0; + while(i 32767) val=32767; + if (val < -32768) val=-32768; + dsp->audioPlay((char*)&val,2); + + if (outFrame->getStereo()) { + val=(int)(32768.0*right[i]); + if (val > 32767) val=32767; + if (val < -32768) val=-32768; + dsp->audioPlay((char*)&val,2); + } + + i++; + } + + } + } + + + delete frameQueue; + delete splay; + delete frame; + delete dsp; + delete left; + delete right; + delete outFrame; + return(0); +} + -- cgit v1.2.1