Commit 37754559 authored by David Woodhouse's avatar David Woodhouse

Add audio_format_init() function

It makes no difference right now, but we're about to add an endianness flag and will want to make sure it's correctly initialised every time.
parent 4100035b
...@@ -36,6 +36,15 @@ static inline void audio_format_clear(struct audio_format *af) ...@@ -36,6 +36,15 @@ static inline void audio_format_clear(struct audio_format *af)
af->channels = 0; af->channels = 0;
} }
static inline void audio_format_init(struct audio_format *af,
uint32_t sample_rate,
uint8_t bits, uint8_t channels)
{
af->sample_rate = sample_rate;
af->bits = bits;
af->channels = channels;
}
static inline bool audio_format_defined(const struct audio_format *af) static inline bool audio_format_defined(const struct audio_format *af)
{ {
return af->sample_rate != 0; return af->sample_rate != 0;
......
...@@ -41,6 +41,8 @@ audio_format_parse(struct audio_format *dest, const char *src, GError **error) ...@@ -41,6 +41,8 @@ audio_format_parse(struct audio_format *dest, const char *src, GError **error)
{ {
char *endptr; char *endptr;
unsigned long value; unsigned long value;
uint32_t rate;
uint8_t bits, channels;
audio_format_clear(dest); audio_format_clear(dest);
...@@ -61,7 +63,7 @@ audio_format_parse(struct audio_format *dest, const char *src, GError **error) ...@@ -61,7 +63,7 @@ audio_format_parse(struct audio_format *dest, const char *src, GError **error)
return false; return false;
} }
dest->sample_rate = value; rate = value;
/* parse sample format */ /* parse sample format */
...@@ -81,7 +83,7 @@ audio_format_parse(struct audio_format *dest, const char *src, GError **error) ...@@ -81,7 +83,7 @@ audio_format_parse(struct audio_format *dest, const char *src, GError **error)
return false; return false;
} }
dest->bits = value; bits = value;
/* parse channel count */ /* parse channel count */
...@@ -93,7 +95,9 @@ audio_format_parse(struct audio_format *dest, const char *src, GError **error) ...@@ -93,7 +95,9 @@ audio_format_parse(struct audio_format *dest, const char *src, GError **error)
return false; return false;
} }
dest->channels = value; channels = value;
audio_format_init(dest, rate, bits, channels);
return true; return true;
} }
...@@ -195,9 +195,8 @@ void flac_metadata_common_cb(const FLAC__StreamMetadata * block, ...@@ -195,9 +195,8 @@ void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
switch (block->type) { switch (block->type) {
case FLAC__METADATA_TYPE_STREAMINFO: case FLAC__METADATA_TYPE_STREAMINFO:
data->audio_format.bits = (int8_t)si->bits_per_sample; audio_format_init(&data->audio_format, si->sample_rate,
data->audio_format.sample_rate = si->sample_rate; si->bits_per_sample, si->channels);
data->audio_format.channels = (int8_t)si->channels;
data->total_time = ((float)si->total_samples) / (si->sample_rate); data->total_time = ((float)si->total_samples) / (si->sample_rate);
break; break;
case FLAC__METADATA_TYPE_VORBIS_COMMENT: case FLAC__METADATA_TYPE_VORBIS_COMMENT:
......
...@@ -136,11 +136,9 @@ audiofile_stream_decode(struct decoder *decoder, struct input_stream *is) ...@@ -136,11 +136,9 @@ audiofile_stream_decode(struct decoder *decoder, struct input_stream *is)
afSetVirtualSampleFormat(af_fp, AF_DEFAULT_TRACK, afSetVirtualSampleFormat(af_fp, AF_DEFAULT_TRACK,
AF_SAMPFMT_TWOSCOMP, bits); AF_SAMPFMT_TWOSCOMP, bits);
afGetVirtualSampleFormat(af_fp, AF_DEFAULT_TRACK, &fs, &bits); afGetVirtualSampleFormat(af_fp, AF_DEFAULT_TRACK, &fs, &bits);
audio_format.bits = (uint8_t)bits;
audio_format.sample_rate = audio_format_init(&audio_format, afGetRate(af_fp, AF_DEFAULT_TRACK),
(unsigned int)afGetRate(af_fp, AF_DEFAULT_TRACK); bits, afGetVirtualChannels(af_fp, AF_DEFAULT_TRACK));
audio_format.channels =
(uint8_t)afGetVirtualChannels(af_fp, AF_DEFAULT_TRACK);
if (!audio_format_valid(&audio_format)) { if (!audio_format_valid(&audio_format)) {
g_warning("Invalid audio format: %u:%u:%u\n", g_warning("Invalid audio format: %u:%u:%u\n",
......
...@@ -262,11 +262,7 @@ faad_decoder_init(faacDecHandle decoder, struct decoder_buffer *buffer, ...@@ -262,11 +262,7 @@ faad_decoder_init(faacDecHandle decoder, struct decoder_buffer *buffer,
decoder_buffer_consume(buffer, nbytes); decoder_buffer_consume(buffer, nbytes);
*audio_format = (struct audio_format){ audio_format_init(audio_format, sample_rate, 16, channels);
.bits = 16,
.channels = channels,
.sample_rate = sample_rate,
};
return true; return true;
} }
......
...@@ -267,6 +267,7 @@ ffmpeg_decode_internal(struct ffmpeg_context *ctx) ...@@ -267,6 +267,7 @@ ffmpeg_decode_internal(struct ffmpeg_context *ctx)
struct audio_format audio_format; struct audio_format audio_format;
enum decoder_command cmd; enum decoder_command cmd;
int total_time; int total_time;
uint8_t bits;
total_time = 0; total_time = 0;
...@@ -275,13 +276,13 @@ ffmpeg_decode_internal(struct ffmpeg_context *ctx) ...@@ -275,13 +276,13 @@ ffmpeg_decode_internal(struct ffmpeg_context *ctx)
} }
#if LIBAVCODEC_VERSION_INT >= ((51<<16)+(41<<8)+0) #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(41<<8)+0)
audio_format.bits = (uint8_t) av_get_bits_per_sample_format(codec_context->sample_fmt); bits = (uint8_t) av_get_bits_per_sample_format(codec_context->sample_fmt);
#else #else
/* XXX fixme 16-bit for older ffmpeg (13 Aug 2007) */ /* XXX fixme 16-bit for older ffmpeg (13 Aug 2007) */
audio_format.bits = (uint8_t) 16; bits = (uint8_t) 16;
#endif #endif
audio_format.sample_rate = (unsigned int)codec_context->sample_rate; audio_format_init(&audio_format, codec_context->sample_rate, bits,
audio_format.channels = codec_context->channels; codec_context->channels);
if (!audio_format_valid(&audio_format)) { if (!audio_format_valid(&audio_format)) {
g_warning("Invalid audio format: %u:%u:%u\n", g_warning("Invalid audio format: %u:%u:%u\n",
......
...@@ -1148,13 +1148,6 @@ mp3_read(struct mp3_data *data, struct replay_gain_info **replay_gain_info_r) ...@@ -1148,13 +1148,6 @@ mp3_read(struct mp3_data *data, struct replay_gain_info **replay_gain_info_r)
return ret != DECODE_BREAK; return ret != DECODE_BREAK;
} }
static void mp3_audio_format(struct mp3_data *data, struct audio_format *af)
{
af->bits = 24;
af->sample_rate = (data->frame).header.samplerate;
af->channels = MAD_NCHANNELS(&(data->frame).header);
}
static void static void
mp3_decode(struct decoder *decoder, struct input_stream *input_stream) mp3_decode(struct decoder *decoder, struct input_stream *input_stream)
{ {
...@@ -1170,7 +1163,8 @@ mp3_decode(struct decoder *decoder, struct input_stream *input_stream) ...@@ -1170,7 +1163,8 @@ mp3_decode(struct decoder *decoder, struct input_stream *input_stream)
return; return;
} }
mp3_audio_format(&data, &audio_format); audio_format_init(&audio_format, data.frame.header.samplerate, 24,
MAD_NCHANNELS(&data.frame.header));
decoder_initialized(decoder, &audio_format, decoder_initialized(decoder, &audio_format,
data.input_stream->seekable, data.total_time); data.input_stream->seekable, data.total_time);
......
...@@ -175,9 +175,7 @@ mod_decode(struct decoder *decoder, const char *path) ...@@ -175,9 +175,7 @@ mod_decode(struct decoder *decoder, const char *path)
return; return;
} }
audio_format.bits = 16; audio_format_init(&audio_format, 44100, 16, 2);
audio_format.sample_rate = 44100;
audio_format.channels = 2;
secPerByte = secPerByte =
1.0 / ((audio_format.bits * audio_format.channels / 8.0) * 1.0 / ((audio_format.bits * audio_format.channels / 8.0) *
......
...@@ -121,9 +121,7 @@ mod_decode(struct decoder *decoder, struct input_stream *is) ...@@ -121,9 +121,7 @@ mod_decode(struct decoder *decoder, struct input_stream *is)
return; return;
} }
audio_format.bits = 16; audio_format_init(&audio_format, 44100, 16, 2);
audio_format.sample_rate = 44100;
audio_format.channels = 2;
sec_perbyte = sec_perbyte =
1.0 / ((audio_format.bits * audio_format.channels / 8.0) * 1.0 / ((audio_format.bits * audio_format.channels / 8.0) *
......
...@@ -131,11 +131,7 @@ mp4_faad_new(mp4ff_t *mp4fh, int *track_r, struct audio_format *audio_format) ...@@ -131,11 +131,7 @@ mp4_faad_new(mp4ff_t *mp4fh, int *track_r, struct audio_format *audio_format)
} }
*track_r = track; *track_r = track;
*audio_format = (struct audio_format){ audio_format_init(audio_format, sample_rate, 16, channels);
.bits = 16,
.channels = channels,
.sample_rate = sample_rate,
};
if (!audio_format_valid(audio_format)) { if (!audio_format_valid(audio_format)) {
g_warning("Invalid audio format: %u:%u:%u\n", g_warning("Invalid audio format: %u:%u:%u\n",
......
...@@ -193,9 +193,7 @@ mpcdec_decode(struct decoder *mpd_decoder, struct input_stream *is) ...@@ -193,9 +193,7 @@ mpcdec_decode(struct decoder *mpd_decoder, struct input_stream *is)
mpc_demux_get_info(demux, &info); mpc_demux_get_info(demux, &info);
#endif #endif
audio_format.bits = 24; audio_format_init(&audio_format, info.sample_freq, 24, info.channels);
audio_format.channels = info.channels;
audio_format.sample_rate = info.sample_freq;
if (!audio_format_valid(&audio_format)) { if (!audio_format_valid(&audio_format)) {
#ifndef MPC_IS_OLD_API #ifndef MPC_IS_OLD_API
......
...@@ -103,9 +103,7 @@ sidplay_file_decode(struct decoder *decoder, const char *path_fs) ...@@ -103,9 +103,7 @@ sidplay_file_decode(struct decoder *decoder, const char *path_fs)
/* initialize the MPD decoder */ /* initialize the MPD decoder */
struct audio_format audio_format; struct audio_format audio_format;
audio_format.sample_rate = 48000; audio_format_init(&audio_format, 48000, 16, 2);
audio_format.bits = 16;
audio_format.channels = 2;
decoder_initialized(decoder, &audio_format, false, -1); decoder_initialized(decoder, &audio_format, false, -1);
......
...@@ -124,12 +124,10 @@ sndfile_stream_decode(struct decoder *decoder, struct input_stream *is) ...@@ -124,12 +124,10 @@ sndfile_stream_decode(struct decoder *decoder, struct input_stream *is)
return; return;
} }
audio_format.sample_rate = info.samplerate;
/* for now, always read 32 bit samples. Later, we could lower /* for now, always read 32 bit samples. Later, we could lower
MPD's CPU usage by reading 16 bit samples with MPD's CPU usage by reading 16 bit samples with
sf_readf_short() on low-quality source files. */ sf_readf_short() on low-quality source files. */
audio_format.bits = 32; audio_format_init(&audio_format, info.samplerate, 32, info.channels);
audio_format.channels = info.channels;
if (!audio_format_valid(&audio_format)) { if (!audio_format_valid(&audio_format)) {
g_warning("invalid audio format"); g_warning("invalid audio format");
......
...@@ -324,8 +324,7 @@ vorbis_stream_decode(struct decoder *decoder, ...@@ -324,8 +324,7 @@ vorbis_stream_decode(struct decoder *decoder,
vorbis_info *vi = ov_info(&vf, -1); vorbis_info *vi = ov_info(&vf, -1);
struct replay_gain_info *new_rgi; struct replay_gain_info *new_rgi;
audio_format.channels = vi->channels; audio_format_init(&audio_format, vi->rate, 16, vi->channels);
audio_format.sample_rate = vi->rate;
if (!audio_format_valid(&audio_format)) { if (!audio_format_valid(&audio_format)) {
g_warning("Invalid audio format: %u:%u:%u\n", g_warning("Invalid audio format: %u:%u:%u\n",
......
...@@ -145,9 +145,9 @@ wavpack_decode(struct decoder *decoder, WavpackContext *wpc, bool can_seek, ...@@ -145,9 +145,9 @@ wavpack_decode(struct decoder *decoder, WavpackContext *wpc, bool can_seek,
int bytes_per_sample, output_sample_size; int bytes_per_sample, output_sample_size;
int position; int position;
audio_format.sample_rate = WavpackGetSampleRate(wpc); audio_format_init(&audio_format, WavpackGetSampleRate(wpc),
audio_format.channels = WavpackGetReducedChannels(wpc); WavpackGetBitsPerSample(wpc),
audio_format.bits = WavpackGetBitsPerSample(wpc); WavpackGetReducedChannels(wpc));
/* round bitwidth to 8-bit units */ /* round bitwidth to 8-bit units */
audio_format.bits = (audio_format.bits + 7) & (~7); audio_format.bits = (audio_format.bits + 7) & (~7);
......
...@@ -41,11 +41,7 @@ encoder_to_stdout(struct encoder *encoder) ...@@ -41,11 +41,7 @@ encoder_to_stdout(struct encoder *encoder)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
GError *error = NULL; GError *error = NULL;
struct audio_format audio_format = { struct audio_format audio_format;
.sample_rate = 44100,
.bits = 16,
.channels = 2,
};
bool ret; bool ret;
const char *encoder_name; const char *encoder_name;
const struct encoder_plugin *plugin; const struct encoder_plugin *plugin;
...@@ -66,6 +62,8 @@ int main(int argc, char **argv) ...@@ -66,6 +62,8 @@ int main(int argc, char **argv)
else else
encoder_name = "vorbis"; encoder_name = "vorbis";
audio_format_init(&audio_format, 44100, 16, 2);
/* create the encoder */ /* create the encoder */
plugin = encoder_plugin_get(encoder_name); plugin = encoder_plugin_get(encoder_name);
......
...@@ -70,11 +70,7 @@ load_filter(const char *name) ...@@ -70,11 +70,7 @@ load_filter(const char *name)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct audio_format audio_format = { struct audio_format audio_format;
.sample_rate = 44100,
.bits = 16,
.channels = 2,
};
bool success; bool success;
GError *error = NULL; GError *error = NULL;
struct filter *filter; struct filter *filter;
...@@ -87,6 +83,8 @@ int main(int argc, char **argv) ...@@ -87,6 +83,8 @@ int main(int argc, char **argv)
return 1; return 1;
} }
audio_format_init(&audio_format, 44100, 16, 2);
g_thread_init(NULL); g_thread_init(NULL);
/* read configuration file (mpd.conf) */ /* read configuration file (mpd.conf) */
......
...@@ -100,11 +100,7 @@ load_audio_output(struct audio_output *ao, const char *name) ...@@ -100,11 +100,7 @@ load_audio_output(struct audio_output *ao, const char *name)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct audio_output ao; struct audio_output ao;
struct audio_format audio_format = { struct audio_format audio_format;
.sample_rate = 44100,
.bits = 16,
.channels = 2,
};
bool success; bool success;
GError *error = NULL; GError *error = NULL;
char buffer[4096]; char buffer[4096];
...@@ -116,6 +112,8 @@ int main(int argc, char **argv) ...@@ -116,6 +112,8 @@ int main(int argc, char **argv)
return 1; return 1;
} }
audio_format_init(&audio_format, 44100, 16, 2);
g_thread_init(NULL); g_thread_init(NULL);
/* read configuration file (mpd.conf) */ /* read configuration file (mpd.conf) */
......
...@@ -35,11 +35,7 @@ ...@@ -35,11 +35,7 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
GError *error = NULL; GError *error = NULL;
struct audio_format audio_format = { struct audio_format audio_format;
.sample_rate = 48000,
.bits = 16,
.channels = 2,
};
bool ret; bool ret;
static char buffer[4096]; static char buffer[4096];
ssize_t nbytes; ssize_t nbytes;
...@@ -57,6 +53,7 @@ int main(int argc, char **argv) ...@@ -57,6 +53,7 @@ int main(int argc, char **argv)
return 1; return 1;
} }
} }
audio_format_init(&audio_format, 48000, 16, 2);
while ((nbytes = read(0, buffer, sizeof(buffer))) > 0) { while ((nbytes = read(0, buffer, sizeof(buffer))) > 0) {
pcm_volume(buffer, nbytes, &audio_format, PCM_VOLUME_1 / 2); pcm_volume(buffer, nbytes, &audio_format, PCM_VOLUME_1 / 2);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment