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-01-19 10:08:57 +00:00
|
|
|
#import "hdcd_decode2.h"
|
|
|
|
|
2022-02-16 06:45:28 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
#import "BadSampleCleaner.h"
|
|
|
|
#endif
|
|
|
|
|
2022-06-10 09:13:10 +00:00
|
|
|
#if !DSD_DECIMATE
|
|
|
|
#include "dsd2float.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;
|
|
|
|
floatBuffer = NULL;
|
|
|
|
floatBufferSize = 0;
|
|
|
|
|
|
|
|
stopping = NO;
|
|
|
|
convertEntered = NO;
|
|
|
|
paused = NO;
|
|
|
|
|
2022-06-10 09:13:10 +00:00
|
|
|
#if DSD_DECIMATE
|
2022-02-07 05:49:27 +00:00
|
|
|
dsd2pcm = NULL;
|
|
|
|
dsd2pcmCount = 0;
|
2022-06-10 09:13:10 +00:00
|
|
|
#endif
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
hdcd_decoder = NULL;
|
|
|
|
|
2022-02-08 06:02:17 +00:00
|
|
|
lastChunkIn = nil;
|
|
|
|
|
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-06-10 09:13:10 +00:00
|
|
|
#if DSD_DECIMATE
|
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
|
2022-02-07 05:49:27 +00:00
|
|
|
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
|
2022-01-14 14:12:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct dsd2pcm_state {
|
2022-02-07 05:49:27 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2022-01-14 14:12:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void dsd2pcm_free(void *);
|
|
|
|
static void dsd2pcm_reset(void *);
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static void *dsd2pcm_alloc() {
|
|
|
|
struct dsd2pcm_state *state = (struct dsd2pcm_state *)calloc(1, sizeof(struct dsd2pcm_state));
|
|
|
|
|
2022-03-04 10:07:38 +00:00
|
|
|
float *FILT_LOOKUP_TABLE;
|
|
|
|
double *temp;
|
|
|
|
uint8_t *REVERSE_BITS;
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
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;
|
2022-03-04 10:07:38 +00:00
|
|
|
FILT_LOOKUP_TABLE = state->FILT_LOOKUP_TABLE;
|
|
|
|
temp = (double *)calloc(sizeof(double), 0x100);
|
2022-02-07 05:49:27 +00:00
|
|
|
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;
|
2022-03-04 10:07:38 +00:00
|
|
|
REVERSE_BITS = state->REVERSE_BITS;
|
2022-02-07 05:49:27 +00:00
|
|
|
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;
|
|
|
|
|
2022-01-14 14:12:14 +00:00
|
|
|
fail:
|
2022-02-07 05:49:27 +00:00
|
|
|
dsd2pcm_free(state);
|
|
|
|
return NULL;
|
2022-01-14 14:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
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;
|
2022-01-14 14:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
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);
|
|
|
|
}
|
2022-01-14 14:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
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-14 14:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +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-19 08:27:59 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static void dsd2pcm_process(void *_state, const 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-14 14:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static void convert_dsd_to_f32(float *output, const uint8_t *input, size_t count, size_t channels, void **dsd2pcm) {
|
|
|
|
for(size_t channel = 0; channel < channels; ++channel) {
|
|
|
|
dsd2pcm_process(dsd2pcm[channel], input, channel, channels, output, channel, channels, count);
|
|
|
|
}
|
2022-01-14 14:12:14 +00:00
|
|
|
}
|
2022-06-10 09:13:10 +00:00
|
|
|
#else
|
|
|
|
static void convert_dsd_to_f32(float *output, const uint8_t *input, size_t count, size_t channels) {
|
|
|
|
const uint8_t *iptr = input;
|
|
|
|
float *optr = output;
|
|
|
|
for(size_t index = 0; index < count; ++index) {
|
|
|
|
for(size_t channel = 0; channel < channels; ++channel) {
|
|
|
|
uint8_t sample = *iptr++;
|
|
|
|
cblas_scopy(8, &dsd2float[sample][0], 1, optr++, (int)channels);
|
|
|
|
}
|
|
|
|
optr += channels * 7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2022-01-14 14:12:14 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static void convert_u8_to_s16(int16_t *output, const uint8_t *input, size_t count) {
|
|
|
|
for(size_t i = 0; i < count; ++i) {
|
|
|
|
uint16_t sample = (input[i] << 8) | input[i];
|
|
|
|
sample ^= 0x8080;
|
|
|
|
output[i] = (int16_t)(sample);
|
|
|
|
}
|
2022-01-11 12:00:34 +00:00
|
|
|
}
|
2007-10-03 20:23:14 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static void convert_s8_to_s16(int16_t *output, const uint8_t *input, size_t count) {
|
|
|
|
for(size_t i = 0; i < count; ++i) {
|
|
|
|
uint16_t sample = (input[i] << 8) | input[i];
|
|
|
|
output[i] = (int16_t)(sample);
|
|
|
|
}
|
2022-01-11 12:00:34 +00:00
|
|
|
}
|
2021-12-26 07:41:45 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static void convert_u16_to_s16(int16_t *buffer, size_t count) {
|
|
|
|
for(size_t i = 0; i < count; ++i) {
|
|
|
|
buffer[i] ^= 0x8000;
|
|
|
|
}
|
2007-10-03 20:23:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +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];
|
|
|
|
}
|
2022-01-19 10:08:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static void convert_s24_to_s32(int32_t *output, const uint8_t *input, size_t count) {
|
|
|
|
for(size_t i = 0; i < count; ++i) {
|
|
|
|
int32_t sample = (input[i * 3] << 8) | (input[i * 3 + 1] << 16) | (input[i * 3 + 2] << 24);
|
|
|
|
output[i] = sample;
|
|
|
|
}
|
2022-01-11 12:00:34 +00:00
|
|
|
}
|
2013-10-07 07:24:26 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static void convert_u24_to_s32(int32_t *output, const uint8_t *input, size_t count) {
|
|
|
|
for(size_t i = 0; i < count; ++i) {
|
|
|
|
int32_t sample = (input[i * 3] << 8) | (input[i * 3 + 1] << 16) | (input[i * 3 + 2] << 24);
|
|
|
|
output[i] = sample ^ 0x80000000;
|
|
|
|
}
|
2022-01-11 12:00:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static void convert_u32_to_s32(int32_t *buffer, size_t count) {
|
|
|
|
for(size_t i = 0; i < count; ++i) {
|
|
|
|
buffer[i] ^= 0x80000000;
|
|
|
|
}
|
2022-01-11 12:00:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static void convert_f64_to_f32(float *output, const double *input, size_t count) {
|
2022-02-12 08:03:25 +00:00
|
|
|
vDSP_vdpsp(input, 1, output, 1, count);
|
2022-01-12 01:09:06 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static void convert_be_to_le(uint8_t *buffer, size_t bitsPerSample, size_t bytes) {
|
|
|
|
size_t i;
|
|
|
|
bitsPerSample = (bitsPerSample + 7) / 8;
|
|
|
|
switch(bitsPerSample) {
|
|
|
|
case 2:
|
|
|
|
for(i = 0; i < bytes; i += 2) {
|
2022-02-12 08:38:08 +00:00
|
|
|
*(int16_t *)buffer = __builtin_bswap16(*(int16_t *)buffer);
|
2022-02-07 05:49:27 +00:00
|
|
|
buffer += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-02-12 08:38:08 +00:00
|
|
|
case 3: {
|
|
|
|
union {
|
|
|
|
vDSP_int24 int24;
|
|
|
|
uint32_t int32;
|
|
|
|
} intval;
|
|
|
|
intval.int32 = 0;
|
2022-02-07 05:49:27 +00:00
|
|
|
for(i = 0; i < bytes; i += 3) {
|
2022-02-12 08:38:08 +00:00
|
|
|
intval.int24 = *(vDSP_int24 *)buffer;
|
|
|
|
intval.int32 = __builtin_bswap32(intval.int32 << 8);
|
|
|
|
*(vDSP_int24 *)buffer = intval.int24;
|
2022-02-07 05:49:27 +00:00
|
|
|
buffer += 3;
|
|
|
|
}
|
2022-02-12 08:38:08 +00:00
|
|
|
} break;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
case 4:
|
|
|
|
for(i = 0; i < bytes; i += 4) {
|
2022-02-12 08:38:08 +00:00
|
|
|
*(uint32_t *)buffer = __builtin_bswap32(*(uint32_t *)buffer);
|
2022-02-07 05:49:27 +00:00
|
|
|
buffer += 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
for(i = 0; i < bytes; i += 8) {
|
2022-02-12 08:38:08 +00:00
|
|
|
*(uint64_t *)buffer = __builtin_bswap64(*(uint64_t *)buffer);
|
2022-02-07 05:49:27 +00:00
|
|
|
buffer += 8;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-10-07 07:24:26 +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) {
|
|
|
|
continue;
|
2022-02-07 12:06:36 +00:00
|
|
|
}
|
2022-07-10 22:14:47 +00:00
|
|
|
@autoreleasepool {
|
|
|
|
[self writeChunk:chunk];
|
|
|
|
chunk = nil;
|
|
|
|
}
|
|
|
|
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
|
|
|
int amountReadFromFC;
|
|
|
|
int amountRead = 0;
|
|
|
|
|
|
|
|
if(stopping)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
convertEntered = YES;
|
|
|
|
|
2022-01-11 12:00:34 +00:00
|
|
|
tryagain:
|
2022-02-07 05:49:27 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
amountReadFromFC = 0;
|
|
|
|
|
|
|
|
if(floatOffset == floatSize) // skip this step if there's still float buffered
|
|
|
|
while(inpOffset == inpSize) {
|
|
|
|
size_t samplesRead = 0;
|
|
|
|
|
|
|
|
BOOL isFloat = !!(inputFormat.mFormatFlags & kAudioFormatFlagIsFloat);
|
|
|
|
BOOL isUnsigned = !isFloat && !(inputFormat.mFormatFlags & kAudioFormatFlagIsSignedInteger);
|
2022-06-10 09:13:10 +00:00
|
|
|
size_t bitsPerSample = inputFormat.mBitsPerChannel;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
// Approximately the most we want on input
|
|
|
|
ioNumberPackets = CHUNK_SIZE;
|
|
|
|
|
2022-06-10 09:13:10 +00:00
|
|
|
#if DSD_DECIMATE
|
|
|
|
const size_t sizeScale = 3;
|
|
|
|
#else
|
|
|
|
const size_t sizeScale = (bitsPerSample == 1) ? 10 : 3;
|
|
|
|
#endif
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
size_t newSize = ioNumberPackets * floatFormat.mBytesPerPacket;
|
|
|
|
if(!inputBuffer || inputBufferSize < newSize)
|
2022-06-10 09:13:10 +00:00
|
|
|
inputBuffer = realloc(inputBuffer, inputBufferSize = newSize * sizeScale);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
ssize_t amountToWrite = ioNumberPackets * inputFormat.mBytesPerPacket;
|
|
|
|
|
|
|
|
ssize_t bytesReadFromInput = 0;
|
|
|
|
|
2022-02-08 06:02:17 +00:00
|
|
|
while(bytesReadFromInput < amountToWrite && !stopping && !paused && !streamFormatChanged && [self shouldContinue] == YES && [self endOfStream] == NO) {
|
2022-02-08 03:18:45 +00:00
|
|
|
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-07 05:49:27 +00:00
|
|
|
AudioChunk *chunk = [self readChunk:((amountToWrite - bytesReadFromInput) / inputFormat.mBytesPerPacket)];
|
2022-02-08 03:18:45 +00:00
|
|
|
inf = [chunk format];
|
2022-02-07 05:49:27 +00:00
|
|
|
size_t frameCount = [chunk frameCount];
|
2022-02-08 03:18:45 +00:00
|
|
|
config = [chunk channelConfig];
|
2022-02-07 05:49:27 +00:00
|
|
|
size_t bytesRead = frameCount * inf.mBytesPerPacket;
|
|
|
|
if(frameCount) {
|
|
|
|
NSData *samples = [chunk removeSamples:frameCount];
|
2022-03-04 10:07:38 +00:00
|
|
|
memcpy(((uint8_t *)inputBuffer) + bytesReadFromInput, [samples bytes], bytesRead);
|
2022-02-08 03:18:45 +00:00
|
|
|
lastChunkIn = [[AudioChunk alloc] init];
|
|
|
|
[lastChunkIn setFormat:inf];
|
|
|
|
[lastChunkIn setChannelConfig:config];
|
|
|
|
[lastChunkIn setLossless:[chunk lossless]];
|
|
|
|
[lastChunkIn assignSamples:[samples bytes] frameCount:frameCount];
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
bytesReadFromInput += bytesRead;
|
|
|
|
if(!frameCount) {
|
2022-02-08 06:02:17 +00:00
|
|
|
usleep(500);
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 03:18:45 +00:00
|
|
|
BOOL isBigEndian = !!(inputFormat.mFormatFlags & kAudioFormatFlagIsBigEndian);
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(!bytesReadFromInput) {
|
|
|
|
convertEntered = NO;
|
2022-07-10 22:14:47 +00:00
|
|
|
return nil;
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 03:18:45 +00:00
|
|
|
if(bytesReadFromInput && isBigEndian) {
|
2022-02-07 05:49:27 +00:00
|
|
|
// Time for endian swap!
|
2022-03-04 10:07:38 +00:00
|
|
|
convert_be_to_le((uint8_t *)inputBuffer, inputFormat.mBitsPerChannel, bytesReadFromInput);
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(bytesReadFromInput && isFloat && inputFormat.mBitsPerChannel == 64) {
|
|
|
|
// Time for precision loss from weird inputs
|
|
|
|
samplesRead = bytesReadFromInput / sizeof(double);
|
2022-03-04 10:07:38 +00:00
|
|
|
convert_f64_to_f32((float *)(((uint8_t *)inputBuffer) + bytesReadFromInput), (const double *)inputBuffer, samplesRead);
|
|
|
|
memmove(inputBuffer, ((uint8_t *)inputBuffer) + bytesReadFromInput, samplesRead * sizeof(float));
|
2022-02-07 05:49:27 +00:00
|
|
|
bytesReadFromInput = samplesRead * sizeof(float);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bytesReadFromInput && !isFloat) {
|
|
|
|
float gain = 1.0;
|
|
|
|
if(bitsPerSample == 1) {
|
|
|
|
samplesRead = bytesReadFromInput / inputFormat.mBytesPerPacket;
|
2022-02-16 06:53:09 +00:00
|
|
|
size_t buffer_adder = (bytesReadFromInput + 15) & ~15;
|
2022-06-10 09:13:10 +00:00
|
|
|
convert_dsd_to_f32((float *)(((uint8_t *)inputBuffer) + buffer_adder), (const uint8_t *)inputBuffer, samplesRead, inputFormat.mChannelsPerFrame
|
|
|
|
#if DSD_DECIMATE
|
|
|
|
,
|
|
|
|
dsd2pcm
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
#if !DSD_DECIMATE
|
|
|
|
samplesRead *= 8;
|
|
|
|
#endif
|
2022-03-04 10:07:38 +00:00
|
|
|
memmove(inputBuffer, ((const uint8_t *)inputBuffer) + buffer_adder, samplesRead * inputFormat.mChannelsPerFrame * sizeof(float));
|
2022-02-07 05:49:27 +00:00
|
|
|
bitsPerSample = 32;
|
|
|
|
bytesReadFromInput = samplesRead * inputFormat.mChannelsPerFrame * sizeof(float);
|
|
|
|
isFloat = YES;
|
|
|
|
} else if(bitsPerSample <= 8) {
|
|
|
|
samplesRead = bytesReadFromInput;
|
2022-02-16 06:53:09 +00:00
|
|
|
size_t buffer_adder = (bytesReadFromInput + 1) & ~1;
|
2022-02-07 05:49:27 +00:00
|
|
|
if(!isUnsigned)
|
2022-03-04 10:07:38 +00:00
|
|
|
convert_s8_to_s16((int16_t *)(((uint8_t *)inputBuffer) + buffer_adder), (const uint8_t *)inputBuffer, samplesRead);
|
2022-02-07 05:49:27 +00:00
|
|
|
else
|
2022-03-04 10:07:38 +00:00
|
|
|
convert_u8_to_s16((int16_t *)(((uint8_t *)inputBuffer) + buffer_adder), (const uint8_t *)inputBuffer, samplesRead);
|
|
|
|
memmove(inputBuffer, ((uint8_t *)inputBuffer) + buffer_adder, samplesRead * 2);
|
2022-02-07 05:49:27 +00:00
|
|
|
bitsPerSample = 16;
|
|
|
|
bytesReadFromInput = samplesRead * 2;
|
|
|
|
isUnsigned = NO;
|
|
|
|
}
|
|
|
|
if(hdcd_decoder) { // implied bits per sample is 16, produces 32 bit int scale
|
|
|
|
samplesRead = bytesReadFromInput / 2;
|
|
|
|
if(isUnsigned)
|
2022-03-04 10:07:38 +00:00
|
|
|
convert_u16_to_s16((int16_t *)inputBuffer, samplesRead);
|
2022-02-16 06:53:09 +00:00
|
|
|
size_t buffer_adder = (bytesReadFromInput + 3) & ~3;
|
2022-03-04 10:07:38 +00:00
|
|
|
convert_s16_to_hdcd_input((int32_t *)(((uint8_t *)inputBuffer) + buffer_adder), (int16_t *)inputBuffer, samplesRead);
|
|
|
|
memmove(inputBuffer, ((uint8_t *)inputBuffer) + buffer_adder, samplesRead * 4);
|
|
|
|
hdcd_process_stereo((hdcd_state_stereo_t *)hdcd_decoder, (int32_t *)inputBuffer, (int)(samplesRead / 2));
|
2022-02-07 05:49:27 +00:00
|
|
|
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) {
|
|
|
|
samplesRead = bytesReadFromInput / 2;
|
|
|
|
if(isUnsigned)
|
2022-03-04 10:07:38 +00:00
|
|
|
convert_u16_to_s16((int16_t *)inputBuffer, samplesRead);
|
2022-02-16 06:53:09 +00:00
|
|
|
size_t buffer_adder = (bytesReadFromInput + 15) & ~15; // vDSP functions expect aligned to four elements
|
2022-03-04 10:07:38 +00:00
|
|
|
vDSP_vflt16((const short *)inputBuffer, 1, (float *)(((uint8_t *)inputBuffer) + buffer_adder), 1, samplesRead);
|
2022-02-16 06:53:09 +00:00
|
|
|
float scale = 1ULL << 15;
|
2022-03-04 10:07:38 +00:00
|
|
|
vDSP_vsdiv((const float *)(((uint8_t *)inputBuffer) + buffer_adder), 1, &scale, (float *)(((uint8_t *)inputBuffer) + buffer_adder), 1, samplesRead);
|
|
|
|
memmove(inputBuffer, ((uint8_t *)inputBuffer) + buffer_adder, samplesRead * sizeof(float));
|
2022-02-07 05:49:27 +00:00
|
|
|
bitsPerSample = 32;
|
|
|
|
bytesReadFromInput = samplesRead * sizeof(float);
|
|
|
|
isUnsigned = NO;
|
|
|
|
isFloat = YES;
|
|
|
|
} else if(bitsPerSample <= 24) {
|
|
|
|
samplesRead = bytesReadFromInput / 3;
|
2022-02-16 06:53:09 +00:00
|
|
|
size_t buffer_adder = (bytesReadFromInput + 3) & ~3;
|
2022-02-07 05:49:27 +00:00
|
|
|
if(isUnsigned)
|
2022-03-04 10:07:38 +00:00
|
|
|
convert_u24_to_s32((int32_t *)(((uint8_t *)inputBuffer) + buffer_adder), (uint8_t *)inputBuffer, samplesRead);
|
2022-02-07 05:49:27 +00:00
|
|
|
else
|
2022-03-04 10:07:38 +00:00
|
|
|
convert_s24_to_s32((int32_t *)(((uint8_t *)inputBuffer) + buffer_adder), (uint8_t *)inputBuffer, samplesRead);
|
|
|
|
memmove(inputBuffer, ((uint8_t *)inputBuffer) + buffer_adder, samplesRead * 4);
|
2022-02-07 05:49:27 +00:00
|
|
|
bitsPerSample = 32;
|
|
|
|
bytesReadFromInput = samplesRead * 4;
|
|
|
|
isUnsigned = NO;
|
|
|
|
}
|
|
|
|
if(!isFloat && bitsPerSample <= 32) {
|
|
|
|
samplesRead = bytesReadFromInput / 4;
|
|
|
|
if(isUnsigned)
|
2022-03-04 10:07:38 +00:00
|
|
|
convert_u32_to_s32((int32_t *)inputBuffer, samplesRead);
|
2022-02-16 06:53:09 +00:00
|
|
|
size_t buffer_adder = (bytesReadFromInput + 31) & ~31; // vDSP functions expect aligned to four elements
|
2022-03-04 10:07:38 +00:00
|
|
|
vDSP_vflt32((const int *)inputBuffer, 1, (float *)(((uint8_t *)inputBuffer) + buffer_adder), 1, samplesRead);
|
2022-02-16 06:53:09 +00:00
|
|
|
float scale = (1ULL << 31) / gain;
|
2022-03-04 10:07:38 +00:00
|
|
|
vDSP_vsdiv((const float *)(((uint8_t *)inputBuffer) + buffer_adder), 1, &scale, (float *)(((uint8_t *)inputBuffer) + buffer_adder), 1, samplesRead);
|
|
|
|
memmove(inputBuffer, ((uint8_t *)inputBuffer) + buffer_adder, samplesRead * sizeof(float));
|
2022-02-07 05:49:27 +00:00
|
|
|
bitsPerSample = 32;
|
|
|
|
bytesReadFromInput = samplesRead * sizeof(float);
|
|
|
|
isUnsigned = NO;
|
|
|
|
isFloat = YES;
|
|
|
|
}
|
2022-02-16 06:45:28 +00:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
[BadSampleCleaner cleanSamples:(float *)inputBuffer
|
|
|
|
amount:bytesReadFromInput / sizeof(float)
|
|
|
|
location:@"post int to float conversion"];
|
|
|
|
#endif
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Input now contains bytesReadFromInput worth of floats, in the input sample rate
|
|
|
|
inpSize = bytesReadFromInput;
|
2022-06-24 06:17:31 +00:00
|
|
|
inpOffset = 0;
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(inpOffset != inpSize && floatOffset == floatSize) {
|
2022-06-10 09:14:04 +00:00
|
|
|
#if DSD_DECIMATE
|
|
|
|
const float scaleModifier = (inputFormat.mBitsPerChannel == 1) ? 0.5f : 1.0f;
|
|
|
|
#endif
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
size_t inputSamples = (inpSize - inpOffset) / floatFormat.mBytesPerPacket;
|
|
|
|
|
|
|
|
ioNumberPackets = (UInt32)inputSamples;
|
|
|
|
|
|
|
|
ioNumberPackets = (ioNumberPackets + 255) & ~255;
|
|
|
|
|
|
|
|
size_t newSize = ioNumberPackets * floatFormat.mBytesPerPacket;
|
|
|
|
if(newSize < (ioNumberPackets * dmFloatFormat.mBytesPerPacket))
|
|
|
|
newSize = ioNumberPackets * dmFloatFormat.mBytesPerPacket;
|
|
|
|
if(!floatBuffer || floatBufferSize < newSize)
|
|
|
|
floatBuffer = realloc(floatBuffer, floatBufferSize = newSize * 3);
|
|
|
|
|
|
|
|
if(stopping) {
|
|
|
|
convertEntered = NO;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t inputDone = 0;
|
|
|
|
size_t outputDone = 0;
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
memcpy(floatBuffer, (((uint8_t *)inputBuffer) + inpOffset), inputSamples * floatFormat.mBytesPerPacket);
|
|
|
|
inputDone = inputSamples;
|
|
|
|
outputDone = inputSamples;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
inpOffset += inputDone * floatFormat.mBytesPerPacket;
|
|
|
|
|
|
|
|
amountReadFromFC = (int)(outputDone * floatFormat.mBytesPerPacket);
|
|
|
|
|
2022-06-10 09:14:04 +00:00
|
|
|
scale_by_volume((float *)floatBuffer, amountReadFromFC / sizeof(float), volumeScale
|
|
|
|
#if DSD_DECIMATE
|
|
|
|
* scaleModifier
|
|
|
|
#endif
|
|
|
|
);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
floatSize = amountReadFromFC;
|
|
|
|
floatOffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(floatOffset == floatSize)
|
|
|
|
goto tryagain;
|
|
|
|
|
2022-07-10 22:14:47 +00:00
|
|
|
ioNumberPackets = (UInt32)(floatSize - floatOffset);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
ioNumberPackets -= ioNumberPackets % dmFloatFormat.mBytesPerPacket;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-10 22:14:47 +00:00
|
|
|
AudioChunk *chunk = [[AudioChunk alloc] init];
|
|
|
|
[chunk setFormat:nodeFormat];
|
|
|
|
if(nodeChannelConfig) {
|
|
|
|
[chunk setChannelConfig:nodeChannelConfig];
|
|
|
|
}
|
|
|
|
[chunk assignSamples:floatBuffer frameCount:ioNumberPackets / dmFloatFormat.mBytesPerPacket];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
floatOffset += ioNumberPackets;
|
|
|
|
amountRead += ioNumberPackets;
|
|
|
|
|
|
|
|
convertEntered = NO;
|
2022-07-10 22:14:47 +00:00
|
|
|
return chunk;
|
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;
|
|
|
|
|
|
|
|
// These are really placeholders, as we're doing everything internally now
|
|
|
|
if(lossless &&
|
|
|
|
inputFormat.mBitsPerChannel == 16 &&
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
dsd2pcmCount = floatFormat.mChannelsPerFrame;
|
2022-03-04 10:07:38 +00:00
|
|
|
dsd2pcm = (void **)calloc(dsd2pcmCount, sizeof(void *));
|
2022-02-07 05:49:27 +00:00
|
|
|
dsd2pcm[0] = dsd2pcm_alloc();
|
|
|
|
dsd2pcmLatency = dsd2pcm_latency(dsd2pcm[0]);
|
|
|
|
for(size_t i = 1; i < dsd2pcmCount; ++i) {
|
|
|
|
dsd2pcm[i] = dsd2pcm_dup(dsd2pcm[0]);
|
|
|
|
}
|
|
|
|
}
|
2022-06-10 09:13:10 +00:00
|
|
|
#endif
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
inpOffset = 0;
|
|
|
|
inpSize = 0;
|
|
|
|
|
|
|
|
floatOffset = 0;
|
|
|
|
floatSize = 0;
|
|
|
|
|
|
|
|
// This is a post resampler, post-down/upmix format
|
|
|
|
|
|
|
|
dmFloatFormat = floatFormat;
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
nodeFormat = dmFloatFormat;
|
|
|
|
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(hdcd_decoder) {
|
|
|
|
free(hdcd_decoder);
|
|
|
|
hdcd_decoder = NULL;
|
|
|
|
}
|
2022-06-10 09:13:10 +00:00
|
|
|
#if DSD_DECIMATE
|
2022-02-07 05:49:27 +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-06-10 09:13:10 +00:00
|
|
|
#endif
|
2022-02-07 05:49:27 +00:00
|
|
|
if(floatBuffer) {
|
|
|
|
free(floatBuffer);
|
|
|
|
floatBuffer = NULL;
|
|
|
|
floatBufferSize = 0;
|
|
|
|
}
|
|
|
|
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-02-07 05:49:27 +00:00
|
|
|
floatOffset = 0;
|
|
|
|
floatSize = 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
|