2024-06-19 04:08:54 +02:00
|
|
|
package flac_example
|
|
|
|
|
|
|
|
|
|
import "core:fmt"
|
|
|
|
|
import "core:mem"
|
|
|
|
|
import "core:mem/virtual"
|
|
|
|
|
import "core:crypto/legacy/md5"
|
|
|
|
|
import "core:os"
|
|
|
|
|
import "core:time"
|
|
|
|
|
|
|
|
|
|
import "shared:flac"
|
|
|
|
|
import "shared:alsa"
|
|
|
|
|
|
|
|
|
|
DEVICE :: "default"
|
|
|
|
|
|
|
|
|
|
main :: proc() {
|
|
|
|
|
if len(os.args) < 2 {
|
|
|
|
|
fmt.eprintln("Please provide a path to a flac file.")
|
|
|
|
|
os.exit(1)
|
|
|
|
|
}
|
2024-06-20 05:00:35 +02:00
|
|
|
defer delete(os.args)
|
2024-06-19 04:08:54 +02:00
|
|
|
|
|
|
|
|
arena: virtual.Arena
|
|
|
|
|
handle: ^alsa.pcm_t
|
|
|
|
|
|
|
|
|
|
alsa_err := alsa.pcm_open(&handle, DEVICE, .PLAYBACK, .BLOCK)
|
|
|
|
|
if alsa_err < 0 {
|
|
|
|
|
fmt.eprintln("Failed to initialize audio device: ", alsa.strerror(alsa_err))
|
|
|
|
|
os.exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arena_err := virtual.arena_init_growing(&arena, mem.Megabyte * 8)
|
|
|
|
|
if arena_err != nil {
|
|
|
|
|
fmt.eprintln("Failed to alloc memory")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
allocator := virtual.arena_allocator(&arena)
|
|
|
|
|
|
|
|
|
|
start := time.now()
|
|
|
|
|
flac_data, reader, err := flac.load_from_file_buffered(os.args[1], allocator)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.println("Error while reading flac file:", err)
|
|
|
|
|
os.exit(1)
|
|
|
|
|
}
|
|
|
|
|
defer flac.destroy(flac_data, reader, allocator)
|
|
|
|
|
|
|
|
|
|
fmt.println(flac_data)
|
|
|
|
|
|
2024-06-19 21:28:33 +02:00
|
|
|
alsa_err = alsa.pcm_set_params(handle, .S16_LE, .RW_INTERLEAVED, u32(flac_data.metadata.channels), flac_data.metadata.sample_rate, 1, 500000)
|
|
|
|
|
if alsa_err < 0 {
|
|
|
|
|
fmt.eprintln("Playback open error:", alsa.strerror(alsa_err))
|
|
|
|
|
os.exit(1)
|
|
|
|
|
}
|
2024-06-19 04:08:54 +02:00
|
|
|
|
2024-06-19 21:28:33 +02:00
|
|
|
sample_rate := flac_data.metadata.sample_rate
|
|
|
|
|
channels := flac_data.metadata.channels
|
|
|
|
|
|
2024-06-20 05:00:35 +02:00
|
|
|
md5_ctx: md5.Context
|
|
|
|
|
md5.init(&md5_ctx)
|
2024-06-19 21:28:33 +02:00
|
|
|
for {
|
2024-06-20 05:00:35 +02:00
|
|
|
frame, err := flac.read_next_frame(reader, flac_data, allocator)
|
2024-06-19 21:28:33 +02:00
|
|
|
if err != nil {
|
|
|
|
|
if err == .EOF {
|
|
|
|
|
break
|
|
|
|
|
} else {
|
|
|
|
|
fmt.println("Error while decoding frame:", err)
|
|
|
|
|
os.exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//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)
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//converted_samples := make([]u8, len(samples))
|
|
|
|
|
//defer delete(converted_samples)
|
|
|
|
|
//for sample, i in samples {
|
|
|
|
|
// converted_samples[i] = u8(sample & 0xFF)
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
converted_samples := make([]i16, len(frame.samples))
|
|
|
|
|
defer delete(converted_samples)
|
|
|
|
|
for sample, i in frame.samples {
|
|
|
|
|
new_sample := sample & 0xFF
|
|
|
|
|
new_sample |= ((sample >> 8) & 0xFF) << 8
|
|
|
|
|
converted_samples[i] = i16(new_sample)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
frames := alsa.pcm_writei(handle, raw_data(converted_samples), u64(len(frame.samples)) / u64(frame.channels))
|
|
|
|
|
if frames < 0 {
|
|
|
|
|
fmt.eprintln("Error writing to PCM device:", alsa.strerror(auto_cast frames))
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
fmt.printfln("Played %d frames", frames)
|
|
|
|
|
|
2024-06-20 05:00:35 +02:00
|
|
|
flac.md5hash(&md5_ctx, flac_data.metadata.bits_per_sample, frame.samples)
|
2024-06-19 21:28:33 +02:00
|
|
|
}
|
|
|
|
|
end := time.now()
|
2024-06-19 04:08:54 +02:00
|
|
|
|
2024-06-20 05:00:35 +02:00
|
|
|
err = flac.md5sum(&md5_ctx, flac_data)
|
|
|
|
|
if err == .MD5_Mismatch {
|
|
|
|
|
fmt.println("Decoded audio data is not correct.")
|
|
|
|
|
os.exit(1)
|
|
|
|
|
}
|
2024-06-19 04:08:54 +02:00
|
|
|
|
2024-06-19 21:28:33 +02:00
|
|
|
alsa_err = alsa.pcm_drain(handle)
|
|
|
|
|
if alsa_err < 0 {
|
|
|
|
|
fmt.eprintln("pcm_drain failed with:", alsa.strerror(alsa_err))
|
|
|
|
|
}
|
|
|
|
|
alsa.pcm_close(handle)
|
|
|
|
|
|
|
|
|
|
elapsed := time.diff(start, end)
|
|
|
|
|
fmt.println("Decoding took", elapsed)
|
2024-06-19 04:08:54 +02:00
|
|
|
}
|