Skip to content

Commit

Permalink
support inverting dop output
Browse files Browse the repository at this point in the history
  • Loading branch information
triode committed Jan 23, 2015
1 parent 159325a commit daf16b2
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 23 deletions.
29 changes: 20 additions & 9 deletions dop.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,28 @@ bool is_flac_dop(u32_t *lptr, u32_t *rptr, frames_t frames) {
return false;
}

// update the dop marker for frames in the output buffer
// update the dop marker and potentially invert polarity for frames in the output buffer
// performaned on all output including silence to maintain marker phase consitency
void update_dop_marker(u32_t *ptr, frames_t frames) {
void update_dop(u32_t *ptr, frames_t frames, bool invert) {
static u32_t marker = 0x05;
while (frames--) {
u32_t scaled_marker = marker << 24;
*ptr = (*ptr & 0x00FFFFFF) | scaled_marker;
++ptr;
*ptr = (*ptr & 0x00FFFFFF) | scaled_marker;
++ptr;
marker = ( 0x05 + 0xFA ) - marker;
if (!invert) {
while (frames--) {
u32_t scaled_marker = marker << 24;
*ptr = (*ptr & 0x00FFFFFF) | scaled_marker;
++ptr;
*ptr = (*ptr & 0x00FFFFFF) | scaled_marker;
++ptr;
marker = ( 0x05 + 0xFA ) - marker;
}
} else {
while (frames--) {
u32_t scaled_marker = marker << 24;
*ptr = ((~(*ptr)) & 0x00FFFFFF) | scaled_marker;
++ptr;
*ptr = ((~(*ptr)) & 0x00FFFFFF) | scaled_marker;
++ptr;
marker = ( 0x05 + 0xFA ) - marker;
}
}
}

Expand Down
18 changes: 11 additions & 7 deletions output.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ frames_t _output_frames(frames_t avail) {

s32_t cross_gain_in = 0, cross_gain_out = 0; s32_t *cross_ptr = NULL;

s32_t gainL = output.polarity * (output.current_replay_gain ? gain(output.gainL, output.current_replay_gain) : output.gainL);
s32_t gainR = output.polarity * (output.current_replay_gain ? gain(output.gainR, output.current_replay_gain) : output.gainR);
s32_t gainL = output.current_replay_gain ? gain(output.gainL, output.current_replay_gain) : output.gainL;
s32_t gainR = output.current_replay_gain ? gain(output.gainR, output.current_replay_gain) : output.gainR;

if (output.invert) { gainL = -gainL; gainR = -gainR; }

frames = _buf_used(outputbuf) / BYTES_PER_FRAME;
silence = false;
Expand Down Expand Up @@ -218,8 +220,9 @@ frames_t _output_frames(frames_t avail) {
cur_f = dur_f - cur_f;
}
fade_gain = to_gain((float)cur_f / (float)dur_f);
gainL = output.polarity * gain(gainL, fade_gain);
gainR = output.polarity * gain(gainR, fade_gain);
gainL = gain(gainL, fade_gain);
gainR = gain(gainR, fade_gain);
if (output.invert) { gainL = -gainL; gainR = -gainR; }
}
if (output.fade_dir == FADE_CROSS) {
// cross fade requires special treatment - performed later based on these values
Expand All @@ -233,8 +236,9 @@ frames_t _output_frames(frames_t avail) {
if (output.next_replay_gain) {
cross_gain_in = gain(cross_gain_in, output.next_replay_gain);
}
gainL = output.polarity * output.gainL;
gainR = output.polarity * output.gainR;
gainL = output.gainL;
gainR = output.gainR;
if (output.invert) { gainL = -gainL; gainR = -gainR; }
cross_ptr = (s32_t *)(output.fade_end + cur_f * BYTES_PER_FRAME);
} else {
LOG_INFO("unable to continue crossfade - too few samples");
Expand Down Expand Up @@ -368,7 +372,7 @@ void output_init_common(log_level level, const char *device, unsigned output_buf
output.state = idle ? OUTPUT_OFF: OUTPUT_STOPPED;
output.device = device;
output.fade = FADE_INACTIVE;
output.polarity = 1;
output.invert = false;
output.error_opening = false;
output.idle_to = (u32_t) idle;

Expand Down
2 changes: 1 addition & 1 deletion output_alsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ static int _write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t g
if (silence) {
inputptr = (s32_t *) silencebuf_dop;
}
update_dop_marker((u32_t *) inputptr, out_frames);
update_dop((u32_t *) inputptr, out_frames, output.invert);
}
)

Expand Down
4 changes: 2 additions & 2 deletions output_pa.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ static int _write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t g

IF_DSD(
if (output.dop) {
update_dop_marker((u32_t *) outputbuf->readp, out_frames);
update_dop((u32_t *) outputbuf->readp, out_frames, output.invert);
}
)

Expand All @@ -327,7 +327,7 @@ static int _write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t g
IF_DSD(
if (output.dop) {
buf = silencebuf_dop;
update_dop_marker((u32_t *) buf, out_frames);
update_dop((u32_t *) buf, out_frames, output.invert);
}
)

Expand Down
2 changes: 1 addition & 1 deletion output_stdout.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static int _stdout_write_frames(frames_t out_frames, bool silence, s32_t gainL,
if (silence) {
obuf = silencebuf_dop;
}
update_dop_marker((u32_t *)obuf, out_frames);
update_dop((u32_t *)obuf, out_frames, output.invert);
}
)

Expand Down
2 changes: 1 addition & 1 deletion slimproto.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ static void process_strm(u8_t *pkt, int len) {
output.next_replay_gain = unpackN(&strm->replay_gain);
output.fade_mode = strm->transition_type - '0';
output.fade_secs = strm->transition_period;
output.polarity = strm->flags & 0x03 ? -1 : 1;
output.invert = (strm->flags & 0x03) == 0x03;
LOG_DEBUG("set fade mode: %u", output.fade_mode);
UNLOCK_O;
}
Expand Down
4 changes: 2 additions & 2 deletions squeezelite.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ struct outputstate {
u8_t *track_start; // set in decode thread
u32_t gainL; // set by slimproto
u32_t gainR; // set by slimproto
int polarity; // set by slimproto (1 or -1)
bool invert; // set by slimproto
u32_t next_replay_gain; // set by slimproto
unsigned threshold; // set by slimproto
fade_state fade;
Expand Down Expand Up @@ -634,7 +634,7 @@ void vis_stop(void);
// dop.c
#if DSD
bool is_flac_dop(u32_t *lptr, u32_t *rptr, frames_t frames);
void update_dop_marker(u32_t *ptr, frames_t frames);
void update_dop(u32_t *ptr, frames_t frames, bool invert);
void dop_silence_frames(u32_t *ptr, frames_t frames);
void dop_init(bool enable, unsigned delay);
#endif
Expand Down

0 comments on commit daf16b2

Please sign in to comment.