+class SoundReader
+{
+
+ short int* sound_buffer;
+ int mp3_block_size;
+ int write_pos;
+ int read_pos;
+ IAviReadStream* astream;
+ void readBlock()
+ {
+ unsigned samples_read, bytes_read;
+ int ret;
+ short int tmpbuf[4096];
+ ret = astream->ReadFrames(tmpbuf, 4096*sizeof(short int),
+ 4096, samples_read, bytes_read);
+ if(ret<0) {
+ printf("couldn't read %d samples\n", mp3_block_size);
+ exit(1);
+ }
+ int t;
+ samples_read = bytes_read/sizeof(short int);
+ for(t=0;t<samples_read/2;t++) {
+ sound_buffer[write_pos+t] = tmpbuf[t*2];
+ }
+ write_pos += samples_read/2;
+
+ if(write_pos >= mp3_block_size*8)
+ {
+ if(write_pos > mp3_block_size*8)
+ memcpy(&sound_buffer[0],&sound_buffer[mp3_block_size*8],write_pos - mp3_block_size*8);
+ write_pos %= (mp3_block_size*8);
+ }
+ }
+ public:
+
+ SoundReader(IAviReadStream*astream)
+ {
+ this->astream = astream;
+ this->write_pos = 0;
+ this->read_pos = 0;
+ this->mp3_block_size = 2304;
+ this->sound_buffer = new short int[mp3_block_size*16];
+ }
+ ~SoundReader()
+ {
+ delete sound_buffer;
+ }
+ int available()
+ {
+ if(read_pos<=write_pos)
+ return write_pos-read_pos;
+ else
+ return (write_pos+mp3_block_size*8)-read_pos;
+ }
+ short int* readFrame()
+ {
+ int tmp;
+ while(available()<mp3_block_size) {
+ readBlock();
+ }
+ tmp = read_pos;
+ read_pos += mp3_block_size;
+ read_pos %= mp3_block_size*8;
+ return &sound_buffer[tmp];
+ }
+};
+
+
+static int mp3_block_size = 2304;
+
+static int do_video = 1;
+static int do_audio = 1;
+