2020-08-23 19:49:16 +02:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import sokol.audio
|
|
|
|
|
|
|
|
struct Player {
|
|
|
|
mut:
|
|
|
|
samples []f32
|
|
|
|
pos int
|
|
|
|
finished bool
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
if os.args.len < 2 {
|
|
|
|
eprintln('Usage: play_wav file1.wav file2.wav ...')
|
2020-10-26 12:14:21 +01:00
|
|
|
play_sounds([os.resource_abs_path('uhoh.wav')]) ?
|
2020-08-23 19:49:16 +02:00
|
|
|
exit(1)
|
|
|
|
}
|
2020-10-26 12:14:21 +01:00
|
|
|
play_sounds(os.args[1..]) ?
|
2020-08-23 19:49:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn play_sounds(files []string) ? {
|
|
|
|
mut player := Player{}
|
|
|
|
player.init()
|
|
|
|
for f in files {
|
|
|
|
if !os.exists(f) || os.is_dir(f) {
|
|
|
|
eprintln('skipping "$f" (does not exist)')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fext := os.file_ext(f).to_lower()
|
|
|
|
if fext != '.wav' {
|
|
|
|
eprintln('skipping "$f" (not a .wav file)')
|
|
|
|
continue
|
|
|
|
}
|
2020-10-26 12:14:21 +01:00
|
|
|
player.play_wav_file(f) ?
|
2020-08-23 19:49:16 +02:00
|
|
|
}
|
|
|
|
player.stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2020-10-26 12:14:21 +01:00
|
|
|
fn audio_player_callback(buffer &f32, num_frames int, num_channels int, mut p Player) {
|
2020-08-23 19:49:16 +02:00
|
|
|
if p.finished {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ntotal := num_channels * num_frames
|
|
|
|
nremaining := p.samples.len - p.pos
|
|
|
|
nsamples := if nremaining < ntotal { nremaining } else { ntotal }
|
|
|
|
if nsamples <= 0 {
|
|
|
|
p.finished = true
|
|
|
|
return
|
|
|
|
}
|
2022-04-11 21:23:06 +02:00
|
|
|
unsafe { vmemcpy(buffer, &p.samples[p.pos], nsamples * int(sizeof(f32))) }
|
2020-08-23 19:49:16 +02:00
|
|
|
p.pos += nsamples
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Player) init() {
|
2021-01-22 21:22:27 +01:00
|
|
|
audio.setup(
|
2020-08-23 19:49:16 +02:00
|
|
|
num_channels: 2
|
|
|
|
stream_userdata_cb: audio_player_callback
|
|
|
|
user_data: p
|
2021-01-22 21:22:27 +01:00
|
|
|
)
|
2020-08-23 19:49:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Player) stop() {
|
|
|
|
audio.shutdown()
|
|
|
|
p.free()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Player) play_wav_file(fpath string) ? {
|
|
|
|
println('> play_wav_file: $fpath')
|
2020-10-26 12:14:21 +01:00
|
|
|
samples := read_wav_file_samples(fpath) ?
|
2020-08-23 19:49:16 +02:00
|
|
|
p.finished = true
|
|
|
|
p.samples << samples
|
|
|
|
p.finished = false
|
|
|
|
for !p.finished {
|
2021-02-27 18:41:06 +01:00
|
|
|
time.sleep(16 * time.millisecond)
|
2020-08-23 19:49:16 +02:00
|
|
|
}
|
|
|
|
p.free()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Player) free() {
|
|
|
|
p.finished = false
|
|
|
|
p.samples = []f32{}
|
|
|
|
p.pos = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// The read_wav_file_samples function below is based on the following sources:
|
|
|
|
// http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
|
|
|
|
// http://www.lightlink.com/tjweber/StripWav/WAVE.html
|
|
|
|
// http://www.lightlink.com/tjweber/StripWav/Canon.html
|
|
|
|
// https://tools.ietf.org/html/draft-ema-vpim-wav-00
|
2022-03-06 18:01:22 +01:00
|
|
|
// Note: > The chunks MAY appear in any order except that the Format chunk
|
2020-08-23 19:49:16 +02:00
|
|
|
// > MUST be placed before the Sound data chunk (but not necessarily
|
|
|
|
// > contiguous to the Sound data chunk).
|
|
|
|
struct RIFFHeader {
|
|
|
|
riff [4]byte
|
|
|
|
file_size u32
|
|
|
|
form_type [4]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RIFFChunkHeader {
|
|
|
|
chunk_type [4]byte
|
|
|
|
chunk_size u32
|
|
|
|
chunk_data voidptr
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RIFFFormat {
|
2021-01-22 21:22:27 +01:00
|
|
|
format_tag u16 // PCM = 1; Values other than 1 indicate some form of compression.
|
|
|
|
nchannels u16 // Nc ; 1 = mono ; 2 = stereo
|
|
|
|
sample_rate u32 // F
|
|
|
|
avg_bytes_per_second u32 // F * M*Nc
|
|
|
|
nblock_align u16 // M*Nc
|
|
|
|
bits_per_sample u16 // 8 * M
|
|
|
|
cbsize u16 // Size of the extension: 22
|
|
|
|
valid_bits_per_sample u16 // at most 8*M
|
|
|
|
channel_mask u32 // Speaker position mask
|
2020-08-23 19:49:16 +02:00
|
|
|
sub_format [16]byte // GUID
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_wav_file_samples(fpath string) ?[]f32 {
|
|
|
|
mut res := []f32{}
|
|
|
|
// eprintln('> read_wav_file_samples: $fpath -------------------------------------------------')
|
2020-10-26 12:14:21 +01:00
|
|
|
mut bytes := os.read_bytes(fpath) ?
|
2022-04-15 13:58:56 +02:00
|
|
|
mut pbytes := &u8(bytes.data)
|
2020-08-23 19:49:16 +02:00
|
|
|
mut offset := u32(0)
|
2021-04-20 16:16:35 +02:00
|
|
|
rh := unsafe { &RIFFHeader(pbytes) }
|
2020-08-23 19:49:16 +02:00
|
|
|
// eprintln('rh: $rh')
|
2022-04-15 13:58:56 +02:00
|
|
|
if rh.riff != [u8(`R`), `I`, `F`, `F`]! {
|
2020-08-23 19:49:16 +02:00
|
|
|
return error('WAV should start with `RIFF`')
|
|
|
|
}
|
2022-04-15 13:58:56 +02:00
|
|
|
if rh.form_type != [u8(`W`), `A`, `V`, `E`]! {
|
2020-08-23 19:49:16 +02:00
|
|
|
return error('WAV should have `WAVE` form type')
|
|
|
|
}
|
|
|
|
if rh.file_size + 8 != bytes.len {
|
|
|
|
return error('WAV should have valid lenght')
|
|
|
|
}
|
|
|
|
offset += sizeof(RIFFHeader)
|
|
|
|
mut rf := &RIFFFormat(0)
|
|
|
|
for {
|
|
|
|
if offset >= bytes.len {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
//
|
2021-04-20 16:16:35 +02:00
|
|
|
ch := unsafe { &RIFFChunkHeader(pbytes + offset) }
|
2020-08-23 19:49:16 +02:00
|
|
|
offset += 8 + ch.chunk_size
|
|
|
|
// eprintln('ch: $ch')
|
|
|
|
// eprintln('p: $pbytes | offset: $offset | bytes.len: $bytes.len')
|
|
|
|
// ////////
|
2022-04-15 13:58:56 +02:00
|
|
|
if ch.chunk_type == [u8(`L`), `I`, `S`, `T`]! {
|
2020-08-23 19:49:16 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
//
|
2022-04-15 13:58:56 +02:00
|
|
|
if ch.chunk_type == [u8(`i`), `d`, `3`, ` `]! {
|
2020-08-23 19:49:16 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
//
|
2022-04-15 13:58:56 +02:00
|
|
|
if ch.chunk_type == [u8(`f`), `m`, `t`, ` `]! {
|
2020-08-23 19:49:16 +02:00
|
|
|
// eprintln('`fmt ` chunk')
|
2021-01-22 21:22:27 +01:00
|
|
|
rf = unsafe { &RIFFFormat(&ch.chunk_data) }
|
2020-08-23 19:49:16 +02:00
|
|
|
// eprintln('fmt riff format: $rf')
|
|
|
|
if rf.format_tag != 1 {
|
|
|
|
return error('only PCM encoded WAVs are supported')
|
|
|
|
}
|
|
|
|
if rf.nchannels < 1 || rf.nchannels > 2 {
|
|
|
|
return error('only mono or stereo WAVs are supported')
|
|
|
|
}
|
|
|
|
if rf.bits_per_sample !in [u16(8), 16] {
|
|
|
|
return error('only 8 or 16 bits per sample WAVs are supported')
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
//
|
2022-04-15 13:58:56 +02:00
|
|
|
if ch.chunk_type == [u8(`d`), `a`, `t`, `a`]! {
|
2020-08-23 19:49:16 +02:00
|
|
|
if rf == 0 {
|
|
|
|
return error('`data` chunk should be after `fmt ` chunk')
|
|
|
|
}
|
|
|
|
// eprintln('`fmt ` chunk: $rf\n`data` chunk: $ch')
|
|
|
|
mut doffset := 0
|
2022-04-15 13:58:56 +02:00
|
|
|
mut dp := unsafe { &u8(&ch.chunk_data) }
|
2020-08-23 19:49:16 +02:00
|
|
|
for doffset < ch.chunk_size {
|
|
|
|
for c := 0; c < rf.nchannels; c++ {
|
|
|
|
mut x := f32(0.0)
|
|
|
|
mut step := 0
|
2021-01-22 21:22:27 +01:00
|
|
|
ppos := unsafe { dp + doffset }
|
2020-08-23 19:49:16 +02:00
|
|
|
if rf.bits_per_sample == 8 {
|
2022-04-15 13:58:56 +02:00
|
|
|
d8 := unsafe { &u8(ppos) }
|
2020-08-23 19:49:16 +02:00
|
|
|
x = (f32(*d8) - 128) / 128.0
|
|
|
|
step = 1
|
|
|
|
doffset++
|
|
|
|
}
|
|
|
|
if rf.bits_per_sample == 16 {
|
2021-04-20 16:16:35 +02:00
|
|
|
d16 := unsafe { &i16(ppos) }
|
2020-08-23 19:49:16 +02:00
|
|
|
x = f32(*d16) / 32768.0
|
|
|
|
step = 2
|
|
|
|
}
|
|
|
|
doffset += step
|
|
|
|
if doffset < ch.chunk_size {
|
|
|
|
res << x
|
|
|
|
if rf.nchannels == 1 {
|
|
|
|
// Duplicating single channel mono sounds,
|
|
|
|
// produces a stereo sound, simplifying further processing:
|
|
|
|
res << x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|