From 9af2b6e0072b8dc943e0311c8ac39effd159c65f Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Sat, 20 Jan 2018 15:51:13 +0100 Subject: Improved wave playback --- src/libmobile/sender.c | 4 ++-- src/libsdr/sdr.c | 9 ++++++--- src/libwave/wave.c | 28 ++++++++++++++++------------ src/libwave/wave.h | 2 +- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/libmobile/sender.c b/src/libmobile/sender.c index a46afbc..f0152b5 100644 --- a/src/libmobile/sender.c +++ b/src/libmobile/sender.c @@ -199,14 +199,14 @@ int sender_open_audio(int latspl) } } if (master->read_rx_wave) { - rc = wave_create_playback(&master->wave_rx_play, master->read_rx_wave, master->samplerate, channels, master->max_deviation); + rc = wave_create_playback(&master->wave_rx_play, master->read_rx_wave, &master->samplerate, &channels, master->max_deviation); if (rc < 0) { PDEBUG(DSENDER, DEBUG_ERROR, "Failed to create WAVE playback instance!\n"); return rc; } } if (master->read_tx_wave) { - rc = wave_create_playback(&master->wave_tx_play, master->read_tx_wave, master->samplerate, channels, master->max_deviation); + rc = wave_create_playback(&master->wave_tx_play, master->read_tx_wave, &master->samplerate, &channels, master->max_deviation); if (rc < 0) { PDEBUG(DSENDER, DEBUG_ERROR, "Failed to create WAVE playback instance!\n"); return rc; diff --git a/src/libsdr/sdr.c b/src/libsdr/sdr.c index decf5bd..dec6ab6 100644 --- a/src/libsdr/sdr.c +++ b/src/libsdr/sdr.c @@ -126,7 +126,8 @@ void *sdr_open(const char __attribute__((__unused__)) *audiodev, double *tx_freq } bandwidth = 2.0 * (max_deviation + max_modulation); - PDEBUG(DSDR, DEBUG_INFO, "Require bandwidth of each channel is 2 * (%.1f deviation + %.1f modulation) = %.1f KHz\n", max_deviation / 1e3, max_modulation / 1e3, bandwidth / 1e3); + if (bandwidth) + PDEBUG(DSDR, DEBUG_INFO, "Require bandwidth of each channel is 2 * (%.1f deviation + %.1f modulation) = %.1f KHz\n", max_deviation / 1e3, max_modulation / 1e3, bandwidth / 1e3); if (channels < 1) { PDEBUG(DSDR, DEBUG_ERROR, "No channel given, please fix!\n"); @@ -286,7 +287,8 @@ void *sdr_open(const char __attribute__((__unused__)) *audiodev, double *tx_freq } } if (sdr_config->read_iq_tx_wave) { - rc = wave_create_playback(&sdr->wave_tx_play, sdr_config->read_iq_tx_wave, samplerate, 2, 1.0); + int two = 2; + rc = wave_create_playback(&sdr->wave_tx_play, sdr_config->read_iq_tx_wave, &samplerate, &two, 1.0); if (rc < 0) { PDEBUG(DSDR, DEBUG_ERROR, "Failed to create WAVE playback instance!\n"); goto error; @@ -340,7 +342,8 @@ void *sdr_open(const char __attribute__((__unused__)) *audiodev, double *tx_freq } } if (sdr_config->read_iq_rx_wave) { - rc = wave_create_playback(&sdr->wave_rx_play, sdr_config->read_iq_rx_wave, samplerate, 2, 1.0); + int two = 2; + rc = wave_create_playback(&sdr->wave_rx_play, sdr_config->read_iq_rx_wave, &samplerate, &two, 1.0); if (rc < 0) { PDEBUG(DSDR, DEBUG_ERROR, "Failed to create WAVE playback instance!\n"); goto error; diff --git a/src/libwave/wave.c b/src/libwave/wave.c index 9d1fd0e..a2de2af 100644 --- a/src/libwave/wave.c +++ b/src/libwave/wave.c @@ -166,7 +166,7 @@ error: return rc; } -int wave_create_playback(wave_play_t *play, const char *filename, int samplerate, int channels, double max_deviation) +int wave_create_playback(wave_play_t *play, const char *filename, int *samplerate_p, int *channels_p, double max_deviation) { uint8_t buffer[256]; struct fmt fmt; @@ -176,7 +176,6 @@ int wave_create_playback(wave_play_t *play, const char *filename, int samplerate memset(&fmt, 0, sizeof(fmt)); memset(play, 0, sizeof(*play)); - play->channels = channels; play->max_deviation = max_deviation; play->fp = fopen(filename, "r"); @@ -266,23 +265,27 @@ int wave_create_playback(wave_play_t *play, const char *filename, int samplerate rc = -EINVAL; goto error; } - if (fmt.channels !=channels) { - PDEBUG(DWAVE, DEBUG_ERROR, "WAVE error: We expect %d cannel(s), but wave file only has %d channel(s)\n", channels, fmt.channels); + if (*channels_p == 0) + *channels_p = fmt.channels; + if (fmt.channels != *channels_p) { + PDEBUG(DWAVE, DEBUG_ERROR, "WAVE error: We expect %d cannel(s), but wave file only has %d channel(s)\n", *channels_p, fmt.channels); rc = -EINVAL; goto error; } - if ((int)fmt.sample_rate != samplerate) { - PDEBUG(DWAVE, DEBUG_ERROR, "WAVE error: The WAVE file's sample rate (%d) does not match our sample rate (%d)!\n", fmt.sample_rate, samplerate); + if (*samplerate_p == 0) + *samplerate_p = fmt.sample_rate; + if ((int)fmt.sample_rate != *samplerate_p) { + PDEBUG(DWAVE, DEBUG_ERROR, "WAVE error: The WAVE file's sample rate (%d) does not match our sample rate (%d)!\n", fmt.sample_rate, *samplerate_p); rc = -EINVAL; goto error; } - if ((int)fmt.data_rate != 2 * channels * samplerate) { - PDEBUG(DWAVE, DEBUG_ERROR, "WAVE error: The WAVE file's data rate is only %d bytes per second, but we expect %d bytes per second (2 bytes per sample * channels * samplerate)!\n", fmt.data_rate, 2 * channels * samplerate); + if ((int)fmt.data_rate != 2 * *channels_p * *samplerate_p) { + PDEBUG(DWAVE, DEBUG_ERROR, "WAVE error: The WAVE file's data rate is only %d bytes per second, but we expect %d bytes per second (2 bytes per sample * channels * samplerate)!\n", fmt.data_rate, 2 * *channels_p * *samplerate_p); rc = -EINVAL; goto error; } - if (fmt.bytes_sample != 2 * channels) { - PDEBUG(DWAVE, DEBUG_ERROR, "WAVE error: The WAVE file's bytes per sample is only %d, but we expect %d bytes sample (2 bytes per sample * channels)!\n", fmt.bytes_sample, 2 * channels); + if (fmt.bytes_sample != 2 * *channels_p) { + PDEBUG(DWAVE, DEBUG_ERROR, "WAVE error: The WAVE file's bytes per sample is only %d, but we expect %d bytes sample (2 bytes per sample * channels)!\n", fmt.bytes_sample, 2 * *channels_p); rc = -EINVAL; goto error; } @@ -292,9 +295,10 @@ int wave_create_playback(wave_play_t *play, const char *filename, int samplerate goto error; } - play->left = chunk / 2 / channels; + play->channels = *channels_p; + play->left = chunk / 2 / *channels_p; - play->buffer_size = samplerate * 2 * channels; + play->buffer_size = *samplerate_p * 2 * *channels_p; play->buffer = calloc(play->buffer_size, 1); if (!play->buffer) { PDEBUG(DWAVE, DEBUG_ERROR, "No mem!\n"); diff --git a/src/libwave/wave.h b/src/libwave/wave.h index b7dc34a..064f914 100644 --- a/src/libwave/wave.h +++ b/src/libwave/wave.h @@ -29,7 +29,7 @@ typedef struct wave_play { } wave_play_t; int wave_create_record(wave_rec_t *rec, const char *filename, int samplerate, int channels, double max_deviation); -int wave_create_playback(wave_play_t *play, const char *filename, int samplerate, int channels, double max_deviation); +int wave_create_playback(wave_play_t *play, const char *filename, int *samplerate_p, int *channels_p, double max_deviation); int wave_read(wave_play_t *play, sample_t **samples, int length); int wave_write(wave_rec_t *rec, sample_t **samples, int length); void wave_destroy_record(wave_rec_t *rec); -- cgit v1.2.3