Skip to content

Commit 9261673

Browse files
committed
WIP Set Reader position
Removes 32 audio packet start delay that I had due to clock. Timing is still weird.
1 parent d313ea7 commit 9261673

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

src/output.c

+24-31
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ static unsigned int elastic_average_samples(const output_t *st, const elastic_bu
3939
return dec->avg * (st->radio->mode == NRSC5_MODE_FM ? RADIO_FRAME_SAMPLES_FM : RADIO_FRAME_SAMPLES_AM);
4040
}
4141

42-
static unsigned int elastic_write_available(const elastic_buffer_t *elastic)
43-
{
44-
return elastic->size - elastic->write + elastic->read - 1;
45-
}
46-
4742
#ifdef USE_FAAD2
4843
static unsigned int decoder_buffer_write_available(const decoder_t *st)
4944
{
@@ -208,12 +203,14 @@ void output_align(output_t *st, unsigned int program, unsigned int stream_id, un
208203
elastic->ptr[i].seq = -1;
209204
}
210205

211-
elastic->clock = st->radio->mode == NRSC5_MODE_FM ? FFT_FM : FFT_AM;
212-
elastic->write = seq;
213-
elastic->ptr[elastic->write].seq = seq;
206+
elastic->clock = elastic_average_samples(st, elastic);
207+
208+
unsigned int offset = pdu_seq * avg;
209+
210+
if ((offset + MAX_AUDIO_PACKETS - seq) % MAX_AUDIO_PACKETS >= 32)
211+
offset = (offset + 32) % MAX_AUDIO_PACKETS;
214212

215-
// TODO: Calculate current delay based on sequence number
216-
elastic->read = (seq - elastic->latency) % elastic->size;
213+
elastic->read = (offset - elastic->latency) % elastic->size;
217214
}
218215

219216
#ifdef USE_FAAD2
@@ -272,10 +269,26 @@ void output_advance_elastic(output_t *st, int pos, unsigned int used)
272269
}
273270
elastic->clock -= sample_avg;
274271
}
275-
276272
}
277273
}
278274

275+
void output_push(output_t *st, uint8_t *pkt, unsigned int len, unsigned int program, unsigned int stream_id, unsigned int seq)
276+
{
277+
elastic_buffer_t *elastic = &st->elastic[program][stream_id];
278+
279+
if (stream_id != 0)
280+
return; // TODO: Process enhanced stream
281+
282+
if (elastic->ptr[seq].size != 0)
283+
log_warn("Packet %d already exists in elastic buffer for program %d. Overwriting.", seq, program);
284+
285+
memcpy(elastic->ptr[seq].data, pkt, len);
286+
elastic->ptr[seq].size = len;
287+
elastic->ptr[seq].seq = seq;
288+
289+
elastic->last_write = seq;
290+
}
291+
279292
void output_advance(output_t *st, unsigned int len, int mode)
280293
{
281294
#ifdef USE_FAAD2
@@ -329,23 +342,6 @@ void output_advance(output_t *st, unsigned int len, int mode)
329342
#endif
330343
}
331344

332-
void output_push(output_t *st, uint8_t *pkt, unsigned int len, unsigned int program, unsigned int stream_id, unsigned int seq)
333-
{
334-
elastic_buffer_t *elastic = &st->elastic[program][stream_id];
335-
336-
if (stream_id != 0)
337-
return; // TODO: Process enhanced stream
338-
339-
if (elastic->ptr[seq].size != 0)
340-
log_warn("Packet %d already exists in elastic buffer for program %d. Overwriting.", seq, program);
341-
342-
memcpy(elastic->ptr[seq].data, pkt, len);
343-
elastic->ptr[seq].size = len;
344-
elastic->ptr[seq].seq = seq;
345-
346-
elastic->write = seq;
347-
}
348-
349345
static void aas_free_lot(aas_file_t *file)
350346
{
351347
free(file->name);
@@ -391,7 +387,6 @@ void output_reset(output_t *st)
391387
{
392388
elastic_buffer_t *buffer = &st->elastic[i][j];
393389

394-
buffer->write = 0;
395390
buffer->read = 0;
396391
buffer->clock = 0;
397392

@@ -428,8 +423,6 @@ void output_init(output_t *st, nrsc5_t *radio)
428423
memset(st->services, 0, sizeof(st->services));
429424

430425
#ifdef USE_FAAD2
431-
memset(st->decoder, 0, sizeof(st->decoder));
432-
memset(st->elastic, 0, sizeof(st->elastic));
433426
memset(st->silence, 0, sizeof(st->silence));
434427
#endif
435428

src/output.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ typedef struct
113113
{
114114
packet_t *ptr;
115115

116-
unsigned int size, read, write;
116+
unsigned int size, read, last_write;
117117
unsigned int latency, avg, delay;
118118

119119
unsigned int clock;

0 commit comments

Comments
 (0)