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 gg &gg.Context = unsafe { nil } // used for drawing
} }
fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int, mut acontext AppState) { fn my_audio_stream_callback(mut soundbuffer &f32, num_frames int, num_channels int, mut acontext AppState) {
mut soundbuffer := unsafe { buffer }
for frame := 0; frame < num_frames; frame++ { for frame := 0; frame < num_frames; frame++ {
t := int(f32(acontext.frame_0 + frame) * 0.245) t := int(f32(acontext.frame_0 + frame) * 0.245)
// "Techno" by Gabriel Miceli // "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) (t * (((t / 640 | 0) ^ ((t / 640 | 0) - 2)) % 13) / 2 & 127)
for ch := 0; ch < num_channels; ch++ { for ch := 0; ch < num_channels; ch++ {
idx := frame * num_channels + ch idx := frame * num_channels + ch
unsafe { a := f32(u8(y) - 127) / 255.0
a := f32(u8(y) - 127) / 255.0 soundbuffer[idx] = a
soundbuffer[idx] = a acontext.frames[idx & 2047] = a
acontext.frames[idx & 2047] = a
}
} }
} }
acontext.frame_0 += num_frames 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)) 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 ms := sw.elapsed().milliseconds() - sw_start_ms
unsafe { for frame := 0; frame < num_frames; frame++ {
mut soundbuffer := buffer for ch := 0; ch < num_channels; ch++ {
for frame := 0; frame < num_frames; frame++ { idx := frame * num_channels + ch
for ch := 0; ch < num_channels; ch++ { if ms < 250 {
idx := frame * num_channels + ch soundbuffer[idx] = 0.5 * sintone(20, frame, num_frames)
if ms < 250 { } else if ms < 300 {
soundbuffer[idx] = 0.5 * sintone(20, frame, num_frames) soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames)
} else if ms < 300 { } else if ms < 1500 {
soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames) soundbuffer[idx] *= sintone(22, frame, num_frames)
} else if ms < 1500 { } else {
soundbuffer[idx] *= sintone(22, frame, num_frames) soundbuffer[idx] = 0.5 * sintone(25, 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 { if p.finished {
return return
} }