aboutsummaryrefslogtreecommitdiffstats
path: root/src/libwave
diff options
context:
space:
mode:
authorAndreas Eversberg <jolly@eversberg.eu>2018-01-20 15:51:13 +0100
committerAndreas Eversberg <jolly@eversberg.eu>2018-02-16 15:54:40 +0100
commit9af2b6e0072b8dc943e0311c8ac39effd159c65f (patch)
tree23b4014a89c8f15318b5cc4c3e9adb0d3a063ce2 /src/libwave
parent3a5fa8837cb6b6c9fefdfc02bbe13473eeef32d1 (diff)
Improved wave playback
Diffstat (limited to 'src/libwave')
-rw-r--r--src/libwave/wave.c28
-rw-r--r--src/libwave/wave.h2
2 files changed, 17 insertions, 13 deletions
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);