2007-10-03 20:23:14 +00:00
|
|
|
//
|
|
|
|
// ConverterNode.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Zaphod Beeblebrox on 8/2/05.
|
|
|
|
// Copyright 2005 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
#import <Accelerate/Accelerate.h>
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
2007-10-03 20:23:14 +00:00
|
|
|
#import "ConverterNode.h"
|
|
|
|
|
2022-01-15 10:09:26 +00:00
|
|
|
#import "BufferChain.h"
|
|
|
|
#import "OutputNode.h"
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2022-02-16 06:45:28 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
#import "BadSampleCleaner.h"
|
|
|
|
#endif
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
void PrintStreamDesc(AudioStreamBasicDescription *inDesc) {
|
|
|
|
if(!inDesc) {
|
|
|
|
DLog(@"Can't print a NULL desc!\n");
|
2007-10-03 20:23:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
DLog(@"- - - - - - - - - - - - - - - - - - - -\n");
|
|
|
|
DLog(@" Sample Rate:%f\n", inDesc->mSampleRate);
|
|
|
|
DLog(@" Format ID:%s\n", (char *)&inDesc->mFormatID);
|
|
|
|
DLog(@" Format Flags:%X\n", inDesc->mFormatFlags);
|
|
|
|
DLog(@" Bytes per Packet:%d\n", inDesc->mBytesPerPacket);
|
|
|
|
DLog(@" Frames per Packet:%d\n", inDesc->mFramesPerPacket);
|
|
|
|
DLog(@" Bytes per Frame:%d\n", inDesc->mBytesPerFrame);
|
|
|
|
DLog(@" Channels per Frame:%d\n", inDesc->mChannelsPerFrame);
|
|
|
|
DLog(@" Bits per Channel:%d\n", inDesc->mBitsPerChannel);
|
|
|
|
DLog(@"- - - - - - - - - - - - - - - - - - - -\n");
|
2007-10-03 20:23:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@implementation ConverterNode
|
|
|
|
|
2022-06-15 23:47:43 +00:00
|
|
|
static void *kConverterNodeContext = &kConverterNodeContext;
|
|
|
|
|
2022-01-14 19:18:50 +00:00
|
|
|
@synthesize inputFormat;
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)initWithController:(id)c previous:(id)p {
|
|
|
|
self = [super initWithController:c previous:p];
|
|
|
|
if(self) {
|
|
|
|
rgInfo = nil;
|
|
|
|
|
|
|
|
inputBuffer = NULL;
|
|
|
|
inputBufferSize = 0;
|
|
|
|
|
|
|
|
stopping = NO;
|
|
|
|
convertEntered = NO;
|
|
|
|
paused = NO;
|
|
|
|
|
2022-06-15 23:47:43 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.volumeScaling" options:0 context:kConverterNodeContext];
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
2013-10-11 03:02:02 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 07:34:19 +00:00
|
|
|
void scale_by_volume(float *buffer, size_t count, float volume) {
|
2022-02-07 05:49:27 +00:00
|
|
|
if(volume != 1.0) {
|
2022-02-16 06:53:09 +00:00
|
|
|
size_t unaligned = (uintptr_t)buffer & 15;
|
|
|
|
if(unaligned) {
|
|
|
|
size_t count3 = unaligned >> 2;
|
|
|
|
while(count3 > 0) {
|
|
|
|
*buffer++ *= volume;
|
|
|
|
count3--;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
vDSP_vsmul(buffer, 1, &volume, buffer, 1, count);
|
|
|
|
}
|
2013-10-07 10:59:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)process {
|
|
|
|
// Removed endOfStream check from here, since we want to be able to flush the converter
|
|
|
|
// when the end of stream is reached. Convert function instead processes what it can,
|
|
|
|
// and returns 0 samples when it has nothing more to process at the end of stream.
|
|
|
|
while([self shouldContinue] == YES) {
|
2022-07-10 22:14:47 +00:00
|
|
|
AudioChunk *chunk = nil;
|
2022-02-08 06:02:17 +00:00
|
|
|
while(paused) {
|
|
|
|
usleep(500);
|
|
|
|
}
|
2022-02-07 12:06:36 +00:00
|
|
|
@autoreleasepool {
|
2022-07-10 22:14:47 +00:00
|
|
|
chunk = [self convert];
|
|
|
|
}
|
|
|
|
if(!chunk) {
|
2022-07-10 23:24:08 +00:00
|
|
|
if(paused) {
|
|
|
|
continue;
|
|
|
|
} else if(!streamFormatChanged) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
@autoreleasepool {
|
|
|
|
[self writeChunk:chunk];
|
|
|
|
chunk = nil;
|
|
|
|
}
|
2022-07-10 22:14:47 +00:00
|
|
|
}
|
|
|
|
if(streamFormatChanged) {
|
|
|
|
[self cleanUp];
|
|
|
|
[self setupWithInputFormat:newInputFormat withInputConfig:newInputChannelConfig isLossless:rememberedLossless];
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
2007-10-03 20:23:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-10 22:14:47 +00:00
|
|
|
- (AudioChunk *)convert {
|
2021-12-25 23:02:13 +00:00
|
|
|
UInt32 ioNumberPackets;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if(stopping)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
convertEntered = YES;
|
|
|
|
|
|
|
|
if(stopping || [self shouldContinue] == NO) {
|
|
|
|
convertEntered = NO;
|
2022-07-10 22:14:47 +00:00
|
|
|
return nil;
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 08:45:49 +00:00
|
|
|
while(inpOffset == inpSize) {
|
|
|
|
// Approximately the most we want on input
|
|
|
|
ioNumberPackets = 4096;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-14 08:45:49 +00:00
|
|
|
size_t newSize = ioNumberPackets * floatFormat.mBytesPerPacket;
|
|
|
|
if(!inputBuffer || inputBufferSize < newSize)
|
|
|
|
inputBuffer = realloc(inputBuffer, inputBufferSize = newSize);
|
|
|
|
|
|
|
|
ssize_t amountToWrite = ioNumberPackets * floatFormat.mBytesPerPacket;
|
|
|
|
|
|
|
|
ssize_t bytesReadFromInput = 0;
|
|
|
|
|
|
|
|
while(bytesReadFromInput < amountToWrite && !stopping && !paused && !streamFormatChanged && [self shouldContinue] == YES && [self endOfStream] == NO) {
|
|
|
|
AudioStreamBasicDescription inf;
|
|
|
|
uint32_t config;
|
|
|
|
if([self peekFormat:&inf channelConfig:&config]) {
|
|
|
|
if(config != inputChannelConfig || memcmp(&inf, &inputFormat, sizeof(inf)) != 0) {
|
|
|
|
if(inputChannelConfig == 0 && memcmp(&inf, &inputFormat, sizeof(inf)) == 0) {
|
|
|
|
inputChannelConfig = config;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
newInputFormat = inf;
|
|
|
|
newInputChannelConfig = config;
|
|
|
|
streamFormatChanged = YES;
|
|
|
|
break;
|
2022-02-08 03:18:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 08:45:49 +00:00
|
|
|
AudioChunk *chunk = [self readChunkAsFloat32:((amountToWrite - bytesReadFromInput) / floatFormat.mBytesPerPacket)];
|
|
|
|
inf = [chunk format];
|
|
|
|
size_t frameCount = [chunk frameCount];
|
|
|
|
config = [chunk channelConfig];
|
|
|
|
size_t bytesRead = frameCount * inf.mBytesPerPacket;
|
|
|
|
if(frameCount) {
|
|
|
|
NSData *samples = [chunk removeSamples:frameCount];
|
|
|
|
memcpy(((uint8_t *)inputBuffer) + bytesReadFromInput, [samples bytes], bytesRead);
|
|
|
|
if([chunk isHDCD]) {
|
|
|
|
[controller sustainHDCD];
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-14 08:45:49 +00:00
|
|
|
bytesReadFromInput += bytesRead;
|
|
|
|
if(!frameCount) {
|
|
|
|
usleep(500);
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 08:45:49 +00:00
|
|
|
if(!bytesReadFromInput) {
|
2022-02-07 05:49:27 +00:00
|
|
|
convertEntered = NO;
|
2022-07-14 08:45:49 +00:00
|
|
|
return nil;
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 08:45:49 +00:00
|
|
|
// Input now contains bytesReadFromInput worth of floats, in the input sample rate
|
|
|
|
inpSize = bytesReadFromInput;
|
|
|
|
inpOffset = 0;
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 08:45:49 +00:00
|
|
|
ioNumberPackets = (UInt32)(inpSize - inpOffset);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-14 08:45:49 +00:00
|
|
|
ioNumberPackets -= ioNumberPackets % floatFormat.mBytesPerPacket;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-10 23:36:31 +00:00
|
|
|
if(ioNumberPackets) {
|
|
|
|
AudioChunk *chunk = [[AudioChunk alloc] init];
|
|
|
|
[chunk setFormat:nodeFormat];
|
|
|
|
if(nodeChannelConfig) {
|
|
|
|
[chunk setChannelConfig:nodeChannelConfig];
|
|
|
|
}
|
2022-07-14 08:45:49 +00:00
|
|
|
scale_by_volume((float *)(((uint8_t *)inputBuffer) + inpOffset), ioNumberPackets / sizeof(float), volumeScale);
|
|
|
|
[chunk assignSamples:(((uint8_t *)inputBuffer) + inpOffset) frameCount:ioNumberPackets / floatFormat.mBytesPerPacket];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-14 08:45:49 +00:00
|
|
|
inpOffset += ioNumberPackets;
|
2022-07-10 23:36:31 +00:00
|
|
|
convertEntered = NO;
|
|
|
|
return chunk;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
convertEntered = NO;
|
2022-07-10 23:36:31 +00:00
|
|
|
return nil;
|
2007-10-03 20:23:14 +00:00
|
|
|
}
|
|
|
|
|
2013-10-07 10:59:04 +00:00
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath
|
2022-02-07 05:49:27 +00:00
|
|
|
ofObject:(id)object
|
2013-10-07 10:59:04 +00:00
|
|
|
change:(NSDictionary *)change
|
2022-02-07 05:49:27 +00:00
|
|
|
context:(void *)context {
|
2022-06-15 23:47:43 +00:00
|
|
|
if(context == kConverterNodeContext) {
|
|
|
|
DLog(@"SOMETHING CHANGED!");
|
|
|
|
if([keyPath isEqualToString:@"values.volumeScaling"]) {
|
|
|
|
// User reset the volume scaling option
|
|
|
|
[self refreshVolumeScaling];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
2013-10-07 10:59:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static float db_to_scale(float db) {
|
|
|
|
return pow(10.0, db / 20);
|
2013-10-07 10:59:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)refreshVolumeScaling {
|
|
|
|
if(rgInfo == nil) {
|
|
|
|
volumeScale = 1.0;
|
|
|
|
return;
|
|
|
|
}
|
2013-10-07 10:59:04 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
NSString *scaling = [[NSUserDefaults standardUserDefaults] stringForKey:@"volumeScaling"];
|
|
|
|
BOOL useAlbum = [scaling hasPrefix:@"albumGain"];
|
|
|
|
BOOL useTrack = useAlbum || [scaling hasPrefix:@"trackGain"];
|
|
|
|
BOOL useVolume = useAlbum || useTrack || [scaling isEqualToString:@"volumeScale"];
|
|
|
|
BOOL usePeak = [scaling hasSuffix:@"WithPeak"];
|
|
|
|
float scale = 1.0;
|
|
|
|
float peak = 0.0;
|
|
|
|
if(useVolume) {
|
|
|
|
id pVolumeScale = [rgInfo objectForKey:@"volume"];
|
|
|
|
if(pVolumeScale != nil)
|
|
|
|
scale = [pVolumeScale floatValue];
|
|
|
|
}
|
|
|
|
if(useTrack) {
|
|
|
|
id trackGain = [rgInfo objectForKey:@"replayGainTrackGain"];
|
|
|
|
id trackPeak = [rgInfo objectForKey:@"replayGainTrackPeak"];
|
|
|
|
if(trackGain != nil)
|
|
|
|
scale = db_to_scale([trackGain floatValue]);
|
|
|
|
if(trackPeak != nil)
|
|
|
|
peak = [trackPeak floatValue];
|
|
|
|
}
|
|
|
|
if(useAlbum) {
|
|
|
|
id albumGain = [rgInfo objectForKey:@"replayGainAlbumGain"];
|
|
|
|
id albumPeak = [rgInfo objectForKey:@"replayGainAlbumPeak"];
|
|
|
|
if(albumGain != nil)
|
|
|
|
scale = db_to_scale([albumGain floatValue]);
|
|
|
|
if(albumPeak != nil)
|
|
|
|
peak = [albumPeak floatValue];
|
|
|
|
}
|
|
|
|
if(usePeak) {
|
|
|
|
if(scale * peak > 1.0)
|
|
|
|
scale = 1.0 / peak;
|
|
|
|
}
|
|
|
|
volumeScale = scale;
|
|
|
|
}
|
2013-10-07 10:59:04 +00:00
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
- (BOOL)setupWithInputFormat:(AudioStreamBasicDescription)inf withInputConfig:(uint32_t)inputConfig isLossless:(BOOL)lossless {
|
2022-02-07 05:49:27 +00:00
|
|
|
// Make the converter
|
2007-10-03 20:23:14 +00:00
|
|
|
inputFormat = inf;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-02-07 08:56:05 +00:00
|
|
|
inputChannelConfig = inputConfig;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
rememberedLossless = lossless;
|
|
|
|
|
|
|
|
// These are the only sample formats we support translating
|
|
|
|
BOOL isFloat = !!(inputFormat.mFormatFlags & kAudioFormatFlagIsFloat);
|
|
|
|
if((!isFloat && !(inputFormat.mBitsPerChannel >= 1 && inputFormat.mBitsPerChannel <= 32)) || (isFloat && !(inputFormat.mBitsPerChannel == 32 || inputFormat.mBitsPerChannel == 64)))
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
floatFormat = inputFormat;
|
|
|
|
floatFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
|
|
|
|
floatFormat.mBitsPerChannel = 32;
|
|
|
|
floatFormat.mBytesPerFrame = (32 / 8) * floatFormat.mChannelsPerFrame;
|
|
|
|
floatFormat.mBytesPerPacket = floatFormat.mBytesPerFrame * floatFormat.mFramesPerPacket;
|
|
|
|
|
2022-06-10 09:13:10 +00:00
|
|
|
#if DSD_DECIMATE
|
2022-02-07 05:49:27 +00:00
|
|
|
if(inputFormat.mBitsPerChannel == 1) {
|
|
|
|
// Decimate this for speed
|
|
|
|
floatFormat.mSampleRate *= 1.0 / 8.0;
|
|
|
|
}
|
2022-06-10 09:13:10 +00:00
|
|
|
#endif
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
inpOffset = 0;
|
|
|
|
inpSize = 0;
|
|
|
|
|
|
|
|
// This is a post resampler, post-down/upmix format
|
|
|
|
|
2022-07-14 08:45:49 +00:00
|
|
|
nodeFormat = floatFormat;
|
2022-06-24 06:17:31 +00:00
|
|
|
nodeChannelConfig = inputChannelConfig;
|
2022-01-19 08:27:59 +00:00
|
|
|
|
2007-10-11 02:08:29 +00:00
|
|
|
PrintStreamDesc(&inf);
|
2022-06-24 06:17:31 +00:00
|
|
|
PrintStreamDesc(&nodeFormat);
|
2013-10-07 10:59:04 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
[self refreshVolumeScaling];
|
|
|
|
|
|
|
|
// Move this here so process call isn't running the resampler until it's allocated
|
|
|
|
stopping = NO;
|
|
|
|
convertEntered = NO;
|
2022-02-08 03:18:45 +00:00
|
|
|
streamFormatChanged = NO;
|
2022-02-08 06:02:17 +00:00
|
|
|
paused = NO;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-10-03 20:23:14 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)dealloc {
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Decoder dealloc");
|
2013-10-11 03:02:02 +00:00
|
|
|
|
2022-06-15 23:47:43 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.volumeScaling" context:kConverterNodeContext];
|
2022-01-26 05:30:33 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
paused = NO;
|
2007-10-13 07:09:46 +00:00
|
|
|
[self cleanUp];
|
|
|
|
}
|
|
|
|
|
2022-02-07 08:56:05 +00:00
|
|
|
- (void)inputFormatDidChange:(AudioStreamBasicDescription)format inputConfig:(uint32_t)inputConfig {
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"FORMAT CHANGED");
|
2022-02-07 05:49:27 +00:00
|
|
|
paused = YES;
|
2022-02-08 06:02:17 +00:00
|
|
|
while(convertEntered) {
|
|
|
|
usleep(500);
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
2022-02-08 06:02:17 +00:00
|
|
|
[self cleanUp];
|
2022-06-24 06:17:31 +00:00
|
|
|
[self setupWithInputFormat:format withInputConfig:inputConfig isLossless:rememberedLossless];
|
2007-10-12 02:55:59 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setRGInfo:(NSDictionary *)rgi {
|
|
|
|
DLog(@"Setting ReplayGain info");
|
|
|
|
rgInfo = rgi;
|
|
|
|
[self refreshVolumeScaling];
|
2013-10-07 10:59:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)cleanUp {
|
|
|
|
stopping = YES;
|
|
|
|
while(convertEntered) {
|
|
|
|
usleep(500);
|
|
|
|
}
|
|
|
|
if(inputBuffer) {
|
2022-01-11 12:00:34 +00:00
|
|
|
free(inputBuffer);
|
|
|
|
inputBuffer = NULL;
|
2022-02-07 05:49:27 +00:00
|
|
|
inputBufferSize = 0;
|
2007-10-13 07:09:46 +00:00
|
|
|
}
|
2022-07-14 08:45:49 +00:00
|
|
|
inpOffset = 0;
|
|
|
|
inpSize = 0;
|
2007-10-03 20:23:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (double)secondsBuffered {
|
|
|
|
return [buffer listDuration];
|
2022-01-14 07:05:32 +00:00
|
|
|
}
|
|
|
|
|
2007-10-03 20:23:14 +00:00
|
|
|
@end
|