mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
time: consolidate the different sleep functions into time.wait(Duration) (#8853)
This commit is contained in:
@@ -2,11 +2,13 @@ import gg
|
||||
import gx
|
||||
import sokol.audio
|
||||
|
||||
const credits = 'Based on the ByteBeat formula from: https://www.youtube.com/watch?v=V4GfkFbDojc \n "Techno" by Gabriel Miceli'
|
||||
|
||||
struct AppState {
|
||||
mut:
|
||||
gframe int // the current graphical frame
|
||||
frame_0 int // offset of the current audio frames, relative to the start of the music
|
||||
frames [2048]f32 // a copy of the last rendered audio frames
|
||||
gframe int // the current graphical frame
|
||||
frame_0 int // offset of the current audio frames, relative to the start of the music
|
||||
frames [2048]f32 // a copy of the last rendered audio frames
|
||||
gg &gg.Context // used for drawing
|
||||
}
|
||||
|
||||
@@ -14,10 +16,8 @@ fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int, mut a
|
||||
mut soundbuffer := buffer
|
||||
for frame := 0; frame < num_frames; frame++ {
|
||||
t := int(f32(acontext.frame_0 + frame) * 0.245)
|
||||
// Credits for the formula below: https://www.youtube.com/watch?v=V4GfkFbDojc
|
||||
// "Techno" by Gabriel Miceli
|
||||
y := (t * (((t / 10 | 0) ^ ((t / 10 | 0) -
|
||||
1280)) % 11) / 2 & 127) +
|
||||
y := (t * (((t / 10 | 0) ^ ((t / 10 | 0) - 1280)) % 11) / 2 & 127) +
|
||||
(t * (((t / 640 | 0) ^ ((t / 640 | 0) - 2)) % 13) / 2 & 127)
|
||||
for ch := 0; ch < num_channels; ch++ {
|
||||
idx := frame * num_channels + ch
|
||||
@@ -32,14 +32,15 @@ fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int, mut a
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println(credits)
|
||||
mut state := &AppState{
|
||||
gg: 0
|
||||
}
|
||||
audio.setup({
|
||||
audio.setup(
|
||||
stream_userdata_cb: my_audio_stream_callback
|
||||
user_data: state
|
||||
})
|
||||
state.gg = gg.new_context({
|
||||
)
|
||||
state.gg = gg.new_context(
|
||||
bg_color: gx.rgb(50, 50, 50)
|
||||
width: 1024
|
||||
height: 400
|
||||
@@ -48,7 +49,7 @@ fn main() {
|
||||
window_title: 'ByteBeat Music'
|
||||
frame_fn: graphics_frame
|
||||
user_data: state
|
||||
})
|
||||
)
|
||||
state.gg.run()
|
||||
audio.shutdown()
|
||||
}
|
||||
|
||||
@@ -19,14 +19,14 @@ fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int) {
|
||||
for frame := 0; frame < num_frames; frame++ {
|
||||
for ch := 0; ch < num_channels; ch++ {
|
||||
idx := frame * num_channels + ch
|
||||
if ms < 500 {
|
||||
soundbuffer[idx] = sintone(20, frame, num_frames)
|
||||
} else if ms < 1000 {
|
||||
soundbuffer[idx] = sintone(25, frame, num_frames)
|
||||
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] = sintone(25, frame, num_frames)
|
||||
soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,9 +34,9 @@ fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int) {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
audio.setup({
|
||||
audio.setup(
|
||||
stream_cb: my_audio_stream_callback
|
||||
})
|
||||
time.sleep_ms(2500)
|
||||
)
|
||||
time.wait(2000 * time.millisecond)
|
||||
audio.shutdown()
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ fn (mut p Player) play_wav_file(fpath string) ? {
|
||||
p.samples << samples
|
||||
p.finished = false
|
||||
for !p.finished {
|
||||
time.sleep_ms(16)
|
||||
time.wait(16 * time.millisecond)
|
||||
}
|
||||
p.free()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user