From f444efc5afdb5dc402ec723753ecad84c85dc564 Mon Sep 17 00:00:00 2001 From: IllusionMan1212 Date: Tue, 24 Sep 2024 23:07:49 +0200 Subject: [PATCH] improve the linux example --- example/{main.odin => main_linux.odin} | 55 ++++++++++++++++---------- 1 file changed, 35 insertions(+), 20 deletions(-) rename example/{main.odin => main_linux.odin} (75%) diff --git a/example/main.odin b/example/main_linux.odin similarity index 75% rename from example/main.odin rename to example/main_linux.odin index 7e04a92..854ba2e 100644 --- a/example/main.odin +++ b/example/main_linux.odin @@ -1,4 +1,4 @@ -package flac_example +package flac_linux_example import "core:fmt" import "core:mem" @@ -51,9 +51,13 @@ main :: proc() { os.exit(1) } - sample_rate := flac_data.metadata.sample_rate + // TODO: channel mapping is incorrect for anything with more than 2 channels. channels := flac_data.metadata.channels + chmap := alsa.pcm_get_chmap(handle) + + fmt.println(chmap) + md5_ctx: md5.Context md5.init(&md5_ctx) for { @@ -67,20 +71,30 @@ main :: proc() { } } - //if frame.sample_rate != sample_rate || frame.channels != channels { - // sample_rate = frame.sample_rate - // channels = frame.channels - // alsa_err = alsa.pcm_set_params(handle, .S16_LE, .RW_INTERLEAVED, u32(channels), sample_rate, 1, 0) - // if alsa_err < 0 { - // fmt.eprintln("Playback open error:", alsa.strerror(alsa_err)) - // os.exit(1) - // } - //} + if frame.channels != channels { + // Finish playing the previous frame before changing the hw params. + alsa.pcm_drain(handle) + + channels = frame.channels + alsa_err = alsa.pcm_set_params(handle, .S16_LE, .RW_INTERLEAVED, u32(channels), flac_data.metadata.sample_rate, 1, 500000) + if alsa_err < 0 { + fmt.eprintln("Playback open error:", alsa.strerror(alsa_err)) + os.exit(1) + } + } + + fmt.println("Channels:", frame.channels) + + if frame.channels == 6 { + fmt.println("Sample:", len(frame.samples)) + } + + // TODO: resampling // 8 bps audio - //converted_samples := make([]u8, len(samples)) + //converted_samples := make([]u8, len(frame.samples)) //defer delete(converted_samples) - //for sample, i in samples { + //for sample, i in frame.samples { // converted_samples[i] = u8(sample & 0xFF) //} @@ -102,13 +116,6 @@ main :: proc() { flac.md5hash(&md5_ctx, flac_data.metadata.bits_per_sample, frame.samples) } - end := time.now() - - err = flac.md5sum(&md5_ctx, flac_data) - if err == .MD5_Mismatch { - fmt.println("Decoded audio data is not correct.") - os.exit(1) - } alsa_err = alsa.pcm_drain(handle) if alsa_err < 0 { @@ -116,6 +123,14 @@ main :: proc() { } alsa.pcm_close(handle) + end := time.now() + + err = flac.md5sum(&md5_ctx, flac_data) + if err == .MD5_Mismatch { + fmt.printfln("Decoded audio data is not correct. Expected MD5: %v, Got: %v", flac_data.metadata.expected_md5, flac_data.metadata.calculated_md5) + os.exit(1) + } + elapsed := time.diff(start, end) fmt.println("Decoding took", elapsed) }