diff --git a/Audio/Chain/InputNode.m b/Audio/Chain/InputNode.m index 3381a7437..a2ec1f73e 100644 --- a/Audio/Chain/InputNode.m +++ b/Audio/Chain/InputNode.m @@ -49,7 +49,7 @@ int bitsPerSample = [[properties objectForKey:@"bitsPerSample"] intValue]; int channels = [[properties objectForKey:@"channels"] intValue]; - bytesPerFrame = (bitsPerSample / 8) * channels; + bytesPerFrame = ((bitsPerSample + 7) / 8) * channels; shouldContinue = YES; shouldSeek = NO; @@ -66,7 +66,7 @@ int bitsPerSample = [[properties objectForKey:@"bitsPerSample"] intValue]; int channels = [[properties objectForKey:@"channels"] intValue]; - bytesPerFrame = (bitsPerSample / 8) * channels; + bytesPerFrame = ((bitsPerSample + 7) / 8) * channels; [self registerObservers]; diff --git a/Audio/ThirdParty/CoreAudioUtils/CoreAudioUtils.m b/Audio/ThirdParty/CoreAudioUtils/CoreAudioUtils.m index 6fededc55..66e16ac2f 100644 --- a/Audio/ThirdParty/CoreAudioUtils/CoreAudioUtils.m +++ b/Audio/ThirdParty/CoreAudioUtils/CoreAudioUtils.m @@ -42,7 +42,7 @@ AudioStreamBasicDescription propertiesToASBD(NSDictionary *properties) asbd.mBitsPerChannel = [[properties objectForKey:@"bitsPerSample"] intValue]; asbd.mChannelsPerFrame = [[properties objectForKey:@"channels"] intValue];; - asbd.mBytesPerFrame = (asbd.mBitsPerChannel/8)*asbd.mChannelsPerFrame; + asbd.mBytesPerFrame = ((asbd.mBitsPerChannel+7)/8)*asbd.mChannelsPerFrame; asbd.mFramesPerPacket = 1; asbd.mBytesPerPacket = asbd.mBytesPerFrame * asbd.mFramesPerPacket; diff --git a/Plugins/Flac/FlacDecoder.h b/Plugins/Flac/FlacDecoder.h index c6b255443..95fb92daf 100644 --- a/Plugins/Flac/FlacDecoder.h +++ b/Plugins/Flac/FlacDecoder.h @@ -10,7 +10,7 @@ #import "FLAC/all.h" #define SAMPLES_PER_WRITE 512 -#define FLAC__MAX_SUPPORTED_CHANNELS 2 +#define FLAC__MAX_SUPPORTED_CHANNELS 8 #define SAMPLE_blockBuffer_SIZE ((FLAC__MAX_BLOCK_SIZE + SAMPLES_PER_WRITE) * FLAC__MAX_SUPPORTED_CHANNELS * (24/8)) #import "Plugin.h" @@ -30,6 +30,8 @@ float frequency; long totalFrames; + long fileSize; + BOOL hasStreamInfo; } @@ -39,6 +41,8 @@ - (void)setEndOfStream:(BOOL)eos; - (BOOL)endOfStream; +- (void)setSize:(long)size; + - (FLAC__StreamDecoder *)decoder; - (char *)blockBuffer; - (int)blockBufferFrames; diff --git a/Plugins/Flac/FlacDecoder.m b/Plugins/Flac/FlacDecoder.m index b0ad4c03f..8f6ec1eb3 100644 --- a/Plugins/Flac/FlacDecoder.m +++ b/Plugins/Flac/FlacDecoder.m @@ -140,7 +140,22 @@ FLAC__StreamDecoderWriteStatus WriteCallback(const FLAC__StreamDecoder *decoder, } } default: - ALog(@"Error, unsupported sample size."); + // Time for some nearest byte padding up to 32 + alias8 = blockBuffer; + int sampleSize = frame->header.bits_per_sample; + int sampleBit; + for(sample = 0; sample < frame->header.blocksize; ++sample) { + for(channel = 0; channel < frame->header.channels; ++channel) { + int32_t sampleExtended = sampleblockBuffer[channel][sample]; + for(sampleBit = sampleSize - 8; sampleBit >= -8; sampleBit -= 8) { + if (sampleBit >= 0) + *alias8++ = (uint8_t)((sampleExtended >> sampleBit) & 0xFF); + else + *alias8++ = (uint8_t)((sampleExtended << -sampleBit) & 0xFF); + } + } + } + break; } [flacDecoder setBlockBufferFrames:frame->header.blocksize]; @@ -178,6 +193,14 @@ void ErrorCallback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorS - (BOOL)open:(id)s { [self setSource:s]; + [self setSize:0]; + + if ([s seekable]) + { + [s seek:0 whence:SEEK_END]; + [self setSize:[s tell]]; + [s seek:0 whence:SEEK_SET]; + } decoder = FLAC__stream_decoder_new(); if (decoder == NULL) @@ -208,7 +231,7 @@ void ErrorCallback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorS - (int)readAudio:(void *)buffer frames:(UInt32)frames { int framesRead = 0; - int bytesPerFrame = (bitsPerSample/8) * channels; + int bytesPerFrame = ((bitsPerSample+7)/8) * channels; while (framesRead < frames) { if (blockBufferFrames == 0) @@ -309,6 +332,11 @@ void ErrorCallback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorS return endOfStream; } +- (void)setSize:(long)size +{ + fileSize = size; +} + - (NSDictionary *)properties { return [NSDictionary dictionaryWithObjectsAndKeys: @@ -317,6 +345,7 @@ void ErrorCallback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorS [NSNumber numberWithFloat:frequency],@"sampleRate", [NSNumber numberWithDouble:totalFrames],@"totalFrames", [NSNumber numberWithBool:[source seekable]], @"seekable", + [NSNumber numberWithInt:fileSize ? (fileSize * 8 / ((totalFrames + (frequency / 2)) / frequency)) / 1000 : 0], @"bitrate", @"FLAC",@"codec", @"big",@"endian", nil]; @@ -334,7 +363,7 @@ void ErrorCallback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorS + (float)priority { - return 1.0; + return 2.0; } @end