[REBOL] Re: wave progress
From: anton::wilddsl::net::au at: 11-Oct-2008 19:42
Rosemary,
I see better what you're trying to do, now.
If all you want to do is show progress while
the sound is playing, then see the code at bottom.
The code just calculates where the progress
should be, judging by how much time has elapsed
since the beginning. After inserting the sound
into the sound port, there's no buffer management
that we can do that I'm aware of.
(See code at bottom.)
Regards,
Anton.
Rosemary de Dear wrote:
> Thank you Henrik and Anton.
>
> I can't make the progress bar synchronise with the wave file as it's
> playing. I realise I need to make a loop, but don't know how to read each
> part of the wave. This is the script that gives a "correct" step for one
> interation.
>
> rebol []
>
> wave: %any.wav
>
> play: func [audio] [
> fileSize: to-integer ((size? wave) / 1024)
> sound-port: open sound://
> insert sound-port load audio
> step: (prog/size/x / (max 1 fileSize))
> wait 0.1
> prog/data: prog/data + step
> show prog
> wait sound-port
> close sound-port
> ]
>
> view layout [
> prog: progress
> btn "Play" [play wave]
> ]
>
> Regards, Rosemary
rebol [
Date: 11-Oct-2008
Author: "Anton Rolls"
Purpose: {Demonstrate updating a progress bar while playing a sound.}
Status: {Working. Tested on 2.7.6.3.1 in Wine on Linux.}
Note: "Rebol's sound port is still buggy."
]
;wavefile: %../local/audio/music/samples/loop-db-4bars.wav
;wavefile: %../local/audio/music/samples/robot1-infiltrate.wav
;wavefile: %../local/audio/music/samples/robot2-suspect.wav
wavefile: %../local/audio/music/samples/robot3-abort.wav
play: func [wavefile][
wave: load wavefile
total-time: 1000 * (length? wave/data) / (
(wave/bits / 8) * wave/channels) / wave/rate
playing?: true
insert sound-port wave
]
window: center-face layout [
button "play" [
set-face my-progress 0
play wavefile
]
button "stop" [
playing?: false
start-time: none
clear sound-port
]
my-progress: progress
; Set face/rate so that timer events
; will start flowing into this window.
box rate 1
]
view/new window
playing?: none
start-time: none
my-event-func: insert-event-func func [face event][
if event/type = 'time [
if playing? [
either none? start-time [
; This is the first event,
; so just record the time.
start-time: event/time
][
; How much time has elapsed
; since the start ?
elapsed: event/time - start-time
; Convert elapsed time
; to range [0.0, 1.0],
; good for the progress bar.
tp: min 1.0 elapsed / total-time
if tp <= 1.0 [
; Update progress bar
set-face my-progress tp
]
if tp >= 1.0 [
playing?: false
start-time: none
]
]
]
]
event
]
sound-port: open sound://
if error? set/any 'err try [
do-events
][
print mold disarm err
]
close sound-port
remove-event-func :my-event-func
()