From 130a17cbf756cf918ee7fa77fdeb0a4c3c6aa909 Mon Sep 17 00:00:00 2001 From: vspader Date: Sun, 4 Mar 2007 20:47:49 +0000 Subject: [PATCH] Moved WavPack and MusePack over to the source system. --- .../Musepack.xcodeproj/project.pbxproj | 2 - Plugins/Musepack/MusepackDecoder.h | 4 +- Plugins/Musepack/MusepackDecoder.m | 25 ++- .../WavPack/WavPack.xcodeproj/project.pbxproj | 24 ++- Plugins/WavPack/WavPackDecoder.h | 6 + Plugins/WavPack/WavPackDecoder.m | 188 +++++++++++++++++- Plugins/WavPack/WavPackPlugin.m | 2 +- Plugins/WavPack/WavPackPropertiesReader.m | 4 +- 8 files changed, 224 insertions(+), 31 deletions(-) diff --git a/Plugins/Musepack/Musepack.xcodeproj/project.pbxproj b/Plugins/Musepack/Musepack.xcodeproj/project.pbxproj index 89d2b8cb3..e4a8b4cfc 100644 --- a/Plugins/Musepack/Musepack.xcodeproj/project.pbxproj +++ b/Plugins/Musepack/Musepack.xcodeproj/project.pbxproj @@ -242,8 +242,6 @@ "$(FRAMEWORK_SEARCH_PATHS_QUOTED_2)", "$(FRAMEWORK_SEARCH_PATHS_QUOTED_3)", ); - FRAMEWORK_SEARCH_PATHS_QUOTED_1 = "\"$(SRCROOT)/../../../MPCDec/build/Release\""; - FRAMEWORK_SEARCH_PATHS_QUOTED_2 = "\"$(SRCROOT)/../../../Frameworks/MPCDec/build/Release\""; FRAMEWORK_SEARCH_PATHS_QUOTED_3 = "\"$(SRCROOT)/../../Frameworks/MPCDec/build/Release\""; GCC_GENERATE_DEBUGGING_SYMBOLS = NO; GCC_MODEL_TUNING = G5; diff --git a/Plugins/Musepack/MusepackDecoder.h b/Plugins/Musepack/MusepackDecoder.h index 068a5e577..3ca4cf597 100644 --- a/Plugins/Musepack/MusepackDecoder.h +++ b/Plugins/Musepack/MusepackDecoder.h @@ -14,7 +14,9 @@ { id source; mpc_decoder decoder; - + mpc_streaminfo info; + mpc_reader reader; + char buffer[MPC_FRAME_LENGTH*4]; int bufferAmount; diff --git a/Plugins/Musepack/MusepackDecoder.m b/Plugins/Musepack/MusepackDecoder.m index e5340017d..dc2aa3be8 100644 --- a/Plugins/Musepack/MusepackDecoder.m +++ b/Plugins/Musepack/MusepackDecoder.m @@ -12,6 +12,7 @@ mpc_int32_t ReadProc(void *data, void *ptr, mpc_int32_t size) { + NSLog(@"MPC Read: %i", size); MusepackDecoder *decoder = (MusepackDecoder *) data; return [[decoder source] read:ptr amount:size]; @@ -20,12 +21,13 @@ mpc_int32_t ReadProc(void *data, void *ptr, mpc_int32_t size) mpc_bool_t SeekProc(void *data, mpc_int32_t offset) { MusepackDecoder *decoder = (MusepackDecoder *) data; - + NSLog(@"MPC Seek: %i", offset); return [[decoder source] seek:offset whence:SEEK_SET]; } mpc_int32_t TellProc(void *data) { + NSLog(@"MPC Tell"); MusepackDecoder *decoder = (MusepackDecoder *) data; return [[decoder source] tell]; @@ -33,6 +35,7 @@ mpc_int32_t TellProc(void *data) mpc_int32_t GetSizeProc(void *data) { + NSLog(@"MPC GetSize"); MusepackDecoder *decoder = (MusepackDecoder *) data; if ([[decoder source] seekable]) { @@ -52,6 +55,7 @@ mpc_int32_t GetSizeProc(void *data) mpc_bool_t CanSeekProc(void *data) { + NSLog(@"MPC Canseek"); MusepackDecoder *decoder = (MusepackDecoder *) data; return [[decoder source] seekable]; @@ -61,15 +65,14 @@ mpc_bool_t CanSeekProc(void *data) { [self setSource: s]; - mpc_reader reader = { - read: ReadProc, - seek: SeekProc, - tell: TellProc, - get_size: GetSizeProc, - canseek: CanSeekProc - }; + reader.read = ReadProc; + reader.seek = SeekProc; + reader.tell = TellProc; + reader.get_size = GetSizeProc; + reader.canseek = CanSeekProc; + reader.data = self; + NSLog(@"%@", reader.data); - mpc_streaminfo info; mpc_streaminfo_init(&info); if (mpc_streaminfo_read(&info, &reader) != ERROR_CODE_OK) { @@ -77,7 +80,7 @@ mpc_bool_t CanSeekProc(void *data) return NO; } - /* instantiate a decoder with our file reader */ + /* instantiate a decoder with our reader */ mpc_decoder_setup(&decoder, &reader); if (!mpc_decoder_initialize(&decoder, &info)) { @@ -218,7 +221,7 @@ mpc_bool_t CanSeekProc(void *data) [NSNumber numberWithDouble:length], @"length", [NSNumber numberWithInt:16], @"bitsPerSample", [NSNumber numberWithInt:2], @"channels", - @"little",@"endian", + @"host",@"endian", nil]; } diff --git a/Plugins/WavPack/WavPack.xcodeproj/project.pbxproj b/Plugins/WavPack/WavPack.xcodeproj/project.pbxproj index da6d2f2f0..f0e980668 100644 --- a/Plugins/WavPack/WavPack.xcodeproj/project.pbxproj +++ b/Plugins/WavPack/WavPack.xcodeproj/project.pbxproj @@ -9,12 +9,10 @@ /* Begin PBXBuildFile section */ 1745C4DA0B90C42500A6768C /* WavPackDecoder.m in Sources */ = {isa = PBXBuildFile; fileRef = 1745C4D60B90C42500A6768C /* WavPackDecoder.m */; }; 1745C4DB0B90C42500A6768C /* WavPackPropertiesReader.m in Sources */ = {isa = PBXBuildFile; fileRef = 1745C4D80B90C42500A6768C /* WavPackPropertiesReader.m */; }; - 177FCF950B90C9450011C3B5 /* Plugin.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 177FCF940B90C9450011C3B5 /* Plugin.h */; }; - 179CFD490B90C6DC00C8C4DB /* WavPack.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 179CFD480B90C6DC00C8C4DB /* WavPack.framework */; }; - 179CFD4C0B90C6DF00C8C4DB /* WavPack.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 179CFD480B90C6DC00C8C4DB /* WavPack.framework */; }; - 17ADB2620B97946600257CA2 /* WavPackPlugin.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 17ADB2600B97946600257CA2 /* WavPackPlugin.h */; }; 17ADB2630B97946600257CA2 /* WavPackPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 17ADB2610B97946600257CA2 /* WavPackPlugin.m */; }; 8D5B49B4048680CD000E48DA /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7ADFEA557BF11CA2CBB /* Cocoa.framework */; }; + 8E2B8FE40B9B63CD00F2D9E8 /* WavPack.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8E2B8FE30B9B63CD00F2D9E8 /* WavPack.framework */; }; + 8E2B8FE70B9B63CF00F2D9E8 /* WavPack.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 8E2B8FE30B9B63CD00F2D9E8 /* WavPack.framework */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -24,9 +22,7 @@ dstPath = ""; dstSubfolderSpec = 10; files = ( - 179CFD4C0B90C6DF00C8C4DB /* WavPack.framework in CopyFiles */, - 177FCF950B90C9450011C3B5 /* Plugin.h in CopyFiles */, - 17ADB2620B97946600257CA2 /* WavPackPlugin.h in CopyFiles */, + 8E2B8FE70B9B63CF00F2D9E8 /* WavPack.framework in CopyFiles */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -41,12 +37,12 @@ 1745C4D70B90C42500A6768C /* WavPackPropertiesReader.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = WavPackPropertiesReader.h; sourceTree = ""; }; 1745C4D80B90C42500A6768C /* WavPackPropertiesReader.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = WavPackPropertiesReader.m; sourceTree = ""; }; 177FCF940B90C9450011C3B5 /* Plugin.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = Plugin.h; path = ../../Audio/Plugin.h; sourceTree = SOURCE_ROOT; }; - 179CFD480B90C6DC00C8C4DB /* WavPack.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WavPack.framework; path = ../../Frameworks/WavPack/build/Release/WavPack.framework; sourceTree = SOURCE_ROOT; }; 17ADB2600B97946600257CA2 /* WavPackPlugin.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = WavPackPlugin.h; sourceTree = ""; }; 17ADB2610B97946600257CA2 /* WavPackPlugin.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = WavPackPlugin.m; sourceTree = ""; }; 32DBCF630370AF2F00C91783 /* WavPack_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WavPack_Prefix.pch; sourceTree = ""; }; 8D5B49B6048680CD000E48DA /* WavPack.bundle */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = WavPack.bundle; sourceTree = BUILT_PRODUCTS_DIR; }; 8D5B49B7048680CD000E48DA /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = Info.plist; sourceTree = ""; }; + 8E2B8FE30B9B63CD00F2D9E8 /* WavPack.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WavPack.framework; path = ../../Frameworks/WavPack/build/Release/WavPack.framework; sourceTree = SOURCE_ROOT; }; D2F7E65807B2D6F200F64583 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = ""; }; /* End PBXFileReference section */ @@ -56,7 +52,7 @@ buildActionMask = 2147483647; files = ( 8D5B49B4048680CD000E48DA /* Cocoa.framework in Frameworks */, - 179CFD490B90C6DC00C8C4DB /* WavPack.framework in Frameworks */, + 8E2B8FE40B9B63CD00F2D9E8 /* WavPack.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -109,7 +105,7 @@ 1058C7ACFEA557BF11CA2CBB /* Linked Frameworks */ = { isa = PBXGroup; children = ( - 179CFD480B90C6DC00C8C4DB /* WavPack.framework */, + 8E2B8FE30B9B63CD00F2D9E8 /* WavPack.framework */, 1058C7ADFEA557BF11CA2CBB /* Cocoa.framework */, ); name = "Linked Frameworks"; @@ -210,9 +206,13 @@ "$(inherited)", "$(FRAMEWORK_SEARCH_PATHS_QUOTED_1)", "$(FRAMEWORK_SEARCH_PATHS_QUOTED_2)", + "$(FRAMEWORK_SEARCH_PATHS_QUOTED_3)", + "$(FRAMEWORK_SEARCH_PATHS_QUOTED_4)", ); FRAMEWORK_SEARCH_PATHS_QUOTED_1 = "\"$(SRCROOT)/../../../Frameworks/WavPack/build/Release\""; FRAMEWORK_SEARCH_PATHS_QUOTED_2 = "\"$(SRCROOT)/../../Frameworks/WavPack/build/Release\""; + FRAMEWORK_SEARCH_PATHS_QUOTED_3 = "\"$(SRCROOT)/../../Frameworks/WavPack/build/Debug\""; + FRAMEWORK_SEARCH_PATHS_QUOTED_4 = "\"$(SRCROOT)/../../Frameworks/WavPack/build/Release\""; GCC_DYNAMIC_NO_PIC = NO; GCC_ENABLE_FIX_AND_CONTINUE = YES; GCC_MODEL_TUNING = G5; @@ -238,9 +238,13 @@ "$(inherited)", "$(FRAMEWORK_SEARCH_PATHS_QUOTED_1)", "$(FRAMEWORK_SEARCH_PATHS_QUOTED_2)", + "$(FRAMEWORK_SEARCH_PATHS_QUOTED_3)", + "$(FRAMEWORK_SEARCH_PATHS_QUOTED_4)", ); FRAMEWORK_SEARCH_PATHS_QUOTED_1 = "\"$(SRCROOT)/../../../Frameworks/WavPack/build/Release\""; FRAMEWORK_SEARCH_PATHS_QUOTED_2 = "\"$(SRCROOT)/../../Frameworks/WavPack/build/Release\""; + FRAMEWORK_SEARCH_PATHS_QUOTED_3 = "\"$(SRCROOT)/../../Frameworks/WavPack/build/Debug\""; + FRAMEWORK_SEARCH_PATHS_QUOTED_4 = "\"$(SRCROOT)/../../Frameworks/WavPack/build/Release\""; GCC_GENERATE_DEBUGGING_SYMBOLS = NO; GCC_MODEL_TUNING = G5; GCC_PRECOMPILE_PREFIX_HEADER = YES; diff --git a/Plugins/WavPack/WavPackDecoder.h b/Plugins/WavPack/WavPackDecoder.h index d5d6508fd..a4e01c4ff 100644 --- a/Plugins/WavPack/WavPackDecoder.h +++ b/Plugins/WavPack/WavPackDecoder.h @@ -14,6 +14,9 @@ @interface WavPackDecoder : NSObject { WavpackContext *wpc; + WavpackStreamReader reader; + + id source; int bitsPerSample; int channels; @@ -22,4 +25,7 @@ double length; } +- (void)setSource:(id)s; +- (id)source; + @end diff --git a/Plugins/WavPack/WavPackDecoder.m b/Plugins/WavPack/WavPackDecoder.m index ff5a2e976..ac5ab6124 100644 --- a/Plugins/WavPack/WavPackDecoder.m +++ b/Plugins/WavPack/WavPackDecoder.m @@ -11,14 +11,102 @@ @implementation WavPackDecoder -- (BOOL)open:(NSURL *)url +int32_t ReadBytesProc(void *ds, void *data, int32_t bcount) +{ + WavPackDecoder *decoder = (WavPackDecoder *)ds; + + return [[decoder source] read:data amount:bcount]; +} + +uint32_t GetPosProc(void *ds) +{ + WavPackDecoder *decoder = (WavPackDecoder *)ds; + + return [[decoder source] tell]; +} + +int SetPosAbsProc(void *ds, uint32_t pos) +{ + WavPackDecoder *decoder = (WavPackDecoder *)ds; + + return ([[decoder source] seek:pos whence:SEEK_SET] ? 0: -1); +} + +int SetPosRelProc(void *ds, int32_t delta, int mode) +{ + WavPackDecoder *decoder = (WavPackDecoder *)ds; + + return ([[decoder source] seek:delta whence:mode] ? 0: -1); +} + +int PushBackByteProc(void *ds, int c) +{ + WavPackDecoder *decoder = (WavPackDecoder *)ds; + + if ([[decoder source] seekable]) { + [[decoder source] seek:-1 whence:SEEK_CUR]; + + return c; + } + else { + return -1; + } +} + +uint32_t GetLengthProc(void *ds) +{ + WavPackDecoder *decoder = (WavPackDecoder *)ds; + + if ([[decoder source] seekable]) { + long currentPos = [[decoder source] tell]; + + [[decoder source] seek:0 whence:SEEK_END]; + long size = [[decoder source] tell]; + + [[decoder source] seek:currentPos whence:SEEK_SET]; + + return size; + } + else { + return 0; + } +} + +int CanSeekProc(void *ds) +{ + WavPackDecoder *decoder = (WavPackDecoder *)ds; + + return [[decoder source] seekable]; +} + +int32_t WriteBytesProc(void *ds, void *data, int32_t bcount) +{ + return -1; +} + + +- (BOOL)open:(id)s { int open_flags = 0; char error[80]; + + [self setSource:s]; - wpc = WavpackOpenFileInput([[url path] UTF8String], error, open_flags, 0); - if (!wpc) + reader.read_bytes = ReadBytesProc; + reader.get_pos = GetPosProc; + reader.set_pos_abs = SetPosAbsProc; + reader.set_pos_rel = SetPosRelProc; + reader.push_back_byte = PushBackByteProc; + reader.get_length = GetLengthProc; + reader.can_seek = CanSeekProc; + reader.write_bytes = WriteBytesProc; + + //No corrections file (WVC) support at the moment. + wpc = WavpackOpenFileInputEx(&reader, self, NULL, error, open_flags, 0); + if (!wpc) { + NSLog(@"Unable to open file.."); return NO; + } channels = WavpackGetNumChannels(wpc); bitsPerSample = WavpackGetBitsPerSample(wpc); @@ -29,9 +117,12 @@ bitrate = (int)(WavpackGetAverageBitrate(wpc, TRUE)/1000.0); + [self willChangeValueForKey:@"properties"]; + [self didChangeValueForKey:@"properties"]; + return YES; } - +/* - (int)fillBuffer:(void *)buf ofSize:(UInt32)size { int numsamples; @@ -47,12 +138,85 @@ { ((UInt16 *)buf)[i] = ((UInt32 *)sampleBuf)[i]; } + n *= (bitsPerSample/8)*channels; free(sampleBuf); return n; } +*/ +- (int)fillBuffer:(void *)buf ofSize:(UInt32)size +{ + uint32_t sample; + int32_t audioSample; + uint32_t samplesRead; + int8_t *alias8; + int16_t *alias16; + int32_t *alias32; + + int inputBufferLength = size / (bitsPerSample/8); + int32_t *inputBuffer = malloc(inputBufferLength*sizeof(int32_t)); + + // Wavpack uses "complete" samples (one sample across all channels), i.e. a Core Audio frame + samplesRead = WavpackUnpackSamples(wpc, inputBuffer, inputBufferLength/channels); + + // Handle floating point files + // Perform hard clipping and convert to integers + if(MODE_FLOAT & WavpackGetMode(wpc) && 127 == WavpackGetFloatNormExp(wpc)) { + float f; + alias32 = inputBuffer; + for(sample = 0; sample < samplesRead * channels; ++sample) { + f = * ((float *) alias32); + + if(f > 1.0) { f = 1.0; } + if(f < -1.0) { f = -1.0; } + + // *alias32++ = (int32_t) (f * 2147483647.0); + *alias32++ = (int32_t) (f * 32767.0); + } + } + + switch(bitsPerSample) { + case 8: + // No need for byte swapping + alias8 = buf; + for(sample = 0; sample < samplesRead * channels; ++sample) { + *alias8++ = (int8_t)inputBuffer[sample]; + } + break; + case 16: + // Convert to big endian byte order + alias16 = buf; + for(sample = 0; sample < samplesRead*channels; ++sample) { + *alias16++ = (int16_t)OSSwapHostToBigInt16((int16_t)inputBuffer[sample]); + } + break; + case 24: + // Convert to big endian byte order + alias8 = buf; + for(sample = 0; sample < samplesRead * channels; ++sample) { + audioSample = OSSwapHostToBigInt32(inputBuffer[sample]); + *alias8++ = (int8_t)(audioSample >> 16); + *alias8++ = (int8_t)(audioSample >> 8); + *alias8++ = (int8_t)audioSample; + } + break; + case 32: + // Convert to big endian byte order + alias32 = buf; + for(sample = 0; sample < samplesRead * channels; ++sample) { + *alias32++ = OSSwapHostToBigInt32(inputBuffer[sample]); + } + break; + default: + NSLog(@"Unsupported sample size.."); + } + + free(inputBuffer); + + return samplesRead * channels * (bitsPerSample/8); +} - (double)seekToTime:(double)milliseconds { @@ -69,6 +233,22 @@ WavpackCloseFile(wpc); } +- (void)setSource:(id)s +{ + [s retain]; + [source release]; + source = s; +} + +- (id)source +{ + return source; +} + +- (BOOL)seekable +{ + return [source seekable]; +} - (NSDictionary *)properties { diff --git a/Plugins/WavPack/WavPackPlugin.m b/Plugins/WavPack/WavPackPlugin.m index 7743dab32..89d66fa19 100644 --- a/Plugins/WavPack/WavPackPlugin.m +++ b/Plugins/WavPack/WavPackPlugin.m @@ -16,7 +16,7 @@ { return [NSDictionary dictionaryWithObjectsAndKeys: kCogDecoder, [WavPackDecoder className], - kCogPropertiesReader, [WavPackPropertiesReader className, + kCogPropertiesReader, [WavPackPropertiesReader className], nil ]; } diff --git a/Plugins/WavPack/WavPackPropertiesReader.m b/Plugins/WavPack/WavPackPropertiesReader.m index f576a0db1..c18360ccc 100644 --- a/Plugins/WavPack/WavPackPropertiesReader.m +++ b/Plugins/WavPack/WavPackPropertiesReader.m @@ -11,13 +11,13 @@ @implementation WavPackPropertiesReader -- (NSDictionary *)propertiesForURL:(NSURL *)url ++ (NSDictionary *)propertiesForSource:(id)source { NSDictionary *properties; WavPackDecoder *decoder; decoder = [[WavPackDecoder alloc] init]; - if (![decoder open:url]) + if (![decoder open:source]) { return nil; }