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.
|
|
|
|
//
|
|
|
|
|
|
|
|
#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-01-11 12:00:34 +00:00
|
|
|
#import <audio/conversion/s16_to_float.h>
|
|
|
|
#import <audio/conversion/s32_to_float.h>
|
|
|
|
|
2022-01-12 02:19:30 +00:00
|
|
|
#import "lpc.h"
|
2022-01-19 08:27:59 +00:00
|
|
|
#import "util.h"
|
2022-01-12 02:19:30 +00:00
|
|
|
|
2022-01-19 10:08:57 +00:00
|
|
|
#import "hdcd_decode2.h"
|
|
|
|
|
2022-01-16 08:41:54 +00:00
|
|
|
#import <TargetConditionals.h>
|
|
|
|
|
|
|
|
#if TARGET_CPU_X86 || TARGET_CPU_X86_64
|
|
|
|
#include <emmintrin.h>
|
|
|
|
#elif TARGET_CPU_ARM || TARGET_CPU_ARM64
|
|
|
|
#include <arm_neon.h>
|
|
|
|
#endif
|
|
|
|
|
2007-10-03 20:23:14 +00:00
|
|
|
void PrintStreamDesc (AudioStreamBasicDescription *inDesc)
|
|
|
|
{
|
|
|
|
if (!inDesc) {
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog (@"Can't print a NULL desc!\n");
|
2007-10-03 20:23:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-10-11 12:03:55 +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-01-14 19:18:50 +00:00
|
|
|
@synthesize inputFormat;
|
|
|
|
|
2013-10-11 03:02:02 +00:00
|
|
|
- (id)initWithController:(id)c previous:(id)p
|
|
|
|
{
|
|
|
|
self = [super initWithController:c previous:p];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
rgInfo = nil;
|
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
resampler = NULL;
|
|
|
|
resampler_data = NULL;
|
|
|
|
inputBuffer = NULL;
|
|
|
|
inputBufferSize = 0;
|
2013-10-13 12:14:57 +00:00
|
|
|
floatBuffer = NULL;
|
2021-12-25 23:02:13 +00:00
|
|
|
floatBufferSize = 0;
|
2021-12-26 07:41:45 +00:00
|
|
|
|
|
|
|
stopping = NO;
|
|
|
|
convertEntered = NO;
|
2022-01-13 07:13:00 +00:00
|
|
|
paused = NO;
|
|
|
|
outputFormatChanged = NO;
|
2022-01-11 12:00:34 +00:00
|
|
|
|
2022-01-19 08:27:59 +00:00
|
|
|
skipResampler = YES;
|
2022-01-14 03:43:18 +00:00
|
|
|
|
|
|
|
refillNode = nil;
|
|
|
|
originalPreviousNode = nil;
|
2022-01-14 10:00:32 +00:00
|
|
|
|
|
|
|
extrapolateBuffer = NULL;
|
|
|
|
extrapolateBufferSize = 0;
|
2022-01-14 14:12:14 +00:00
|
|
|
|
|
|
|
dsd2pcm = NULL;
|
|
|
|
dsd2pcmCount = 0;
|
2022-01-14 19:18:50 +00:00
|
|
|
|
|
|
|
outputResampling = @"";
|
2022-01-19 10:08:57 +00:00
|
|
|
|
|
|
|
hdcd_decoder = NULL;
|
2013-10-13 12:14:57 +00:00
|
|
|
|
2013-10-11 03:02:02 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.volumeScaling" options:0 context:nil];
|
2022-01-11 12:00:34 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.outputResampling" options:0 context:nil];
|
2022-01-26 00:50:42 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.headphoneVirtualization" options:0 context:nil];
|
2013-10-11 03:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2013-10-07 07:24:26 +00:00
|
|
|
static const float STEREO_DOWNMIX[8-2][8][2]={
|
|
|
|
/*3.0*/
|
|
|
|
{
|
|
|
|
{0.5858F,0.0F},{0.0F,0.5858F},{0.4142F,0.4142F}
|
|
|
|
},
|
|
|
|
/*quadrophonic*/
|
|
|
|
{
|
|
|
|
{0.4226F,0.0F},{0.0F,0.4226F},{0.366F,0.2114F},{0.2114F,0.336F}
|
|
|
|
},
|
|
|
|
/*5.0*/
|
|
|
|
{
|
|
|
|
{0.651F,0.0F},{0.0F,0.651F},{0.46F,0.46F},{0.5636F,0.3254F},
|
|
|
|
{0.3254F,0.5636F}
|
|
|
|
},
|
|
|
|
/*5.1*/
|
|
|
|
{
|
|
|
|
{0.529F,0.0F},{0.0F,0.529F},{0.3741F,0.3741F},{0.3741F,0.3741F},{0.4582F,0.2645F},
|
|
|
|
{0.2645F,0.4582F}
|
|
|
|
},
|
|
|
|
/*6.1*/
|
|
|
|
{
|
2021-12-28 08:10:44 +00:00
|
|
|
{0.4553F,0.0F},{0.0F,0.4553F},{0.322F,0.322F},{0.322F,0.322F},{0.2788F,0.2788F},
|
|
|
|
{0.3943F,0.2277F},{0.2277F,0.3943F}
|
2013-10-07 07:24:26 +00:00
|
|
|
},
|
|
|
|
/*7.1*/
|
|
|
|
{
|
|
|
|
{0.3886F,0.0F},{0.0F,0.3886F},{0.2748F,0.2748F},{0.2748F,0.2748F},{0.3366F,0.1943F},
|
|
|
|
{0.1943F,0.3366F},{0.3366F,0.1943F},{0.1943F,0.3366F}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
static void downmix_to_stereo(float * buffer, int channels, size_t count)
|
2013-10-07 07:24:26 +00:00
|
|
|
{
|
2021-12-25 23:02:13 +00:00
|
|
|
if (channels >= 3 && channels <= 8)
|
2022-01-11 12:00:34 +00:00
|
|
|
for (size_t i = 0; i < count; ++i)
|
2013-10-07 07:24:26 +00:00
|
|
|
{
|
|
|
|
float left = 0, right = 0;
|
|
|
|
for (int j = 0; j < channels; ++j)
|
|
|
|
{
|
|
|
|
left += buffer[i * channels + j] * STEREO_DOWNMIX[channels - 3][j][0];
|
|
|
|
right += buffer[i * channels + j] * STEREO_DOWNMIX[channels - 3][j][1];
|
|
|
|
}
|
2021-12-25 23:02:13 +00:00
|
|
|
buffer[i * 2 + 0] = left;
|
|
|
|
buffer[i * 2 + 1] = right;
|
2013-10-07 07:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
static void downmix_to_mono(float * buffer, int channels, size_t count)
|
2021-12-26 06:32:43 +00:00
|
|
|
{
|
|
|
|
if (channels >= 3 && channels <= 8)
|
|
|
|
{
|
|
|
|
downmix_to_stereo(buffer, channels, count);
|
|
|
|
channels = 2;
|
|
|
|
}
|
|
|
|
float invchannels = 1.0 / (float)channels;
|
2022-01-11 12:00:34 +00:00
|
|
|
for (size_t i = 0; i < count; ++i)
|
2021-12-26 06:32:43 +00:00
|
|
|
{
|
|
|
|
float sample = 0;
|
|
|
|
for (int j = 0; j < channels; ++j)
|
|
|
|
{
|
|
|
|
sample += buffer[i * channels + j];
|
|
|
|
}
|
|
|
|
buffer[i] = sample * invchannels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
static void upmix(float * buffer, int inchannels, int outchannels, size_t count)
|
2021-12-26 06:32:43 +00:00
|
|
|
{
|
2022-01-11 12:00:34 +00:00
|
|
|
for (ssize_t i = count - 1; i >= 0; --i)
|
2021-12-26 06:32:43 +00:00
|
|
|
{
|
2021-12-28 08:09:57 +00:00
|
|
|
if (inchannels == 1 && outchannels == 2)
|
2021-12-26 06:32:43 +00:00
|
|
|
{
|
2021-12-28 08:09:57 +00:00
|
|
|
// upmix mono to stereo
|
2021-12-26 06:32:43 +00:00
|
|
|
float sample = buffer[i];
|
2021-12-28 08:09:57 +00:00
|
|
|
buffer[i * 2 + 0] = sample;
|
|
|
|
buffer[i * 2 + 1] = sample;
|
|
|
|
}
|
|
|
|
else if (inchannels == 1 && outchannels == 4)
|
|
|
|
{
|
|
|
|
// upmix mono to quad
|
|
|
|
float sample = buffer[i];
|
|
|
|
buffer[i * 4 + 0] = sample;
|
|
|
|
buffer[i * 4 + 1] = sample;
|
|
|
|
buffer[i * 4 + 2] = 0;
|
|
|
|
buffer[i * 4 + 3] = 0;
|
|
|
|
}
|
|
|
|
else if (inchannels == 1 && (outchannels == 3 || outchannels >= 5))
|
|
|
|
{
|
|
|
|
// upmix mono to center channel
|
|
|
|
float sample = buffer[i];
|
|
|
|
buffer[i * outchannels + 2] = sample;
|
2021-12-26 06:32:43 +00:00
|
|
|
for (int j = 0; j < 2; ++j)
|
|
|
|
{
|
2021-12-28 08:09:57 +00:00
|
|
|
buffer[i * outchannels + j] = 0;
|
2021-12-26 06:32:43 +00:00
|
|
|
}
|
2021-12-28 08:09:57 +00:00
|
|
|
for (int j = 3; j < outchannels; ++j)
|
2021-12-26 06:32:43 +00:00
|
|
|
{
|
|
|
|
buffer[i * outchannels + j] = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-12-28 23:39:23 +00:00
|
|
|
else if (inchannels == 4 && outchannels >= 5)
|
|
|
|
{
|
|
|
|
float fl = buffer[i * 4 + 0];
|
|
|
|
float fr = buffer[i * 4 + 1];
|
|
|
|
float bl = buffer[i * 4 + 2];
|
|
|
|
float br = buffer[i * 4 + 3];
|
|
|
|
const int skipclfe = (outchannels == 5) ? 1 : 2;
|
|
|
|
buffer[i * outchannels + 0] = fl;
|
|
|
|
buffer[i * outchannels + 1] = fr;
|
|
|
|
buffer[i * outchannels + skipclfe + 2] = bl;
|
|
|
|
buffer[i * outchannels + skipclfe + 3] = br;
|
|
|
|
for (int j = 0; j < skipclfe; ++j)
|
|
|
|
{
|
|
|
|
buffer[i * outchannels + 2 + j] = 0;
|
|
|
|
}
|
|
|
|
for (int j = 4 + skipclfe; j < outchannels; ++j)
|
|
|
|
{
|
|
|
|
buffer[i * outchannels + j] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (inchannels == 5 && outchannels >= 6)
|
|
|
|
{
|
|
|
|
float fl = buffer[i * 5 + 0];
|
|
|
|
float fr = buffer[i * 5 + 1];
|
|
|
|
float c = buffer[i * 5 + 2];
|
|
|
|
float bl = buffer[i * 5 + 3];
|
|
|
|
float br = buffer[i * 5 + 4];
|
|
|
|
buffer[i * outchannels + 0] = fl;
|
|
|
|
buffer[i * outchannels + 1] = fr;
|
|
|
|
buffer[i * outchannels + 2] = c;
|
|
|
|
buffer[i * outchannels + 3] = 0;
|
|
|
|
buffer[i * outchannels + 4] = bl;
|
|
|
|
buffer[i * outchannels + 5] = br;
|
|
|
|
for (int j = 6; j < outchannels; ++j)
|
|
|
|
{
|
|
|
|
buffer[i * outchannels + j] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (inchannels == 7 && outchannels == 8)
|
|
|
|
{
|
|
|
|
float fl = buffer[i * 7 + 0];
|
|
|
|
float fr = buffer[i * 7 + 1];
|
|
|
|
float c = buffer[i * 7 + 2];
|
|
|
|
float lfe = buffer[i * 7 + 3];
|
|
|
|
float sl = buffer[i * 7 + 4];
|
|
|
|
float sr = buffer[i * 7 + 5];
|
|
|
|
float bc = buffer[i * 7 + 6];
|
|
|
|
buffer[i * 8 + 0] = fl;
|
|
|
|
buffer[i * 8 + 1] = fr;
|
|
|
|
buffer[i * 8 + 2] = c;
|
|
|
|
buffer[i * 8 + 3] = lfe;
|
|
|
|
buffer[i * 8 + 4] = bc;
|
|
|
|
buffer[i * 8 + 5] = bc;
|
|
|
|
buffer[i * 8 + 6] = sl;
|
|
|
|
buffer[i * 8 + 7] = sr;
|
|
|
|
}
|
2021-12-26 06:32:43 +00:00
|
|
|
else
|
|
|
|
{
|
2021-12-28 08:09:57 +00:00
|
|
|
// upmix N channels to N channels plus silence the empty channels
|
2021-12-26 06:32:43 +00:00
|
|
|
float samples[inchannels];
|
|
|
|
for (int j = 0; j < inchannels; ++j)
|
|
|
|
{
|
|
|
|
samples[j] = buffer[i * inchannels + j];
|
|
|
|
}
|
|
|
|
for (int j = 0; j < inchannels; ++j)
|
|
|
|
{
|
|
|
|
buffer[i * outchannels + j] = samples[j];
|
|
|
|
}
|
|
|
|
for (int j = inchannels; j < outchannels; ++j)
|
|
|
|
{
|
|
|
|
buffer[i * outchannels + j] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 15:06:40 +00:00
|
|
|
void scale_by_volume(float * buffer, size_t count, float volume)
|
2013-10-07 10:59:04 +00:00
|
|
|
{
|
|
|
|
if ( volume != 1.0 )
|
2022-01-16 08:41:54 +00:00
|
|
|
{
|
|
|
|
#if TARGET_CPU_X86 || TARGET_CPU_X86_64
|
|
|
|
if ( count >= 8 )
|
|
|
|
{
|
|
|
|
__m128 vgf = _mm_set1_ps(volume);
|
|
|
|
while ( count >= 8 )
|
|
|
|
{
|
|
|
|
__m128 input = _mm_loadu_ps(buffer);
|
|
|
|
__m128 input2 = _mm_loadu_ps(buffer + 4);
|
|
|
|
__m128 output = _mm_mul_ps(input, vgf);
|
|
|
|
__m128 output2 = _mm_mul_ps(input2, vgf);
|
|
|
|
|
|
|
|
_mm_storeu_ps(buffer + 0, output);
|
|
|
|
_mm_storeu_ps(buffer + 4, output2);
|
|
|
|
|
|
|
|
buffer += 8;
|
|
|
|
count -= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif TARGET_CPU_ARM || TARGET_CPU_ARM64
|
|
|
|
if ( count >= 8 )
|
|
|
|
{
|
|
|
|
float32x4_t vgf = vdupq_n_f32(volume);
|
|
|
|
while ( count >= 8 )
|
|
|
|
{
|
|
|
|
float32x4x2_t oreg;
|
|
|
|
float32x4x2_t inreg = vld1q_f32_x2(buffer);
|
|
|
|
oreg.val[0] = vmulq_f32(inreg.val[0], vgf);
|
|
|
|
oreg.val[1] = vmulq_f32(inreg.val[1], vgf);
|
|
|
|
vst1q_f32_x2(buffer, oreg);
|
|
|
|
buffer += 8;
|
|
|
|
count -= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-12 01:09:06 +00:00
|
|
|
for (size_t i = 0; i < count; ++i)
|
2013-10-07 10:59:04 +00:00
|
|
|
buffer[i] *= volume;
|
2022-01-16 08:41:54 +00:00
|
|
|
}
|
2013-10-07 10:59:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 14:12:14 +00:00
|
|
|
/**
|
|
|
|
* DSD 2 PCM: Stage 1:
|
|
|
|
* Decimate by factor 8
|
|
|
|
* (one byte (8 samples) -> one float sample)
|
|
|
|
* The bits are processed from least signicifant to most signicicant.
|
|
|
|
* @author Sebastian Gesemann
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define dsd2pcm_FILTER_COEFFS_COUNT 64
|
|
|
|
static const float dsd2pcm_FILTER_COEFFS[64] =
|
|
|
|
{
|
|
|
|
0.09712411121659f, 0.09613438994044f, 0.09417884216316f, 0.09130441727307f,
|
|
|
|
0.08757947648990f, 0.08309142055179f, 0.07794369263673f, 0.07225228745463f,
|
|
|
|
0.06614191680338f, 0.05974199351302f, 0.05318259916599f, 0.04659059631228f,
|
|
|
|
0.04008603356890f, 0.03377897290478f, 0.02776684382775f, 0.02213240062966f,
|
|
|
|
0.01694232798846f, 0.01224650881275f, 0.00807793792573f, 0.00445323755944f,
|
|
|
|
0.00137370697215f,-0.00117318019994f,-0.00321193033831f,-0.00477694265140f,
|
|
|
|
-0.00591028841335f,-0.00665946056286f,-0.00707518873201f,-0.00720940203988f,
|
|
|
|
-0.00711340642819f,-0.00683632603227f,-0.00642384017266f,-0.00591723006715f,
|
|
|
|
-0.00535273320457f,-0.00476118922548f,-0.00416794965654f,-0.00359301524813f,
|
|
|
|
-0.00305135909510f,-0.00255339111833f,-0.00210551956895f,-0.00171076760278f,
|
|
|
|
-0.00136940723130f,-0.00107957856005f,-0.00083786862365f,-0.00063983084245f,
|
|
|
|
-0.00048043272086f,-0.00035442550015f,-0.00025663481039f,-0.00018217573430f,
|
|
|
|
-0.00012659899635f,-0.00008597726991f,-0.00005694188820f,-0.00003668060332f,
|
|
|
|
-0.00002290670286f,-0.00001380895679f,-0.00000799057558f,-0.00000440385083f,
|
|
|
|
-0.00000228567089f,-0.00000109760778f,-0.00000047286430f,-0.00000017129652f,
|
|
|
|
-0.00000004282776f, 0.00000000119422f, 0.00000000949179f, 0.00000000747450f
|
|
|
|
};
|
|
|
|
|
|
|
|
struct dsd2pcm_state {
|
|
|
|
/*
|
|
|
|
* This is the 2nd half of an even order symmetric FIR
|
|
|
|
* lowpass filter (to be used on a signal sampled at 44100*64 Hz)
|
|
|
|
* Passband is 0-24 kHz (ripples +/- 0.025 dB)
|
|
|
|
* Stopband starts at 176.4 kHz (rejection: 170 dB)
|
|
|
|
* The overall gain is 2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* These remain constant for the duration */
|
|
|
|
int FILT_LOOKUP_PARTS;
|
|
|
|
float * FILT_LOOKUP_TABLE;
|
|
|
|
uint8_t * REVERSE_BITS;
|
|
|
|
int FIFO_LENGTH;
|
|
|
|
int FIFO_OFS_MASK;
|
|
|
|
|
|
|
|
/* These are altered */
|
|
|
|
int * fifo;
|
|
|
|
int fpos;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void dsd2pcm_free(void *);
|
|
|
|
static void dsd2pcm_reset(void *);
|
|
|
|
|
|
|
|
static void * dsd2pcm_alloc()
|
|
|
|
{
|
|
|
|
struct dsd2pcm_state * state = (struct dsd2pcm_state *) calloc(1, sizeof(struct dsd2pcm_state));
|
|
|
|
|
|
|
|
if (!state)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
state->FILT_LOOKUP_PARTS = ( dsd2pcm_FILTER_COEFFS_COUNT + 7 ) / 8;
|
|
|
|
const int FILT_LOOKUP_PARTS = state->FILT_LOOKUP_PARTS;
|
|
|
|
// The current 128 tap FIR leads to an 8 KB lookup table
|
|
|
|
state->FILT_LOOKUP_TABLE = (float*) calloc(sizeof(float), FILT_LOOKUP_PARTS << 8);
|
|
|
|
if (!state->FILT_LOOKUP_TABLE)
|
|
|
|
goto fail;
|
|
|
|
float* FILT_LOOKUP_TABLE = state->FILT_LOOKUP_TABLE;
|
|
|
|
double * temp = (double*) calloc(sizeof(double), 0x100);
|
|
|
|
if (!temp)
|
|
|
|
goto fail;
|
|
|
|
for ( int part=0, sofs=0, dofs=0; part < FILT_LOOKUP_PARTS; )
|
|
|
|
{
|
|
|
|
memset( temp, 0, 0x100 * sizeof( double ) );
|
|
|
|
for ( int bit=0, bitmask=0x80; bit<8 && sofs+bit < dsd2pcm_FILTER_COEFFS_COUNT; )
|
|
|
|
{
|
|
|
|
double coeff = dsd2pcm_FILTER_COEFFS[ sofs + bit ];
|
|
|
|
for ( int bite=0; bite < 0x100; bite++ )
|
|
|
|
{
|
|
|
|
if ( ( bite & bitmask ) == 0 )
|
|
|
|
{
|
|
|
|
temp[ bite ] -= coeff;
|
|
|
|
} else {
|
|
|
|
temp[ bite ] += coeff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bit++;
|
|
|
|
bitmask >>= 1;
|
|
|
|
}
|
|
|
|
for ( int s = 0; s < 0x100; ) {
|
|
|
|
FILT_LOOKUP_TABLE[dofs++] = (float) temp[s++];
|
|
|
|
}
|
|
|
|
part++;
|
|
|
|
sofs += 8;
|
|
|
|
}
|
|
|
|
free(temp);
|
|
|
|
{ // calculate FIFO stuff
|
|
|
|
int k = 1;
|
|
|
|
while (k<FILT_LOOKUP_PARTS*2) k<<=1;
|
|
|
|
state->FIFO_LENGTH = k;
|
|
|
|
state->FIFO_OFS_MASK = k-1;
|
|
|
|
}
|
|
|
|
state->REVERSE_BITS = (uint8_t*) calloc(1, 0x100);
|
|
|
|
if (!state->REVERSE_BITS)
|
|
|
|
goto fail;
|
|
|
|
uint8_t* REVERSE_BITS = state->REVERSE_BITS;
|
|
|
|
for (int i=0, j=0; i<0x100; i++) {
|
|
|
|
REVERSE_BITS[i] = ( uint8_t ) j;
|
|
|
|
// "reverse-increment" of j
|
|
|
|
for (int bitmask=0x80;;) {
|
|
|
|
if (((j^=bitmask) & bitmask)!=0) break;
|
|
|
|
if (bitmask==1) break;
|
|
|
|
bitmask >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state->fifo = (int*) calloc(sizeof(int), state->FIFO_LENGTH);
|
|
|
|
if (!state->fifo)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
dsd2pcm_reset(state);
|
|
|
|
|
|
|
|
return (void*) state;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
dsd2pcm_free(state);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void * dsd2pcm_dup(void * _state)
|
|
|
|
{
|
|
|
|
struct dsd2pcm_state * state = (struct dsd2pcm_state *) _state;
|
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
struct dsd2pcm_state * newstate = (struct dsd2pcm_state *) calloc(1, sizeof(struct dsd2pcm_state));
|
|
|
|
if (newstate) {
|
|
|
|
newstate->FILT_LOOKUP_PARTS = state->FILT_LOOKUP_PARTS;
|
|
|
|
newstate->FIFO_LENGTH = state->FIFO_LENGTH;
|
|
|
|
newstate->FIFO_OFS_MASK = state->FIFO_OFS_MASK;
|
|
|
|
newstate->fpos = state->fpos;
|
|
|
|
|
|
|
|
newstate->FILT_LOOKUP_TABLE = (float*) calloc(sizeof(float), state->FILT_LOOKUP_PARTS << 8);
|
|
|
|
if (!newstate->FILT_LOOKUP_TABLE)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
memcpy(newstate->FILT_LOOKUP_TABLE, state->FILT_LOOKUP_TABLE, sizeof(float) * (state->FILT_LOOKUP_PARTS << 8));
|
|
|
|
|
|
|
|
newstate->REVERSE_BITS = (uint8_t*) calloc(1, 0x100);
|
|
|
|
if (!newstate->REVERSE_BITS)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
memcpy(newstate->REVERSE_BITS, state->REVERSE_BITS, 0x100);
|
|
|
|
|
|
|
|
newstate->fifo = (int*) calloc(sizeof(int), state->FIFO_LENGTH);
|
|
|
|
if (!newstate->fifo)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
memcpy(newstate->fifo, state->fifo, sizeof(int) * state->FIFO_LENGTH);
|
|
|
|
|
|
|
|
return (void*) newstate;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
dsd2pcm_free(newstate);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dsd2pcm_free(void * _state)
|
|
|
|
{
|
|
|
|
struct dsd2pcm_state * state = (struct dsd2pcm_state *) _state;
|
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
free(state->fifo);
|
|
|
|
free(state->REVERSE_BITS);
|
|
|
|
free(state->FILT_LOOKUP_TABLE);
|
|
|
|
free(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dsd2pcm_reset(void * _state)
|
|
|
|
{
|
|
|
|
struct dsd2pcm_state * state = (struct dsd2pcm_state *) _state;
|
|
|
|
const int FILT_LOOKUP_PARTS = state->FILT_LOOKUP_PARTS;
|
|
|
|
int* fifo = state->fifo;
|
|
|
|
for (int i=0; i<FILT_LOOKUP_PARTS; i++) {
|
|
|
|
fifo[i] = 0x55;
|
|
|
|
fifo[i+FILT_LOOKUP_PARTS] = 0xAA;
|
|
|
|
}
|
|
|
|
state->fpos = FILT_LOOKUP_PARTS;
|
|
|
|
}
|
|
|
|
|
2022-01-19 08:27:59 +00:00
|
|
|
static int dsd2pcm_latency(void * _state)
|
|
|
|
{
|
|
|
|
struct dsd2pcm_state * state = (struct dsd2pcm_state *) _state;
|
|
|
|
if (state) return state->FIFO_LENGTH;
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-14 14:12:14 +00:00
|
|
|
static void dsd2pcm_process(void * _state, uint8_t * src, size_t sofs, size_t sinc, float * dest, size_t dofs, size_t dinc, size_t len)
|
|
|
|
{
|
|
|
|
struct dsd2pcm_state * state = (struct dsd2pcm_state *) _state;
|
|
|
|
int bite1, bite2, temp;
|
|
|
|
float sample;
|
|
|
|
int* fifo = state->fifo;
|
|
|
|
const uint8_t* REVERSE_BITS = state->REVERSE_BITS;
|
|
|
|
const float* FILT_LOOKUP_TABLE = state->FILT_LOOKUP_TABLE;
|
|
|
|
const int FILT_LOOKUP_PARTS = state->FILT_LOOKUP_PARTS;
|
|
|
|
const int FIFO_OFS_MASK = state->FIFO_OFS_MASK;
|
|
|
|
int fpos = state->fpos;
|
|
|
|
while ( len > 0 )
|
|
|
|
{
|
|
|
|
fifo[ fpos ] = REVERSE_BITS[ fifo[ fpos ] ] & 0xFF;
|
|
|
|
fifo[ ( fpos + FILT_LOOKUP_PARTS ) & FIFO_OFS_MASK ] = src[ sofs ] & 0xFF;
|
|
|
|
sofs += sinc;
|
|
|
|
temp = ( fpos + 1 ) & FIFO_OFS_MASK;
|
|
|
|
sample = 0;
|
|
|
|
for ( int k=0, lofs=0; k < FILT_LOOKUP_PARTS; )
|
|
|
|
{
|
|
|
|
bite1 = fifo[ ( fpos - k ) & FIFO_OFS_MASK ];
|
|
|
|
bite2 = fifo[ ( temp + k ) & FIFO_OFS_MASK ];
|
|
|
|
sample += FILT_LOOKUP_TABLE[ lofs + bite1 ] + FILT_LOOKUP_TABLE[ lofs + bite2 ];
|
|
|
|
k++;
|
|
|
|
lofs += 0x100;
|
|
|
|
}
|
|
|
|
fpos = temp;
|
|
|
|
dest[ dofs ] = sample;
|
|
|
|
dofs += dinc;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
state->fpos = fpos;
|
|
|
|
}
|
|
|
|
|
2022-01-19 10:08:57 +00:00
|
|
|
static void convert_dsd_to_f32(float *output, const uint8_t *input, size_t count, size_t channels, void ** dsd2pcm)
|
2022-01-14 14:12:14 +00:00
|
|
|
{
|
|
|
|
for (size_t channel = 0; channel < channels; ++channel)
|
|
|
|
{
|
|
|
|
dsd2pcm_process(dsd2pcm[channel], input, channel, channels, output, channel, channels, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 10:08:57 +00:00
|
|
|
static void convert_u8_to_s16(int16_t *output, const uint8_t *input, size_t count)
|
2007-10-03 20:23:14 +00:00
|
|
|
{
|
2022-01-12 01:09:06 +00:00
|
|
|
for (size_t i = 0; i < count; ++i)
|
2022-01-11 12:00:34 +00:00
|
|
|
{
|
|
|
|
uint16_t sample = (input[i] << 8) | input[i];
|
|
|
|
sample ^= 0x8080;
|
|
|
|
output[i] = (int16_t)(sample);
|
|
|
|
}
|
|
|
|
}
|
2007-10-03 20:23:14 +00:00
|
|
|
|
2022-01-19 10:08:57 +00:00
|
|
|
static void convert_s8_to_s16(int16_t *output, const uint8_t *input, size_t count)
|
2022-01-11 12:00:34 +00:00
|
|
|
{
|
2022-01-12 01:09:06 +00:00
|
|
|
for (size_t i = 0; i < count; ++i)
|
2022-01-11 12:00:34 +00:00
|
|
|
{
|
|
|
|
uint16_t sample = (input[i] << 8) | input[i];
|
|
|
|
output[i] = (int16_t)(sample);
|
|
|
|
}
|
|
|
|
}
|
2021-12-26 07:41:45 +00:00
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
static void convert_u16_to_s16(int16_t *buffer, size_t count)
|
|
|
|
{
|
2022-01-12 01:09:06 +00:00
|
|
|
for (size_t i = 0; i < count; ++i)
|
2022-01-11 12:00:34 +00:00
|
|
|
{
|
|
|
|
buffer[i] ^= 0x8000;
|
|
|
|
}
|
2007-10-03 20:23:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 10:08:57 +00:00
|
|
|
static void convert_s16_to_hdcd_input(int32_t *output, const int16_t *input, size_t count)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
output[i] = input[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void convert_s24_to_s32(int32_t *output, const uint8_t *input, size_t count)
|
2013-10-07 07:24:26 +00:00
|
|
|
{
|
2022-01-12 01:09:06 +00:00
|
|
|
for (size_t i = 0; i < count; ++i)
|
2022-01-11 12:00:34 +00:00
|
|
|
{
|
|
|
|
int32_t sample = (input[i * 3] << 8) | (input[i * 3 + 1] << 16) | (input[i * 3 + 2] << 24);
|
|
|
|
output[i] = sample;
|
|
|
|
}
|
|
|
|
}
|
2013-10-07 07:24:26 +00:00
|
|
|
|
2022-01-19 10:08:57 +00:00
|
|
|
static void convert_u24_to_s32(int32_t *output, const uint8_t *input, size_t count)
|
2022-01-11 12:00:34 +00:00
|
|
|
{
|
2022-01-12 01:09:06 +00:00
|
|
|
for (size_t i = 0; i < count; ++i)
|
2021-12-25 23:02:13 +00:00
|
|
|
{
|
2022-01-11 12:00:34 +00:00
|
|
|
int32_t sample = (input[i * 3] << 8) | (input[i * 3 + 1] << 16) | (input[i * 3 + 2] << 24);
|
|
|
|
output[i] = sample ^ 0x80000000;
|
2021-12-25 23:02:13 +00:00
|
|
|
}
|
2022-01-11 12:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void convert_u32_to_s32(int32_t *buffer, size_t count)
|
|
|
|
{
|
2022-01-12 01:09:06 +00:00
|
|
|
for (size_t i = 0; i < count; ++i)
|
2021-12-26 07:41:45 +00:00
|
|
|
{
|
2022-01-11 12:00:34 +00:00
|
|
|
buffer[i] ^= 0x80000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 10:08:57 +00:00
|
|
|
static void convert_f64_to_f32(float *output, const double *input, size_t count)
|
2022-01-12 01:09:06 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
output[i] = (float)(input[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
static void convert_be_to_le(uint8_t *buffer, size_t bitsPerSample, size_t bytes)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
uint8_t temp;
|
|
|
|
bitsPerSample = (bitsPerSample + 7) / 8;
|
|
|
|
switch (bitsPerSample) {
|
|
|
|
case 2:
|
|
|
|
for (i = 0; i < bytes; i += 2)
|
|
|
|
{
|
|
|
|
temp = buffer[1];
|
|
|
|
buffer[1] = buffer[0];
|
|
|
|
buffer[0] = temp;
|
|
|
|
buffer += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
for (i = 0; i < bytes; i += 3)
|
|
|
|
{
|
|
|
|
temp = buffer[2];
|
|
|
|
buffer[2] = buffer[0];
|
|
|
|
buffer[0] = temp;
|
|
|
|
buffer += 3;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
for (i = 0; i < bytes; i += 4)
|
|
|
|
{
|
|
|
|
temp = buffer[3];
|
|
|
|
buffer[3] = buffer[0];
|
|
|
|
buffer[0] = temp;
|
|
|
|
temp = buffer[2];
|
|
|
|
buffer[2] = buffer[1];
|
|
|
|
buffer[1] = temp;
|
|
|
|
buffer += 4;
|
|
|
|
}
|
|
|
|
break;
|
2022-01-12 01:09:06 +00:00
|
|
|
|
|
|
|
case 8:
|
|
|
|
for (i = 0; i < bytes; i += 8)
|
|
|
|
{
|
|
|
|
temp = buffer[7];
|
|
|
|
buffer[7] = buffer[0];
|
|
|
|
buffer[0] = temp;
|
|
|
|
temp = buffer[6];
|
|
|
|
buffer[6] = buffer[1];
|
|
|
|
buffer[1] = temp;
|
|
|
|
temp = buffer[5];
|
|
|
|
buffer[5] = buffer[2];
|
|
|
|
buffer[2] = temp;
|
|
|
|
temp = buffer[4];
|
|
|
|
buffer[4] = buffer[3];
|
|
|
|
buffer[3] = temp;
|
|
|
|
buffer += 8;
|
|
|
|
}
|
|
|
|
break;
|
2021-12-26 07:41:45 +00:00
|
|
|
}
|
2013-10-07 07:24:26 +00:00
|
|
|
}
|
|
|
|
|
2007-10-03 20:23:14 +00:00
|
|
|
-(void)process
|
|
|
|
{
|
2007-10-20 03:24:27 +00:00
|
|
|
char writeBuf[CHUNK_SIZE];
|
2007-10-03 20:23:14 +00:00
|
|
|
|
2022-01-12 11:36:27 +00:00
|
|
|
// 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)
|
2007-10-03 20:23:14 +00:00
|
|
|
{
|
2007-10-20 03:24:27 +00:00
|
|
|
int amountConverted = [self convert:writeBuf amount:CHUNK_SIZE];
|
2022-01-12 10:55:11 +00:00
|
|
|
if (!amountConverted)
|
|
|
|
{
|
2022-01-13 07:13:00 +00:00
|
|
|
if (paused)
|
2022-01-12 10:55:11 +00:00
|
|
|
{
|
2022-01-13 07:13:00 +00:00
|
|
|
while (paused)
|
|
|
|
usleep(500);
|
|
|
|
continue;
|
2022-01-12 10:55:11 +00:00
|
|
|
}
|
2022-01-14 03:43:18 +00:00
|
|
|
else if (refillNode)
|
|
|
|
{
|
|
|
|
// refill node just ended, file resumes
|
|
|
|
[self setPreviousNode:originalPreviousNode];
|
|
|
|
[self setEndOfStream:NO];
|
|
|
|
[self setShouldContinue:YES];
|
|
|
|
refillNode = nil;
|
|
|
|
[self cleanUp];
|
2022-01-22 06:47:11 +00:00
|
|
|
[self setupWithInputFormat:rememberedInputFormat outputFormat:outputFormat isLossless:rememberedLossless];
|
2022-01-14 03:43:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-01-12 10:55:11 +00:00
|
|
|
else break;
|
|
|
|
}
|
2007-10-03 20:23:14 +00:00
|
|
|
[self writeData:writeBuf amount:amountConverted];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int)convert:(void *)dest amount:(int)amount
|
|
|
|
{
|
2021-12-25 23:02:13 +00:00
|
|
|
UInt32 ioNumberPackets;
|
|
|
|
int amountReadFromFC;
|
2013-10-07 07:24:26 +00:00
|
|
|
int amountRead = 0;
|
2022-01-19 08:27:59 +00:00
|
|
|
int amountToSkip;
|
2022-01-12 02:19:30 +00:00
|
|
|
|
2021-12-26 07:41:45 +00:00
|
|
|
if (stopping)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
convertEntered = YES;
|
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
tryagain:
|
2021-12-28 01:04:16 +00:00
|
|
|
if (stopping || [self shouldContinue] == NO)
|
2021-12-26 06:47:17 +00:00
|
|
|
{
|
2021-12-26 07:41:45 +00:00
|
|
|
convertEntered = NO;
|
2021-12-26 06:47:17 +00:00
|
|
|
return amountRead;
|
|
|
|
}
|
|
|
|
|
2021-12-25 23:02:13 +00:00
|
|
|
amountReadFromFC = 0;
|
2022-01-11 12:00:34 +00:00
|
|
|
|
2022-01-11 15:06:40 +00:00
|
|
|
if (floatOffset == floatSize) // skip this step if there's still float buffered
|
2022-01-11 12:00:34 +00:00
|
|
|
while (inpOffset == inpSize) {
|
|
|
|
size_t samplesRead = 0;
|
|
|
|
|
2022-01-14 14:12:14 +00:00
|
|
|
BOOL isFloat = !!(inputFormat.mFormatFlags & kAudioFormatFlagIsFloat);
|
|
|
|
BOOL isUnsigned = !isFloat && !(inputFormat.mFormatFlags & kAudioFormatFlagIsSignedInteger);
|
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
// Approximately the most we want on input
|
|
|
|
ioNumberPackets = (amount - amountRead) / outputFormat.mBytesPerPacket;
|
2022-01-19 08:27:59 +00:00
|
|
|
if (!skipResampler && ioNumberPackets < PRIME_LEN_)
|
|
|
|
ioNumberPackets = PRIME_LEN_;
|
2022-01-11 12:00:34 +00:00
|
|
|
|
2022-01-14 10:00:32 +00:00
|
|
|
// We want to upscale this count if the ratio is below zero
|
|
|
|
if (sampleRatio < 1.0)
|
|
|
|
{
|
|
|
|
ioNumberPackets = ((uint32_t)(ioNumberPackets / sampleRatio) + 15) & ~15;
|
|
|
|
}
|
|
|
|
|
2022-01-19 08:27:59 +00:00
|
|
|
amountToSkip = 0;
|
|
|
|
if (dsd2pcm && !is_preextrapolated_) {
|
|
|
|
amountToSkip = dsd2pcmLatency * inputFormat.mBytesPerPacket;
|
|
|
|
ioNumberPackets += dsd2pcmLatency;
|
|
|
|
}
|
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
size_t newSize = ioNumberPackets * floatFormat.mBytesPerPacket;
|
|
|
|
if (!inputBuffer || inputBufferSize < newSize)
|
|
|
|
inputBuffer = realloc( inputBuffer, inputBufferSize = newSize * 3 );
|
|
|
|
|
2022-01-14 14:12:14 +00:00
|
|
|
ssize_t amountToWrite = ioNumberPackets * inputFormat.mBytesPerPacket;
|
2022-01-19 08:27:59 +00:00
|
|
|
amountToWrite -= amountToSkip;
|
2021-12-25 23:02:13 +00:00
|
|
|
|
2022-01-14 14:12:14 +00:00
|
|
|
ssize_t bytesReadFromInput = 0;
|
2022-01-13 07:13:00 +00:00
|
|
|
|
|
|
|
while (bytesReadFromInput < amountToWrite && !stopping && [self shouldContinue] == YES && [self endOfStream] == NO)
|
2022-01-12 02:19:30 +00:00
|
|
|
{
|
2022-01-13 07:13:00 +00:00
|
|
|
size_t bytesRead = [self readData:inputBuffer + amountToSkip + bytesReadFromInput amount:(int)(amountToWrite - bytesReadFromInput)];
|
|
|
|
bytesReadFromInput += bytesRead;
|
|
|
|
if (!bytesRead)
|
2022-01-14 03:43:18 +00:00
|
|
|
{
|
|
|
|
if (refillNode)
|
|
|
|
[self setEndOfStream:YES];
|
|
|
|
else
|
|
|
|
usleep(500);
|
|
|
|
}
|
2022-01-12 02:19:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 14:12:14 +00:00
|
|
|
// Pad end of track with input format silence
|
2022-01-12 01:09:06 +00:00
|
|
|
|
2022-01-14 14:12:14 +00:00
|
|
|
if (stopping || [self shouldContinue] == NO || [self endOfStream] == YES)
|
|
|
|
{
|
2022-01-19 08:27:59 +00:00
|
|
|
if (!skipResampler && !is_postextrapolated_)
|
2022-01-14 14:12:14 +00:00
|
|
|
{
|
2022-01-19 08:27:59 +00:00
|
|
|
if (dsd2pcm) {
|
|
|
|
amountToSkip = dsd2pcmLatency * inputFormat.mBytesPerPacket;
|
|
|
|
memset(inputBuffer + bytesReadFromInput, 0x55, amountToSkip);
|
|
|
|
bytesReadFromInput += amountToSkip;
|
|
|
|
amountToSkip = 0;
|
2022-01-14 14:12:14 +00:00
|
|
|
}
|
2022-01-19 08:27:59 +00:00
|
|
|
is_postextrapolated_ = 1;
|
|
|
|
}
|
|
|
|
else if (!is_postextrapolated_ && dsd2pcm) {
|
|
|
|
is_postextrapolated_ = 3;
|
2022-01-14 14:12:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bytesReadFromInput) {
|
|
|
|
convertEntered = NO;
|
|
|
|
return amountRead;
|
|
|
|
}
|
2022-01-19 08:27:59 +00:00
|
|
|
|
2022-01-14 14:12:14 +00:00
|
|
|
bytesReadFromInput += amountToSkip;
|
2022-01-19 08:27:59 +00:00
|
|
|
|
|
|
|
if (dsd2pcm && amountToSkip) {
|
|
|
|
memset(inputBuffer, 0x55, amountToSkip);
|
|
|
|
dsdLatencyEaten = (int)ceil(dsd2pcmLatency * sampleRatio);
|
|
|
|
}
|
2022-01-14 14:12:14 +00:00
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
if (bytesReadFromInput &&
|
|
|
|
(inputFormat.mFormatFlags & kAudioFormatFlagIsBigEndian))
|
|
|
|
{
|
|
|
|
// Time for endian swap!
|
|
|
|
convert_be_to_le(inputBuffer, inputFormat.mBitsPerChannel, bytesReadFromInput);
|
|
|
|
}
|
|
|
|
|
2022-01-12 01:09:06 +00:00
|
|
|
if (bytesReadFromInput && isFloat && inputFormat.mBitsPerChannel == 64)
|
|
|
|
{
|
|
|
|
// Time for precision loss from weird inputs
|
|
|
|
samplesRead = bytesReadFromInput / sizeof(double);
|
|
|
|
convert_f64_to_f32(inputBuffer + bytesReadFromInput, inputBuffer, samplesRead);
|
|
|
|
memmove(inputBuffer, inputBuffer + bytesReadFromInput, samplesRead * sizeof(float));
|
|
|
|
bytesReadFromInput = samplesRead * sizeof(float);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytesReadFromInput && !isFloat)
|
2022-01-11 12:00:34 +00:00
|
|
|
{
|
2022-01-19 10:08:57 +00:00
|
|
|
float gain = 1.0;
|
2022-01-11 12:00:34 +00:00
|
|
|
size_t bitsPerSample = inputFormat.mBitsPerChannel;
|
2022-01-14 14:12:14 +00:00
|
|
|
if (bitsPerSample == 1) {
|
2022-01-14 15:39:55 +00:00
|
|
|
samplesRead = bytesReadFromInput / inputFormat.mBytesPerPacket;
|
2022-01-14 14:12:14 +00:00
|
|
|
convert_dsd_to_f32(inputBuffer + bytesReadFromInput, inputBuffer, samplesRead, inputFormat.mChannelsPerFrame, dsd2pcm);
|
|
|
|
memmove(inputBuffer, inputBuffer + bytesReadFromInput, samplesRead * inputFormat.mChannelsPerFrame * sizeof(float));
|
|
|
|
bitsPerSample = 32;
|
|
|
|
bytesReadFromInput = samplesRead * inputFormat.mChannelsPerFrame * sizeof(float);
|
|
|
|
isFloat = YES;
|
|
|
|
}
|
|
|
|
else if (bitsPerSample <= 8) {
|
2022-01-11 12:00:34 +00:00
|
|
|
samplesRead = bytesReadFromInput;
|
2022-01-12 03:37:47 +00:00
|
|
|
if (!isUnsigned)
|
2022-01-11 12:00:34 +00:00
|
|
|
convert_s8_to_s16(inputBuffer + bytesReadFromInput, inputBuffer, samplesRead);
|
|
|
|
else
|
|
|
|
convert_u8_to_s16(inputBuffer + bytesReadFromInput, inputBuffer, samplesRead);
|
|
|
|
memmove(inputBuffer, inputBuffer + bytesReadFromInput, samplesRead * 2);
|
|
|
|
bitsPerSample = 16;
|
|
|
|
bytesReadFromInput = samplesRead * 2;
|
|
|
|
isUnsigned = NO;
|
|
|
|
}
|
2022-01-19 10:08:57 +00:00
|
|
|
if (hdcd_decoder) { // implied bits per sample is 16, produces 32 bit int scale
|
|
|
|
samplesRead = bytesReadFromInput / 2;
|
|
|
|
if (isUnsigned)
|
|
|
|
convert_u16_to_s16(inputBuffer, samplesRead);
|
|
|
|
convert_s16_to_hdcd_input(inputBuffer + bytesReadFromInput, inputBuffer, samplesRead);
|
|
|
|
memmove(inputBuffer, inputBuffer + bytesReadFromInput, samplesRead * 4);
|
|
|
|
hdcd_process_stereo((hdcd_state_stereo_t *)hdcd_decoder, inputBuffer, (int)(samplesRead / 2));
|
|
|
|
if (((hdcd_state_stereo_t*)hdcd_decoder)->channel[0].sustain &&
|
|
|
|
((hdcd_state_stereo_t*)hdcd_decoder)->channel[1].sustain) {
|
|
|
|
[controller sustainHDCD];
|
|
|
|
}
|
|
|
|
gain = 2.0;
|
|
|
|
bitsPerSample = 32;
|
|
|
|
bytesReadFromInput = samplesRead * 4;
|
|
|
|
isUnsigned = NO;
|
|
|
|
}
|
|
|
|
else if (bitsPerSample <= 16) {
|
2022-01-11 12:00:34 +00:00
|
|
|
samplesRead = bytesReadFromInput / 2;
|
|
|
|
if (isUnsigned)
|
|
|
|
convert_u16_to_s16(inputBuffer, samplesRead);
|
|
|
|
convert_s16_to_float(inputBuffer + bytesReadFromInput, inputBuffer, samplesRead, 1.0);
|
|
|
|
memmove(inputBuffer, inputBuffer + bytesReadFromInput, samplesRead * sizeof(float));
|
|
|
|
bitsPerSample = 32;
|
|
|
|
bytesReadFromInput = samplesRead * sizeof(float);
|
|
|
|
isUnsigned = NO;
|
|
|
|
isFloat = YES;
|
|
|
|
}
|
|
|
|
else if (bitsPerSample <= 24) {
|
|
|
|
samplesRead = bytesReadFromInput / 3;
|
|
|
|
if (isUnsigned)
|
|
|
|
convert_u24_to_s32(inputBuffer + bytesReadFromInput, inputBuffer, samplesRead);
|
|
|
|
else
|
|
|
|
convert_s24_to_s32(inputBuffer + bytesReadFromInput, inputBuffer, samplesRead);
|
|
|
|
memmove(inputBuffer, inputBuffer + bytesReadFromInput, samplesRead * 4);
|
|
|
|
bitsPerSample = 32;
|
|
|
|
bytesReadFromInput = samplesRead * 4;
|
|
|
|
isUnsigned = NO;
|
|
|
|
}
|
|
|
|
if (!isFloat && bitsPerSample <= 32) {
|
|
|
|
samplesRead = bytesReadFromInput / 4;
|
|
|
|
if (isUnsigned)
|
|
|
|
convert_u32_to_s32(inputBuffer, samplesRead);
|
2022-01-19 10:08:57 +00:00
|
|
|
convert_s32_to_float(inputBuffer + bytesReadFromInput, inputBuffer, samplesRead, gain);
|
2022-01-11 12:00:34 +00:00
|
|
|
memmove(inputBuffer, inputBuffer + bytesReadFromInput, samplesRead * sizeof(float));
|
|
|
|
bitsPerSample = 32;
|
|
|
|
bytesReadFromInput = samplesRead * sizeof(float);
|
|
|
|
isUnsigned = NO;
|
|
|
|
isFloat = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 02:19:30 +00:00
|
|
|
// Extrapolate start
|
2022-01-19 08:27:59 +00:00
|
|
|
if (!skipResampler && !is_preextrapolated_)
|
2022-01-13 07:13:00 +00:00
|
|
|
{
|
2022-01-19 08:27:59 +00:00
|
|
|
size_t samples_in_buffer = bytesReadFromInput / floatFormat.mBytesPerPacket;
|
|
|
|
size_t prime = min(samples_in_buffer, PRIME_LEN_);
|
|
|
|
size_t newSize = N_samples_to_add_ * floatFormat.mBytesPerPacket;
|
|
|
|
newSize += bytesReadFromInput;
|
|
|
|
|
|
|
|
if (newSize > inputBufferSize) {
|
|
|
|
inputBuffer = realloc(inputBuffer, inputBufferSize = newSize * 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove(inputBuffer + N_samples_to_add_ * floatFormat.mBytesPerPacket, inputBuffer, bytesReadFromInput);
|
|
|
|
|
|
|
|
// Great padding! And we want to eat more, based on the resampler filter size
|
|
|
|
int samplesLatency = (int)ceil(resampler->latency(resampler_data) * sampleRatio);
|
|
|
|
|
|
|
|
// Guess what? This extrapolates into the memory before its input pointer!
|
|
|
|
lpc_extrapolate_bkwd(inputBuffer + N_samples_to_add_ * floatFormat.mBytesPerPacket, samples_in_buffer, prime, floatFormat.mChannelsPerFrame, LPC_ORDER, N_samples_to_add_, &extrapolateBuffer, &extrapolateBufferSize);
|
|
|
|
bytesReadFromInput += N_samples_to_add_ * floatFormat.mBytesPerPacket;
|
|
|
|
latencyEaten = N_samples_to_drop_ + samplesLatency;
|
|
|
|
if (dsd2pcm) latencyEaten += dsdLatencyEaten;
|
|
|
|
is_preextrapolated_ = 2;
|
|
|
|
}
|
|
|
|
else if (dsd2pcm && !is_preextrapolated_) {
|
|
|
|
latencyEaten = dsd2pcmLatency;
|
|
|
|
is_preextrapolated_ = 3;
|
2022-01-12 02:19:30 +00:00
|
|
|
}
|
2022-01-14 15:39:55 +00:00
|
|
|
|
2022-01-19 08:27:59 +00:00
|
|
|
if (is_postextrapolated_ == 1)
|
2022-01-14 14:12:14 +00:00
|
|
|
{
|
2022-01-19 08:27:59 +00:00
|
|
|
size_t samples_in_buffer = bytesReadFromInput / floatFormat.mBytesPerPacket;
|
|
|
|
size_t prime = min(samples_in_buffer, PRIME_LEN_);
|
|
|
|
|
|
|
|
size_t newSize = bytesReadFromInput;
|
|
|
|
newSize += N_samples_to_add_ * floatFormat.mBytesPerPacket;
|
|
|
|
if (newSize > inputBufferSize) {
|
|
|
|
inputBuffer = realloc(inputBuffer, inputBufferSize = newSize * 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// And now that we've reached the end, we eat slightly less, due to the filter size
|
|
|
|
int samplesLatency = (int)resampler->latency(resampler_data);
|
|
|
|
if (dsd2pcm) samplesLatency += dsd2pcmLatency;
|
|
|
|
samplesLatency = (int)ceil(samplesLatency * sampleRatio);
|
|
|
|
|
|
|
|
lpc_extrapolate_fwd(inputBuffer, samples_in_buffer, prime, floatFormat.mChannelsPerFrame, LPC_ORDER, N_samples_to_add_, &extrapolateBuffer, &extrapolateBufferSize);
|
|
|
|
bytesReadFromInput += N_samples_to_add_ * floatFormat.mBytesPerPacket;
|
|
|
|
latencyEatenPost = N_samples_to_drop_;
|
|
|
|
if (latencyEatenPost > samplesLatency) {
|
|
|
|
latencyEatenPost -= samplesLatency;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
latencyEatenPost = 0;
|
|
|
|
}
|
|
|
|
is_postextrapolated_ = 2;
|
|
|
|
}
|
|
|
|
else if (is_postextrapolated_ == 3) { // skip end of DSD output
|
|
|
|
latencyEatenPost = dsd2pcmLatency;
|
2022-01-14 14:12:14 +00:00
|
|
|
}
|
2022-01-12 02:19:30 +00:00
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
// Input now contains bytesReadFromInput worth of floats, in the input sample rate
|
|
|
|
inpSize = bytesReadFromInput;
|
|
|
|
inpOffset = 0;
|
|
|
|
}
|
|
|
|
|
2022-01-14 03:43:18 +00:00
|
|
|
if (inpOffset != inpSize && floatOffset == floatSize)
|
2022-01-11 12:00:34 +00:00
|
|
|
{
|
|
|
|
struct resampler_data src_data;
|
|
|
|
|
|
|
|
size_t inputSamples = (inpSize - inpOffset) / floatFormat.mBytesPerPacket;
|
|
|
|
|
|
|
|
ioNumberPackets = (UInt32)inputSamples;
|
2021-12-25 23:02:13 +00:00
|
|
|
|
2022-01-16 07:41:31 +00:00
|
|
|
ioNumberPackets = (UInt32)ceil((float)ioNumberPackets * sampleRatio);
|
2021-12-25 23:02:13 +00:00
|
|
|
ioNumberPackets = (ioNumberPackets + 255) & ~255;
|
|
|
|
|
|
|
|
size_t newSize = ioNumberPackets * floatFormat.mBytesPerPacket;
|
2021-12-26 06:32:43 +00:00
|
|
|
if (newSize < (ioNumberPackets * dmFloatFormat.mBytesPerPacket))
|
|
|
|
newSize = ioNumberPackets * dmFloatFormat.mBytesPerPacket;
|
2021-12-25 23:02:13 +00:00
|
|
|
if (!floatBuffer || floatBufferSize < newSize)
|
2022-01-11 12:00:34 +00:00
|
|
|
floatBuffer = realloc( floatBuffer, floatBufferSize = newSize * 3 );
|
|
|
|
|
2021-12-26 07:41:45 +00:00
|
|
|
if (stopping)
|
|
|
|
{
|
|
|
|
convertEntered = NO;
|
|
|
|
return 0;
|
|
|
|
}
|
2022-01-11 12:00:34 +00:00
|
|
|
|
|
|
|
src_data.data_out = floatBuffer;
|
|
|
|
src_data.output_frames = 0;
|
|
|
|
|
|
|
|
src_data.data_in = (float*)(((uint8_t*)inputBuffer) + inpOffset);
|
|
|
|
src_data.input_frames = inputSamples;
|
2021-12-26 07:41:45 +00:00
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
src_data.ratio = sampleRatio;
|
|
|
|
|
|
|
|
if (!skipResampler)
|
2013-10-07 07:24:26 +00:00
|
|
|
{
|
2022-01-11 12:00:34 +00:00
|
|
|
resampler->process(resampler_data, &src_data);
|
2013-10-07 07:24:26 +00:00
|
|
|
}
|
2022-01-11 12:00:34 +00:00
|
|
|
else
|
2013-10-07 07:24:26 +00:00
|
|
|
{
|
2022-01-11 12:00:34 +00:00
|
|
|
memcpy(src_data.data_out, src_data.data_in, inputSamples * floatFormat.mBytesPerPacket);
|
|
|
|
src_data.output_frames = inputSamples;
|
|
|
|
}
|
|
|
|
|
|
|
|
inpOffset += inputSamples * floatFormat.mBytesPerPacket;
|
|
|
|
|
2022-01-19 08:27:59 +00:00
|
|
|
if (latencyEaten)
|
2022-01-11 12:00:34 +00:00
|
|
|
{
|
2022-01-19 08:27:59 +00:00
|
|
|
if (src_data.output_frames > latencyEaten)
|
2022-01-11 12:00:34 +00:00
|
|
|
{
|
2022-01-19 08:27:59 +00:00
|
|
|
src_data.output_frames -= latencyEaten;
|
|
|
|
memmove(src_data.data_out, src_data.data_out + latencyEaten * inputFormat.mChannelsPerFrame, src_data.output_frames * floatFormat.mBytesPerPacket);
|
|
|
|
latencyEaten = 0;
|
2022-01-11 12:00:34 +00:00
|
|
|
}
|
2022-01-19 08:27:59 +00:00
|
|
|
else
|
2022-01-11 12:00:34 +00:00
|
|
|
{
|
2022-01-19 08:27:59 +00:00
|
|
|
latencyEaten -= src_data.output_frames;
|
|
|
|
src_data.output_frames = 0;
|
2022-01-11 12:00:34 +00:00
|
|
|
}
|
2013-10-07 07:24:26 +00:00
|
|
|
}
|
2022-01-19 08:27:59 +00:00
|
|
|
else if (latencyEatenPost)
|
|
|
|
{
|
|
|
|
if (src_data.output_frames > latencyEatenPost)
|
|
|
|
{
|
|
|
|
src_data.output_frames -= latencyEatenPost;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
src_data.output_frames = 0;
|
|
|
|
}
|
|
|
|
latencyEatenPost = 0;
|
|
|
|
}
|
2013-10-07 10:59:04 +00:00
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
amountReadFromFC = (int)(src_data.output_frames * floatFormat.mBytesPerPacket);
|
|
|
|
|
|
|
|
scale_by_volume( (float*) floatBuffer, amountReadFromFC / sizeof(float), volumeScale);
|
|
|
|
|
2022-01-26 00:50:42 +00:00
|
|
|
if ( hFilter ) {
|
|
|
|
int samples = amountReadFromFC / floatFormat.mBytesPerFrame;
|
|
|
|
[hFilter process:floatBuffer sampleCount:samples toBuffer:floatBuffer + amountReadFromFC];
|
|
|
|
memmove(floatBuffer, floatBuffer + amountReadFromFC, samples * sizeof(float) * 2);
|
|
|
|
amountReadFromFC = samples * sizeof(float) * 2;
|
|
|
|
}
|
|
|
|
else if ( inputFormat.mChannelsPerFrame > 2 && outputFormat.mChannelsPerFrame == 2 )
|
2021-12-25 23:02:13 +00:00
|
|
|
{
|
|
|
|
int samples = amountReadFromFC / floatFormat.mBytesPerFrame;
|
|
|
|
downmix_to_stereo( (float*) floatBuffer, inputFormat.mChannelsPerFrame, samples );
|
|
|
|
amountReadFromFC = samples * sizeof(float) * 2;
|
|
|
|
}
|
2021-12-26 06:32:43 +00:00
|
|
|
else if ( inputFormat.mChannelsPerFrame > 1 && outputFormat.mChannelsPerFrame == 1 )
|
|
|
|
{
|
|
|
|
int samples = amountReadFromFC / floatFormat.mBytesPerFrame;
|
|
|
|
downmix_to_mono( (float*) floatBuffer, inputFormat.mChannelsPerFrame, samples );
|
|
|
|
amountReadFromFC = samples * sizeof(float);
|
|
|
|
}
|
|
|
|
else if ( inputFormat.mChannelsPerFrame < outputFormat.mChannelsPerFrame )
|
|
|
|
{
|
|
|
|
int samples = amountReadFromFC / floatFormat.mBytesPerFrame;
|
|
|
|
upmix( (float*) floatBuffer, inputFormat.mChannelsPerFrame, outputFormat.mChannelsPerFrame, samples );
|
|
|
|
amountReadFromFC = samples * sizeof(float) * outputFormat.mChannelsPerFrame;
|
|
|
|
}
|
2013-10-07 10:59:04 +00:00
|
|
|
|
2021-12-25 23:02:13 +00:00
|
|
|
floatSize = amountReadFromFC;
|
2013-10-07 10:59:04 +00:00
|
|
|
floatOffset = 0;
|
2013-10-07 07:24:26 +00:00
|
|
|
}
|
2021-12-26 07:41:45 +00:00
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
if (floatOffset == floatSize)
|
|
|
|
goto tryagain;
|
2021-12-26 07:41:45 +00:00
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
ioNumberPackets = (amount - amountRead);
|
|
|
|
if (ioNumberPackets > (floatSize - floatOffset))
|
|
|
|
ioNumberPackets = (UInt32)(floatSize - floatOffset);
|
|
|
|
|
|
|
|
memcpy(dest + amountRead, floatBuffer + floatOffset, ioNumberPackets);
|
|
|
|
|
|
|
|
floatOffset += ioNumberPackets;
|
|
|
|
amountRead += ioNumberPackets;
|
|
|
|
|
2021-12-26 07:41:45 +00:00
|
|
|
convertEntered = NO;
|
2007-10-20 03:24:27 +00:00
|
|
|
return amountRead;
|
2007-10-03 20:23:14 +00:00
|
|
|
}
|
|
|
|
|
2013-10-07 10:59:04 +00:00
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath
|
|
|
|
ofObject:(id)object
|
|
|
|
change:(NSDictionary *)change
|
|
|
|
context:(void *)context
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"SOMETHING CHANGED!");
|
2022-01-26 00:50:42 +00:00
|
|
|
if ([keyPath isEqualToString:@"values.volumeScaling"]) {
|
2013-10-07 10:59:04 +00:00
|
|
|
//User reset the volume scaling option
|
|
|
|
[self refreshVolumeScaling];
|
|
|
|
}
|
2022-01-26 00:50:42 +00:00
|
|
|
else if ([keyPath isEqualToString:@"values.outputResampling"]) {
|
2022-01-11 12:00:34 +00:00
|
|
|
// Reset resampler
|
2022-01-14 19:18:50 +00:00
|
|
|
if (resampler && resampler_data) {
|
|
|
|
NSString *value = [[NSUserDefaults standardUserDefaults] stringForKey:@"outputResampling"];
|
|
|
|
if (![value isEqualToString:outputResampling])
|
|
|
|
[self inputFormatDidChange:inputFormat];
|
|
|
|
}
|
2022-01-11 12:00:34 +00:00
|
|
|
}
|
2022-01-26 00:50:42 +00:00
|
|
|
else if ([keyPath isEqualToString:@"values.headphoneVirtualization"]) {
|
|
|
|
// Reset the converter, without rebuffering
|
|
|
|
if (outputFormat.mChannelsPerFrame == 2 &&
|
|
|
|
inputFormat.mChannelsPerFrame >= 1 &&
|
|
|
|
inputFormat.mChannelsPerFrame <= 8) {
|
|
|
|
[self inputFormatDidChange:inputFormat];
|
|
|
|
}
|
|
|
|
}
|
2013-10-07 10:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static float db_to_scale(float db)
|
|
|
|
{
|
|
|
|
return pow(10.0, db / 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)refreshVolumeScaling
|
|
|
|
{
|
|
|
|
if (rgInfo == nil)
|
|
|
|
{
|
|
|
|
volumeScale = 1.0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-22 06:47:11 +00:00
|
|
|
- (BOOL)setupWithInputFormat:(AudioStreamBasicDescription)inf outputFormat:(AudioStreamBasicDescription)outf isLossless:(BOOL)lossless
|
2007-10-03 20:23:14 +00:00
|
|
|
{
|
|
|
|
//Make the converter
|
|
|
|
inputFormat = inf;
|
|
|
|
outputFormat = outf;
|
2013-10-07 10:59:04 +00:00
|
|
|
|
2022-01-22 06:47:11 +00:00
|
|
|
rememberedLossless = lossless;
|
|
|
|
|
2022-01-12 01:09:06 +00:00
|
|
|
// 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;
|
|
|
|
|
2022-01-14 19:18:50 +00:00
|
|
|
// These are really placeholders, as we're doing everything internally now
|
2022-01-22 06:47:11 +00:00
|
|
|
if (lossless &&
|
|
|
|
inputFormat.mBitsPerChannel == 16 &&
|
2022-01-19 10:08:57 +00:00
|
|
|
inputFormat.mChannelsPerFrame == 2 &&
|
|
|
|
inputFormat.mSampleRate == 44100) {
|
|
|
|
// possibly HDCD, run through decoder
|
|
|
|
hdcd_decoder = calloc(1, sizeof(hdcd_state_stereo_t));
|
|
|
|
hdcd_reset_stereo((hdcd_state_stereo_t *)hdcd_decoder, 44100);
|
|
|
|
}
|
2022-01-14 19:18:50 +00:00
|
|
|
|
|
|
|
floatFormat = inputFormat;
|
|
|
|
floatFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
|
|
|
|
floatFormat.mBitsPerChannel = 32;
|
|
|
|
floatFormat.mBytesPerFrame = (32/8)*floatFormat.mChannelsPerFrame;
|
|
|
|
floatFormat.mBytesPerPacket = floatFormat.mBytesPerFrame * floatFormat.mFramesPerPacket;
|
|
|
|
|
2022-01-14 14:12:14 +00:00
|
|
|
if (inputFormat.mBitsPerChannel == 1) {
|
|
|
|
// Decimate this for speed
|
2022-01-14 19:18:50 +00:00
|
|
|
floatFormat.mSampleRate *= 1.0 / 8.0;
|
|
|
|
dsd2pcmCount = floatFormat.mChannelsPerFrame;
|
2022-01-14 14:12:14 +00:00
|
|
|
dsd2pcm = calloc(dsd2pcmCount, sizeof(void*));
|
|
|
|
dsd2pcm[0] = dsd2pcm_alloc();
|
|
|
|
dsd2pcmLatency = dsd2pcm_latency(dsd2pcm[0]);
|
|
|
|
for (size_t i = 1; i < dsd2pcmCount; ++i)
|
|
|
|
{
|
|
|
|
dsd2pcm[i] = dsd2pcm_dup(dsd2pcm[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
inpOffset = 0;
|
|
|
|
inpSize = 0;
|
|
|
|
|
2013-10-13 12:14:57 +00:00
|
|
|
floatOffset = 0;
|
|
|
|
floatSize = 0;
|
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
// This is a post resampler, post-down/upmix format
|
2013-10-07 10:59:04 +00:00
|
|
|
|
2021-12-25 23:02:13 +00:00
|
|
|
dmFloatFormat = floatFormat;
|
2022-01-11 12:00:34 +00:00
|
|
|
dmFloatFormat.mSampleRate = outputFormat.mSampleRate;
|
2021-12-26 06:32:43 +00:00
|
|
|
dmFloatFormat.mChannelsPerFrame = outputFormat.mChannelsPerFrame;
|
|
|
|
dmFloatFormat.mBytesPerFrame = (32/8)*dmFloatFormat.mChannelsPerFrame;
|
|
|
|
dmFloatFormat.mBytesPerPacket = dmFloatFormat.mBytesPerFrame * floatFormat.mFramesPerPacket;
|
2022-01-26 00:50:42 +00:00
|
|
|
|
|
|
|
BOOL hVirt = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] boolForKey:@"headphoneVirtualization"];
|
|
|
|
|
|
|
|
if (hVirt &&
|
|
|
|
outputFormat.mChannelsPerFrame == 2 &&
|
|
|
|
inputFormat.mChannelsPerFrame >= 1 &&
|
|
|
|
inputFormat.mChannelsPerFrame <= 8) {
|
|
|
|
CFURLRef appUrlRef = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("gsx"), CFSTR("wv"), NULL);
|
|
|
|
|
|
|
|
if (appUrlRef) {
|
|
|
|
hFilter = [[HeadphoneFilter alloc] initWithImpulseFile:(__bridge NSURL *)appUrlRef forSampleRate:outputFormat.mSampleRate withInputChannels:inputFormat.mChannelsPerFrame];
|
|
|
|
}
|
|
|
|
}
|
2007-10-03 20:23:14 +00:00
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
convert_s16_to_float_init_simd();
|
|
|
|
convert_s32_to_float_init_simd();
|
|
|
|
|
2022-01-14 19:18:50 +00:00
|
|
|
skipResampler = outputFormat.mSampleRate == floatFormat.mSampleRate;
|
2022-01-11 12:00:34 +00:00
|
|
|
|
2022-01-14 19:18:50 +00:00
|
|
|
sampleRatio = (double)outputFormat.mSampleRate / (double)floatFormat.mSampleRate;
|
2022-01-11 12:00:34 +00:00
|
|
|
|
|
|
|
if (!skipResampler)
|
2013-10-07 07:24:26 +00:00
|
|
|
{
|
2022-01-11 12:00:34 +00:00
|
|
|
enum resampler_quality quality = RESAMPLER_QUALITY_DONTCARE;
|
2013-10-07 07:24:26 +00:00
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
NSString * resampling = [[NSUserDefaults standardUserDefaults] stringForKey:@"outputResampling"];
|
|
|
|
if ([resampling isEqualToString:@"lowest"])
|
|
|
|
quality = RESAMPLER_QUALITY_LOWEST;
|
|
|
|
else if ([resampling isEqualToString:@"lower"])
|
|
|
|
quality = RESAMPLER_QUALITY_LOWER;
|
|
|
|
else if ([resampling isEqualToString:@"normal"])
|
|
|
|
quality = RESAMPLER_QUALITY_NORMAL;
|
|
|
|
else if ([resampling isEqualToString:@"higher"])
|
|
|
|
quality = RESAMPLER_QUALITY_HIGHER;
|
|
|
|
else if ([resampling isEqualToString:@"highest"])
|
|
|
|
quality = RESAMPLER_QUALITY_HIGHEST;
|
2021-12-25 23:02:13 +00:00
|
|
|
|
2022-01-14 19:18:50 +00:00
|
|
|
outputResampling = resampling;
|
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
if (!retro_resampler_realloc(&resampler_data, &resampler, "sinc", quality, inputFormat.mChannelsPerFrame, sampleRatio))
|
2021-12-25 23:02:13 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
2022-01-19 08:27:59 +00:00
|
|
|
|
|
|
|
PRIME_LEN_ = max(floatFormat.mSampleRate/20, 1024u);
|
|
|
|
PRIME_LEN_ = min(PRIME_LEN_, 16384u);
|
|
|
|
PRIME_LEN_ = max(PRIME_LEN_, 2*LPC_ORDER + 1);
|
|
|
|
|
|
|
|
N_samples_to_add_ = floatFormat.mSampleRate;
|
|
|
|
N_samples_to_drop_ = outputFormat.mSampleRate;
|
|
|
|
|
|
|
|
samples_len(&N_samples_to_add_, &N_samples_to_drop_, 20, 8192u);
|
|
|
|
|
|
|
|
is_preextrapolated_ = 0;
|
|
|
|
is_postextrapolated_ = 0;
|
2021-12-25 23:02:13 +00:00
|
|
|
}
|
2021-12-26 06:32:43 +00:00
|
|
|
|
2022-01-19 08:27:59 +00:00
|
|
|
latencyEaten = 0;
|
|
|
|
latencyEatenPost = 0;
|
|
|
|
|
2007-10-11 02:08:29 +00:00
|
|
|
PrintStreamDesc(&inf);
|
|
|
|
PrintStreamDesc(&outf);
|
2013-10-07 10:59:04 +00:00
|
|
|
|
|
|
|
[self refreshVolumeScaling];
|
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
// Move this here so process call isn't running the resampler until it's allocated
|
|
|
|
stopping = NO;
|
|
|
|
convertEntered = NO;
|
2022-01-13 07:13:00 +00:00
|
|
|
paused = NO;
|
|
|
|
outputFormatChanged = NO;
|
2021-12-25 23:02:13 +00:00
|
|
|
|
2007-10-03 20:23:14 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2007-10-13 07:09:46 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Decoder dealloc");
|
2013-10-11 03:02:02 +00:00
|
|
|
|
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.volumeScaling"];
|
2022-01-11 12:00:34 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.outputResampling"];
|
2022-01-26 00:50:42 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.headphoneVirtualization"];
|
2013-10-11 03:02:02 +00:00
|
|
|
|
2022-01-13 07:13:00 +00:00
|
|
|
paused = NO;
|
2007-10-13 07:09:46 +00:00
|
|
|
[self cleanUp];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-12 02:55:59 +00:00
|
|
|
- (void)setOutputFormat:(AudioStreamBasicDescription)format
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"SETTING OUTPUT FORMAT!");
|
2022-01-14 03:43:18 +00:00
|
|
|
previousOutputFormat = outputFormat;
|
2007-10-12 02:55:59 +00:00
|
|
|
outputFormat = format;
|
2022-01-13 07:13:00 +00:00
|
|
|
outputFormatChanged = YES;
|
2007-10-12 02:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)inputFormatDidChange:(AudioStreamBasicDescription)format
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"FORMAT CHANGED");
|
2022-01-13 07:13:00 +00:00
|
|
|
paused = YES;
|
2007-10-12 02:55:59 +00:00
|
|
|
[self cleanUp];
|
2022-01-14 19:18:50 +00:00
|
|
|
if (outputFormatChanged && ![buffer isEmpty] &&
|
|
|
|
memcmp(&outputFormat, &previousOutputFormat, sizeof(outputFormat)) != 0)
|
2022-01-14 03:43:18 +00:00
|
|
|
{
|
|
|
|
// Transfer previously buffered data, remember input format
|
|
|
|
rememberedInputFormat = format;
|
|
|
|
originalPreviousNode = previousNode;
|
|
|
|
refillNode = [[RefillNode alloc] initWithController:controller previous:nil];
|
|
|
|
[self setPreviousNode:refillNode];
|
|
|
|
|
|
|
|
int dataRead = 0;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
void * ptr;
|
|
|
|
dataRead = [buffer lengthAvailableToReadReturningPointer:&ptr];
|
|
|
|
if (dataRead) {
|
|
|
|
[refillNode writeData:(float*)ptr floatCount:dataRead / sizeof(float)];
|
|
|
|
[buffer didReadLength:dataRead];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-01-22 06:47:11 +00:00
|
|
|
[self setupWithInputFormat:previousOutputFormat outputFormat:outputFormat isLossless:rememberedLossless];
|
2022-01-14 03:43:18 +00:00
|
|
|
}
|
|
|
|
else
|
2022-01-13 07:13:00 +00:00
|
|
|
{
|
2022-01-22 06:47:11 +00:00
|
|
|
[self setupWithInputFormat:format outputFormat:outputFormat isLossless:rememberedLossless];
|
2022-01-13 07:13:00 +00:00
|
|
|
}
|
2007-10-12 02:55:59 +00:00
|
|
|
}
|
|
|
|
|
2013-10-07 10:59:04 +00:00
|
|
|
- (void)setRGInfo:(NSDictionary *)rgi
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Setting ReplayGain info");
|
2013-10-07 10:59:04 +00:00
|
|
|
rgInfo = rgi;
|
|
|
|
[self refreshVolumeScaling];
|
|
|
|
}
|
|
|
|
|
2007-10-03 20:23:14 +00:00
|
|
|
- (void)cleanUp
|
|
|
|
{
|
2021-12-26 12:34:33 +00:00
|
|
|
stopping = YES;
|
2022-01-11 12:00:34 +00:00
|
|
|
while (convertEntered)
|
2021-12-26 12:34:33 +00:00
|
|
|
{
|
|
|
|
usleep(500);
|
|
|
|
}
|
2022-01-26 00:50:42 +00:00
|
|
|
if (hFilter) {
|
|
|
|
hFilter = nil;
|
|
|
|
}
|
2022-01-19 10:08:57 +00:00
|
|
|
if (hdcd_decoder)
|
|
|
|
{
|
|
|
|
free(hdcd_decoder);
|
|
|
|
hdcd_decoder = NULL;
|
|
|
|
}
|
2022-01-11 12:00:34 +00:00
|
|
|
if (resampler && resampler_data)
|
2013-10-07 07:24:26 +00:00
|
|
|
{
|
2022-01-13 02:45:43 +00:00
|
|
|
resampler->free(resampler, resampler_data);
|
2022-01-11 12:00:34 +00:00
|
|
|
resampler = NULL;
|
|
|
|
resampler_data = NULL;
|
2013-10-07 07:24:26 +00:00
|
|
|
}
|
2022-01-14 14:12:14 +00:00
|
|
|
if (dsd2pcm && dsd2pcmCount)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < dsd2pcmCount; ++i)
|
|
|
|
{
|
|
|
|
dsd2pcm_free(dsd2pcm[i]);
|
|
|
|
dsd2pcm[i] = NULL;
|
|
|
|
}
|
|
|
|
free(dsd2pcm);
|
|
|
|
dsd2pcm = NULL;
|
|
|
|
}
|
2022-01-14 10:00:32 +00:00
|
|
|
if (extrapolateBuffer)
|
|
|
|
{
|
|
|
|
free(extrapolateBuffer);
|
|
|
|
extrapolateBuffer = NULL;
|
|
|
|
extrapolateBufferSize = 0;
|
|
|
|
}
|
2013-10-07 10:59:04 +00:00
|
|
|
if (floatBuffer)
|
2013-10-07 07:24:26 +00:00
|
|
|
{
|
2013-10-07 10:59:04 +00:00
|
|
|
free(floatBuffer);
|
|
|
|
floatBuffer = NULL;
|
2021-12-25 23:02:13 +00:00
|
|
|
floatBufferSize = 0;
|
2013-10-07 07:24:26 +00:00
|
|
|
}
|
2022-01-11 12:00:34 +00:00
|
|
|
if (inputBuffer) {
|
|
|
|
free(inputBuffer);
|
|
|
|
inputBuffer = NULL;
|
|
|
|
inputBufferSize = 0;
|
2007-10-13 07:09:46 +00:00
|
|
|
}
|
2013-10-07 10:59:04 +00:00
|
|
|
floatOffset = 0;
|
|
|
|
floatSize = 0;
|
2007-10-03 20:23:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 07:05:32 +00:00
|
|
|
- (double) secondsBuffered
|
|
|
|
{
|
|
|
|
return ((double)[buffer bufferedLength] / (outputFormat.mSampleRate * outputFormat.mBytesPerPacket));
|
|
|
|
}
|
|
|
|
|
2007-10-03 20:23:14 +00:00
|
|
|
@end
|