2020-08-23 20:49:16 +03: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 ...')
|
2022-05-13 06:56:21 +03:00
|
|
|
play_sounds([os.resource_abs_path('uhoh.wav')])?
|
2020-08-23 20:49:16 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
2022-05-13 06:56:21 +03:00
|
|
|
play_sounds(os.args[1..])?
|
2020-08-23 20:49:16 +03: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
|
|
|
|
}
|
2022-05-13 06:56:21 +03:00
|
|
|
player.play_wav_file(f)?
|
2020-08-23 20:49:16 +03:00
|
|
|
}
|
|
|
|
player.stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2020-10-26 14:14:21 +03:00
|
|
|
fn audio_player_callback(buffer &f32, num_frames int, num_channels int, mut p Player) {
|
2020-08-23 20:49:16 +03: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 22:23:06 +03:00
|
|
|
unsafe { vmemcpy(buffer, &p.samples[p.pos], nsamples * int(sizeof(f32))) }
|
2020-08-23 20:49:16 +03:00
|
|
|
p.pos += nsamples
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Player) init() {
|
2021-01-22 23:22:27 +03:00
|
|
|
audio.setup(
|
2020-08-23 20:49:16 +03:00
|
|
|
num_channels: 2
|
|
|
|
stream_userdata_cb: audio_player_callback
|
|
|
|
user_data: p
|
2021-01-22 23:22:27 +03:00
|
|
|
)
|
2020-08-23 20:49:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Player) stop() {
|
|
|
|
audio.shutdown()
|
|
|
|
p.free()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Player) play_wav_file(fpath string) ? {
|
|
|
|
println('> play_wav_file: $fpath')
|
2022-05-13 06:56:21 +03:00
|
|
|
samples := read_wav_file_samples(fpath)?
|
2020-08-23 20:49:16 +03:00
|
|
|
p.finished = true
|
|
|
|
p.samples << samples
|
|
|
|
p.finished = false
|
|
|
|
for !p.finished {
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(16 * time.millisecond)
|
2020-08-23 20:49:16 +03: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 20:01:22 +03:00
|
|
|
// Note: > The chunks MAY appear in any order except that the Format chunk
|
2020-08-23 20:49:16 +03:00
|
|
|
// > MUST be placed before the Sound data chunk (but not necessarily
|
|
|
|
// > contiguous to the Sound data chunk).
|
|
|
|
struct RIFFHeader {
|
2022-04-15 15:55:39 +03:00
|
|
|
riff [4]u8
|
2020-08-23 20:49:16 +03:00
|
|
|
file_size u32
|
2022-04-15 15:55:39 +03:00
|
|
|
form_type [4]u8
|
2020-08-23 20:49:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct RIFFChunkHeader {
|
2022-04-15 15:55:39 +03:00
|
|
|
chunk_type [4]u8
|
2020-08-23 20:49:16 +03:00
|
|
|
chunk_size u32
|
|
|
|
chunk_data voidptr
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RIFFFormat {
|
2022-04-15 18:25:45 +03: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
|
2022-04-15 15:55:39 +03:00
|
|
|
sub_format [16]u8 // GUID
|
2020-08-23 20:49:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read_wav_file_samples(fpath string) ?[]f32 {
|
|
|
|
mut res := []f32{}
|
|
|
|
// eprintln('> read_wav_file_samples: $fpath -------------------------------------------------')
|
2022-05-13 06:56:21 +03:00
|
|
|
mut bytes := os.read_bytes(fpath)?
|
2022-04-15 14:58:56 +03:00
|
|
|
mut pbytes := &u8(bytes.data)
|
2020-08-23 20:49:16 +03:00
|
|
|
mut offset := u32(0)
|
2021-04-20 17:16:35 +03:00
|
|
|
rh := unsafe { &RIFFHeader(pbytes) }
|
2020-08-23 20:49:16 +03:00
|
|
|
// eprintln('rh: $rh')
|
2022-04-15 14:58:56 +03:00
|
|
|
if rh.riff != [u8(`R`), `I`, `F`, `F`]! {
|
2020-08-23 20:49:16 +03:00
|
|
|
return error('WAV should start with `RIFF`')
|
|
|
|
}
|
2022-04-15 14:58:56 +03:00
|
|
|
if rh.form_type != [u8(`W`), `A`, `V`, `E`]! {
|
2020-08-23 20:49:16 +03: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 17:16:35 +03:00
|
|
|
ch := unsafe { &RIFFChunkHeader(pbytes + offset) }
|
2020-08-23 20:49:16 +03:00
|
|
|
offset += 8 + ch.chunk_size
|
|
|
|
// eprintln('ch: $ch')
|
|
|
|
// eprintln('p: $pbytes | offset: $offset | bytes.len: $bytes.len')
|
|
|
|
// ////////
|
2022-04-15 14:58:56 +03:00
|
|
|
if ch.chunk_type == [u8(`L`), `I`, `S`, `T`]! {
|
2020-08-23 20:49:16 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
//
|
2022-04-15 14:58:56 +03:00
|
|
|
if ch.chunk_type == [u8(`i`), `d`, `3`, ` `]! {
|
2020-08-23 20:49:16 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
//
|
2022-04-15 14:58:56 +03:00
|
|
|
if ch.chunk_type == [u8(`f`), `m`, `t`, ` `]! {
|
2020-08-23 20:49:16 +03:00
|
|
|
// eprintln('`fmt ` chunk')
|
2021-01-22 23:22:27 +03:00
|
|
|
rf = unsafe { &RIFFFormat(&ch.chunk_data) }
|
2020-08-23 20:49:16 +03: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 14:58:56 +03:00
|
|
|
if ch.chunk_type == [u8(`d`), `a`, `t`, `a`]! {
|
2022-06-27 19:48:46 +03:00
|
|
|
if unsafe { rf == 0 } {
|
2020-08-23 20:49:16 +03:00
|
|
|
return error('`data` chunk should be after `fmt ` chunk')
|
|
|
|
}
|
|
|
|
// eprintln('`fmt ` chunk: $rf\n`data` chunk: $ch')
|
|
|
|
mut doffset := 0
|
2022-04-15 14:58:56 +03:00
|
|
|
mut dp := unsafe { &u8(&ch.chunk_data) }
|
2020-08-23 20:49:16 +03:00
|
|
|
for doffset < ch.chunk_size {
|
|
|
|
for c := 0; c < rf.nchannels; c++ {
|
|
|
|
mut x := f32(0.0)
|
|
|
|
mut step := 0
|
2021-01-22 23:22:27 +03:00
|
|
|
ppos := unsafe { dp + doffset }
|
2020-08-23 20:49:16 +03:00
|
|
|
if rf.bits_per_sample == 8 {
|
2022-04-15 14:58:56 +03:00
|
|
|
d8 := unsafe { &u8(ppos) }
|
2020-08-23 20:49:16 +03:00
|
|
|
x = (f32(*d8) - 128) / 128.0
|
|
|
|
step = 1
|
|
|
|
doffset++
|
|
|
|
}
|
|
|
|
if rf.bits_per_sample == 16 {
|
2021-04-20 17:16:35 +03:00
|
|
|
d16 := unsafe { &i16(ppos) }
|
2020-08-23 20:49:16 +03: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
|
|
|
|
}
|