From ecf7f30fb49bf2e6ea60781a24562da7b70e1358 Mon Sep 17 00:00:00 2001 From: vspader Date: Sun, 10 Feb 2008 16:18:23 +0000 Subject: [PATCH] Updated Musepack plugin to use new decoder protocol. --- .../Musepack.xcodeproj/project.pbxproj | 14 +-- Plugins/Musepack/MusepackDecoder.h | 6 +- Plugins/Musepack/MusepackDecoder.m | 100 +++++++++--------- 3 files changed, 61 insertions(+), 59 deletions(-) diff --git a/Plugins/Musepack/Musepack.xcodeproj/project.pbxproj b/Plugins/Musepack/Musepack.xcodeproj/project.pbxproj index 944fe7543..1fe6207bf 100644 --- a/Plugins/Musepack/Musepack.xcodeproj/project.pbxproj +++ b/Plugins/Musepack/Musepack.xcodeproj/project.pbxproj @@ -8,8 +8,8 @@ /* Begin PBXBuildFile section */ 1703330D0B8FB64500327265 /* MusepackDecoder.m in Sources */ = {isa = PBXBuildFile; fileRef = 1703330A0B8FB64500327265 /* MusepackDecoder.m */; }; - 17F564B40C3BDD970019975C /* MPCDec.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 17F5625F0C3BD97C0019975C /* MPCDec.framework */; }; - 17F564B70C3BDD990019975C /* MPCDec.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 17F5625F0C3BD97C0019975C /* MPCDec.framework */; }; + 17F564B40C3BDD970019975C /* mpcdec.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 17F5625F0C3BD97C0019975C /* mpcdec.framework */; }; + 17F564B70C3BDD990019975C /* mpcdec.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 17F5625F0C3BD97C0019975C /* mpcdec.framework */; }; 8D5B49B4048680CD000E48DA /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7ADFEA557BF11CA2CBB /* Cocoa.framework */; }; 8E2B8B4B0B9B48D000F2D9E8 /* Plugin.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 8E2B8B4A0B9B48D000F2D9E8 /* Plugin.h */; }; /* End PBXBuildFile section */ @@ -38,7 +38,7 @@ dstPath = ""; dstSubfolderSpec = 10; files = ( - 17F564B70C3BDD990019975C /* MPCDec.framework in CopyFiles */, + 17F564B70C3BDD990019975C /* mpcdec.framework in CopyFiles */, 8E2B8B4B0B9B48D000F2D9E8 /* Plugin.h in CopyFiles */, ); runOnlyForDeploymentPostprocessing = 0; @@ -64,7 +64,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 17F564B40C3BDD970019975C /* MPCDec.framework in Frameworks */, + 17F564B40C3BDD970019975C /* mpcdec.framework in Frameworks */, 8D5B49B4048680CD000E48DA /* Cocoa.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -133,7 +133,7 @@ 17F562580C3BD97B0019975C /* Products */ = { isa = PBXGroup; children = ( - 17F5625F0C3BD97C0019975C /* MPCDec.framework */, + 17F5625F0C3BD97C0019975C /* mpcdec.framework */, ); name = Products; sourceTree = ""; @@ -201,10 +201,10 @@ /* End PBXProject section */ /* Begin PBXReferenceProxy section */ - 17F5625F0C3BD97C0019975C /* MPCDec.framework */ = { + 17F5625F0C3BD97C0019975C /* mpcdec.framework */ = { isa = PBXReferenceProxy; fileType = wrapper.framework; - path = MPCDec.framework; + path = mpcdec.framework; remoteRef = 17F5625E0C3BD97C0019975C /* PBXContainerItemProxy */; sourceTree = BUILT_PRODUCTS_DIR; }; diff --git a/Plugins/Musepack/MusepackDecoder.h b/Plugins/Musepack/MusepackDecoder.h index daace8914..ce29e5d6c 100644 --- a/Plugins/Musepack/MusepackDecoder.h +++ b/Plugins/Musepack/MusepackDecoder.h @@ -18,13 +18,13 @@ mpc_reader reader; char buffer[MPC_FRAME_LENGTH*4]; - int bufferAmount; + int bufferFrames; int bitrate; float frequency; - double length; + long totalFrames; } -- (BOOL)writeSamplesToBuffer:(uint16_t *)sample_buffer fromBuffer:(const MPC_SAMPLE_FORMAT *)p_buffer ofSize:(unsigned)p_size; +- (BOOL)writeToBuffer:(uint16_t *)sample_buffer fromBuffer:(const MPC_SAMPLE_FORMAT *)p_buffer frames:(unsigned)frames; - (void)setSource:(id)s; - (id)source; diff --git a/Plugins/Musepack/MusepackDecoder.m b/Plugins/Musepack/MusepackDecoder.m index 1c75e3c81..8c971743f 100644 --- a/Plugins/Musepack/MusepackDecoder.m +++ b/Plugins/Musepack/MusepackDecoder.m @@ -87,7 +87,7 @@ mpc_bool_t CanSeekProc(void *data) bitrate = (int)(info.average_bitrate/1000.0); frequency = info.sample_freq; - length = ((double)mpc_streaminfo_get_length_samples(&info)*1000.0)/frequency; + totalFrames = mpc_streaminfo_get_length_samples(&info); [self willChangeValueForKey:@"properties"]; [self didChangeValueForKey:@"properties"]; @@ -95,13 +95,15 @@ mpc_bool_t CanSeekProc(void *data) return YES; } -- (BOOL)writeSamplesToBuffer:(uint16_t *)sample_buffer fromBuffer:(const MPC_SAMPLE_FORMAT *)p_buffer ofSize:(unsigned)p_size +- (BOOL)writeToBuffer:(uint16_t *)sample_buffer fromBuffer:(const MPC_SAMPLE_FORMAT *)p_buffer frames:(unsigned)frames { unsigned n; int m_bps = 16; int clip_min = - 1 << (m_bps - 1), clip_max = (1 << (m_bps - 1)) - 1, float_scale = 1 << (m_bps - 1); + + unsigned p_size = frames * 2; //2 = bits per sample for (n = 0; n < p_size; n++) { @@ -125,56 +127,56 @@ mpc_bool_t CanSeekProc(void *data) return YES; } -- (int)fillBuffer:(void *)buf ofSize:(UInt32)size +- (int)readAudio:(void *)buf frames:(UInt32)frames { - int numread = bufferAmount; - int count = 0; MPC_SAMPLE_FORMAT sampleBuffer[MPC_DECODER_BUFFER_LENGTH]; - //Fill from buffer, going by bufferAmount - //if still needs more, decode and repeat - if (bufferAmount == 0) - { - /* returns the length of the samples*/ - - unsigned status = mpc_decoder_decode(&decoder, sampleBuffer, 0, 0); - if (status == (unsigned)( -1)) + int framesRead = 0; + int bytesPerFrame = 4; //bitsPerSample == 16, channels == 2 + while (framesRead < frames) + { + //Fill from buffer, going by bufferFrames + //if still needs more, decode and repeat + if (bufferFrames == 0) { - //decode error - NSLog(@"Decode error"); - return 0; - } - else if (status == 0) //EOF - { - return 0; - } - else //status>0 /* status == MPC_FRAME_LENGTH */ - { - [self writeSamplesToBuffer:((uint16_t*)buffer) fromBuffer:sampleBuffer ofSize:(status*2)]; - } + /* returns the length of the samples*/ + unsigned status = mpc_decoder_decode(&decoder, sampleBuffer, 0, 0); + if (status == (unsigned)( -1)) + { + //decode error + NSLog(@"Decode error"); + return 0; + } + else if (status == 0) //EOF + { + return 0; + } + else //status>0 /* status == MPC_FRAME_LENGTH */ + { + } - bufferAmount = status*4; + bufferFrames = status; + } + + int framesToRead = bufferFrames; + if (bufferFrames > frames) + { + framesToRead = frames; + } + + [self writeToBuffer:((uint16_t*)(buf + (framesRead*bytesPerFrame))) fromBuffer:sampleBuffer frames: bufferFrames]; + + frames -= framesToRead; + framesRead += framesToRead; + bufferFrames -= framesToRead; + + if (bufferFrames > 0) + { + memmove((uint8_t *)sampleBuffer, ((uint8_t *)sampleBuffer) + (framesToRead * bytesPerFrame), bufferFrames * bytesPerFrame); + } } - count = bufferAmount; - if (bufferAmount > size) - { - count = size; - } - - memcpy(buf, buffer, count); - - bufferAmount -= count; - - if (bufferAmount > 0) - memmove(buffer, &buffer[count], bufferAmount); - - if (count < size) - numread = [self fillBuffer:(&((char *)buf)[count]) ofSize:(size - count)]; - else - numread = 0; - - return count + numread; + return framesRead; } - (void)close @@ -182,11 +184,11 @@ mpc_bool_t CanSeekProc(void *data) [source close]; } -- (double)seekToTime:(double)milliseconds +- (long)seek:(long)sample { - mpc_decoder_seek_sample(&decoder, frequency*((double)milliseconds/1000.0)); + mpc_decoder_seek_sample(&decoder, sample); - return milliseconds; + return sample; } - (void)setSource:(id)s @@ -206,7 +208,7 @@ mpc_bool_t CanSeekProc(void *data) return [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:bitrate], @"bitrate", [NSNumber numberWithFloat:frequency], @"sampleRate", - [NSNumber numberWithDouble:length], @"length", + [NSNumber numberWithDouble:totalFrames], @"totalFrames", [NSNumber numberWithInt:16], @"bitsPerSample", [NSNumber numberWithInt:2], @"channels", [NSNumber numberWithBool:[source seekable]], @"seekable",