examples: cleanup obsolete unsafe{} usages in examples/sokol/sounds

This commit is contained in:
Delyan Angelov 2024-08-01 09:10:02 +03:00
parent fdd0dab1c3
commit da5dc8bb22
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 17 additions and 23 deletions

View File

@ -12,8 +12,7 @@ mut:
gg &gg.Context = unsafe { nil } // used for drawing
}
fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int, mut acontext AppState) {
mut soundbuffer := unsafe { buffer }
fn my_audio_stream_callback(mut soundbuffer &f32, num_frames int, num_channels int, mut acontext AppState) {
for frame := 0; frame < num_frames; frame++ {
t := int(f32(acontext.frame_0 + frame) * 0.245)
// "Techno" by Gabriel Miceli
@ -21,11 +20,9 @@ fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int, mut a
(t * (((t / 640 | 0) ^ ((t / 640 | 0) - 2)) % 13) / 2 & 127)
for ch := 0; ch < num_channels; ch++ {
idx := frame * num_channels + ch
unsafe {
a := f32(u8(y) - 127) / 255.0
soundbuffer[idx] = a
acontext.frames[idx & 2047] = a
}
a := f32(u8(y) - 127) / 255.0
soundbuffer[idx] = a
acontext.frames[idx & 2047] = a
}
}
acontext.frame_0 += num_frames

View File

@ -10,22 +10,19 @@ fn sintone(periods int, frame int, num_frames int) f32 {
return math.sinf(f32(periods) * (2 * math.pi) * f32(frame) / f32(num_frames))
}
fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int) {
fn my_audio_stream_callback(mut soundbuffer &f32, num_frames int, num_channels int) {
ms := sw.elapsed().milliseconds() - sw_start_ms
unsafe {
mut soundbuffer := buffer
for frame := 0; frame < num_frames; frame++ {
for ch := 0; ch < num_channels; ch++ {
idx := frame * num_channels + ch
if ms < 250 {
soundbuffer[idx] = 0.5 * sintone(20, frame, num_frames)
} else if ms < 300 {
soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames)
} else if ms < 1500 {
soundbuffer[idx] *= sintone(22, frame, num_frames)
} else {
soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames)
}
for frame := 0; frame < num_frames; frame++ {
for ch := 0; ch < num_channels; ch++ {
idx := frame * num_channels + ch
if ms < 250 {
soundbuffer[idx] = 0.5 * sintone(20, frame, num_frames)
} else if ms < 300 {
soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames)
} else if ms < 1500 {
soundbuffer[idx] *= sintone(22, frame, num_frames)
} else {
soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames)
}
}
}

View File

@ -37,7 +37,7 @@ fn play_sounds(files []string) ! {
}
//
fn audio_player_callback(buffer &f32, num_frames int, num_channels int, mut p Player) {
fn audio_player_callback(mut buffer &f32, num_frames int, num_channels int, mut p Player) {
if p.finished {
return
}