audio_format: added audio_format_sample_size()
The inline function audio_format_sample_size() calculates how many bytes each sample consumes. This function already takes into account that 24 bit samples are 4 bytes long, not 3.
This commit is contained in:
		| @@ -289,7 +289,7 @@ configure_hw: | |||||||
| 	if (err < 0) | 	if (err < 0) | ||||||
| 		goto error; | 		goto error; | ||||||
|  |  | ||||||
| 	ad->sampleSize = (audioFormat->bits / 8) * audioFormat->channels; | 	ad->sampleSize = audio_format_sample_size(audioFormat) * audioFormat->channels; | ||||||
|  |  | ||||||
| 	audioOutput->open = 1; | 	audioOutput->open = 1; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -268,7 +268,7 @@ static int osx_openDevice(struct audio_output *audioOutput) | |||||||
| #endif | #endif | ||||||
|  |  | ||||||
| 	streamDesc.mBytesPerPacket = | 	streamDesc.mBytesPerPacket = | ||||||
| 	    audioFormat->channels * audioFormat->bits / 8; | 		audioFormat->channels * audio_format_sample_size(audioFormat); | ||||||
| 	streamDesc.mFramesPerPacket = 1; | 	streamDesc.mFramesPerPacket = 1; | ||||||
| 	streamDesc.mBytesPerFrame = streamDesc.mBytesPerPacket; | 	streamDesc.mBytesPerFrame = streamDesc.mBytesPerPacket; | ||||||
| 	streamDesc.mChannelsPerFrame = audioFormat->channels; | 	streamDesc.mChannelsPerFrame = audioFormat->channels; | ||||||
|   | |||||||
| @@ -141,7 +141,7 @@ static int shout_mp3_encoder_encode(struct shout_data *sd, | |||||||
| 	float (*lamebuf)[2]; | 	float (*lamebuf)[2]; | ||||||
| 	struct shout_buffer *buf = &(sd->buf); | 	struct shout_buffer *buf = &(sd->buf); | ||||||
| 	unsigned int samples; | 	unsigned int samples; | ||||||
| 	int bytes = sd->audio_format.bits / 8; | 	int bytes = audio_format_sample_size(&sd->audio_format); | ||||||
| 	struct lame_data *ld = (struct lame_data *)sd->encoder_data; | 	struct lame_data *ld = (struct lame_data *)sd->encoder_data; | ||||||
| 	int bytes_out; | 	int bytes_out; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -257,7 +257,7 @@ static int shout_ogg_encoder_encode(struct shout_data *sd, | |||||||
| 	int j; | 	int j; | ||||||
| 	float **vorbbuf; | 	float **vorbbuf; | ||||||
| 	unsigned int samples; | 	unsigned int samples; | ||||||
| 	int bytes = sd->audio_format.bits / 8; | 	int bytes = audio_format_sample_size(&sd->audio_format); | ||||||
| 	struct ogg_vorbis_data *od = (struct ogg_vorbis_data *)sd->encoder_data; | 	struct ogg_vorbis_data *od = (struct ogg_vorbis_data *)sd->encoder_data; | ||||||
|  |  | ||||||
| 	samples = size / (bytes * sd->audio_format.channels); | 	samples = size / (bytes * sd->audio_format.channels); | ||||||
|   | |||||||
| @@ -47,14 +47,27 @@ static inline int audio_format_equals(const struct audio_format *a, | |||||||
| 		a->channels == b->channels; | 		a->channels == b->channels; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Returns the size of each (mono) sample in bytes. | ||||||
|  |  */ | ||||||
|  | static inline unsigned audio_format_sample_size(const struct audio_format *af) | ||||||
|  | { | ||||||
|  | 	if (af->bits <= 8) | ||||||
|  | 		return 1; | ||||||
|  | 	else if (af->bits <= 16) | ||||||
|  | 		return 2; | ||||||
|  | 	else | ||||||
|  | 		return 4; | ||||||
|  | } | ||||||
|  |  | ||||||
| static inline double audio_format_time_to_size(const struct audio_format *af) | static inline double audio_format_time_to_size(const struct audio_format *af) | ||||||
| { | { | ||||||
| 	return af->sampleRate * af->bits * af->channels / 8.0; | 	return af->sampleRate * af->channels * audio_format_sample_size(af); | ||||||
| } | } | ||||||
|  |  | ||||||
| static inline double audioFormatSizeToTime(const struct audio_format *af) | static inline double audioFormatSizeToTime(const struct audio_format *af) | ||||||
| { | { | ||||||
| 	return 8.0 / af->bits / af->channels / af->sampleRate; | 	return 1.0 / audio_format_time_to_size(af); | ||||||
| } | } | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
|   | |||||||
| @@ -237,7 +237,8 @@ static FLAC__StreamDecoderWriteStatus flacWrite(const flac_decoder *dec, | |||||||
| 	FLAC__uint32 samples = frame->header.blocksize; | 	FLAC__uint32 samples = frame->header.blocksize; | ||||||
| 	unsigned int c_samp; | 	unsigned int c_samp; | ||||||
| 	const unsigned int num_channels = frame->header.channels; | 	const unsigned int num_channels = frame->header.channels; | ||||||
| 	const unsigned int bytes_per_sample = (data->audio_format.bits / 8); | 	const unsigned int bytes_per_sample = | ||||||
|  | 		audio_format_sample_size(&data->audio_format); | ||||||
| 	const unsigned int bytes_per_channel = | 	const unsigned int bytes_per_channel = | ||||||
| 		bytes_per_sample * frame->header.channels; | 		bytes_per_sample * frame->header.channels; | ||||||
| 	const unsigned int max_samples = FLAC_CHUNK_SIZE / bytes_per_channel; | 	const unsigned int max_samples = FLAC_CHUNK_SIZE / bytes_per_channel; | ||||||
|   | |||||||
| @@ -160,7 +160,7 @@ static FLAC__StreamDecoderWriteStatus oggflacWrite(mpd_unused const | |||||||
| 	FLAC__uint16 u16; | 	FLAC__uint16 u16; | ||||||
| 	unsigned char *uc; | 	unsigned char *uc; | ||||||
| 	unsigned int c_samp, c_chan; | 	unsigned int c_samp, c_chan; | ||||||
| 	int i; | 	unsigned int i; | ||||||
| 	float timeChange; | 	float timeChange; | ||||||
|  |  | ||||||
| 	timeChange = ((float)samples) / frame->header.sample_rate; | 	timeChange = ((float)samples) / frame->header.sample_rate; | ||||||
| @@ -183,7 +183,7 @@ static FLAC__StreamDecoderWriteStatus oggflacWrite(mpd_unused const | |||||||
| 		     c_chan++) { | 		     c_chan++) { | ||||||
| 			u16 = buf[c_chan][c_samp]; | 			u16 = buf[c_chan][c_samp]; | ||||||
| 			uc = (unsigned char *)&u16; | 			uc = (unsigned char *)&u16; | ||||||
| 			for (i = 0; i < (data->audio_format.bits / 8); i++) { | 			for (i = 0; i < audio_format_sample_size(&data->audio_format); i++) { | ||||||
| 				if (data->chunk_length >= FLAC_CHUNK_SIZE) { | 				if (data->chunk_length >= FLAC_CHUNK_SIZE) { | ||||||
| 					if (flacSendChunk(data) < 0) { | 					if (flacSendChunk(data) < 0) { | ||||||
| 						return | 						return | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Max Kellermann
					Max Kellermann