Author |
Topic: Multiple WAV playback (Read 1429 times) |
|
5against4
New Member
member is offline


Posts: 16
|
 |
Multiple WAV playback
« Thread started on: Oct 28th, 2013, 2:31pm » |
|
i'm a composer & am currently creating a program that plays back various soundwaves simultaneously according to a predefined but indeterminate (i.e. partly randomised) structure. i've developed the program using elements from this article - http://bb4w.wikispaces.com/Playing+multiple+WAV+files - but i'm finding that it's a little CPU hungry, particularly when run on a PC with an older (dual core) processor, and also rather glitchy (it's fine on my quad core i7 laptop however). In this particular program, there are up to eight WAVs being played simultaneously.
So i would like to know if this might be because the file isn't reading the WAVs from memory, & if so, could someone assist with the necessary code to load/read from memory (bearing in mind i'm working with playback of simultaneous WAVs). i'd basically like to lighten the CPU load as much as possible. The relevant portion of the program is listed below.
& on a related note, is there a way to use a FLAC codec instead of the built-in WAV codec?
many thanks! Simon
Code:
80 layers=8
90 DIM layer$(layers,12),reply$(layers,2),wavperc$(layers),start(layers),currentwav(layers),begun(layers)
100 url$=""
110 RESTORE 180
120 FOR loop=1 TO layers
130 READ start(loop),layer$(loop,0)
140 FOR loop2=1 TO VAL(layer$(loop,0))
150 READ filename$:layer$(loop,loop2)=url$+filename$+".wav"
160 NEXT
170 NEXT
180 DATA 0,11,h1,h2,h3,h4,h5,h6,t20,t20,t30,t30,t60
190 DATA 1500,12,h1,h2,h3,h4,h5,h6,t20,t20,t30,t30,t45,t60
200 DATA 4000,11,h1,h2,h3,h4,h5,h6,t20,t30,t30,t45,t60
210 DATA 10500,9,b1,b2,b3,b4,b5,b6,t20,t30,t30
220 DATA 12000,9,b1,b2,b3,b4,b5,b6,t20,t20,t30
230 DATA 15000,10,b1,b2,b3,b4,b5,b6,t20,t30,t30,t45
240 DATA 18000,11,n1x,n2x,n3x,n4x,n5x,n6x,n7x,t45,t45,t60,t60
250 DATA 20000,11,w1,w2,w3,w4,w5,w6,w7,w8,t45,t60,t120
260 a=0
270 FOR loop=1 TO layers:reply$(loop,1)="_":reply$(loop,2)="-":NEXT
280
290 REPEAT
300 FOR check=1 TO layers:REM This check is for whether a new sound should begin
310 IF a=0 THEN TIME=0
320 flag=FALSE:IF TIME<start(check) THEN reply$(check,1)="_":reply$(check,2)="-":GOTO 410
330 begun(check)=1
340 IF a=start(check) OR reply$(check,1)=reply$(check,2) THEN flag=TRUE
350 IF flag=TRUE THEN n=VAL(layer$(check,0)):n_wav=RND(n):currentwav(check)=n_wav
360 IF flag=TRUE THEN SYS "mciSendString", "close wav"+STR$(check), 0, 0, 0
370 IF flag=TRUE THEN SYS "mciSendString", "open """ + layer$(check,currentwav(check)) + """ alias wav"+STR$(check), 0, 0, 0
380 IF flag=TRUE THEN SYS "mciSendString", "play wav"+STR$(check), 0, 0, 0
390 REMIF flag=TRUE THEN PRINTTAB(5,check);"Playing layer ";check
400 REMpause=GET
410 NEXT
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Multiple WAV playback
« Reply #1 on: Oct 28th, 2013, 3:13pm » |
|
on Oct 28th, 2013, 2:31pm, 5against4 wrote:i'm finding that it's a little CPU hungry, particularly when run on a PC with an older (dual core) processor |
|
Unfortunately you've not listed the entire program, so it's hard to advise. I would expect that a program playing audio (even multiple channels of audio) would use up very little CPU time, since the computational requirements are small compared with, say, playing HD video.
If Task Manager is reporting a significant amount of CPU time being taken up by your program then you can use the Trace option (Utilities menu) to get a rough idea of where the time is being spent, and the Profiler Utility (same menu) if you want to get more quantitative results.
I would expect nearly all the time, perhaps 99%, to be spent in a WAIT statement (or equivalent, e.g. INKEY function or Sleep API).
Quote:on a related note, is there a way to use a FLAC codec instead of the built-in WAV codec? |
|
FMOD supports FLAC. Many BBC BASIC programs (particularly games) have used FMOD for their audio output.
Richard.
|
|
Logged
|
|
|
|
5against4
New Member
member is offline


Posts: 16
|
 |
Re: Multiple WAV playback
« Reply #2 on: Oct 28th, 2013, 3:38pm » |
|
Many thanks for the quick reply Richard. Sorry for not including the whole program - see below.
Using the Trace function it's clearly the monitoring of how far through the WAVs that's taking up most of the processing. Do you think this would be reduced at all by reading the WAVs from memory instead of from disk?
Thanks for the tip off about FMOD - i'll investigate right away.
Code:
10 REM http://bb4w.wikispaces.com/Playing+multiple+WAV+files
20 ON ERROR ON:COLOUR9:OSCLI "FONT "+"Consolas"+","+"14":PRINTTAB(0,5):REPORT:PRINT" at line ";ERL:END
30 MODE 30:OFF
40 SYS "ShowWindow", @hwnd%, 3 : REM. SW_MAXIMIZE
50 COLOUR15:OSCLI "FONT Myriad Pro,20"
60 PRINT"Display on?";:REPEAT:disp$=GET$:UNTIL disp$="Y" OR disp$="y" OR disp$="N" OR disp$="n":CLS
70 display=TRUE:IF disp$="N" OR disp$="n" THEN display=FALSE
80 layers=8
90 DIM layer$(layers,12),reply$(layers,2),wavperc$(layers),start(layers),currentwav(layers),begun(layers)
100 url$=""
110 RESTORE 180
120 FOR loop=1 TO layers
130 READ start(loop),layer$(loop,0)
140 FOR loop2=1 TO VAL(layer$(loop,0))
150 READ filename$:layer$(loop,loop2)=url$+filename$+".wav"
160 NEXT
170 NEXT
180 DATA 0,11,h1,h2,h3,h4,h5,h6,t20,t20,t30,t30,t60
190 DATA 1500,12,h1,h2,h3,h4,h5,h6,t20,t20,t30,t30,t45,t60
200 DATA 4000,11,h1,h2,h3,h4,h5,h6,t20,t30,t30,t45,t60
210 DATA 10500,9,b1,b2,b3,b4,b5,b6,t20,t30,t30
220 DATA 12000,9,b1,b2,b3,b4,b5,b6,t20,t20,t30
230 DATA 15000,10,b1,b2,b3,b4,b5,b6,t20,t30,t30,t45
240 DATA 18000,11,n1x,n2x,n3x,n4x,n5x,n6x,n7x,t45,t45,t60,t60
250 DATA 20000,11,w1,w2,w3,w4,w5,w6,w7,w8,t45,t60,t120
260 a=0
270 FOR loop=1 TO layers:reply$(loop,1)="_":reply$(loop,2)="-":NEXT
280
290 REPEAT
300 FOR check=1 TO layers:REM This check is for whether a new sound should begin
310 IF a=0 THEN TIME=0
320 flag=FALSE:IF TIME<start(check) THEN reply$(check,1)="_":reply$(check,2)="-":GOTO 410
330 begun(check)=1
340 IF a=start(check) OR reply$(check,1)=reply$(check,2) THEN flag=TRUE
350 IF flag=TRUE THEN n=VAL(layer$(check,0)):n_wav=RND(n):currentwav(check)=n_wav
360 IF flag=TRUE THEN SYS "mciSendString", "close wav"+STR$(check), 0, 0, 0
370 IF flag=TRUE THEN SYS "mciSendString", "open """ + layer$(check,currentwav(check)) + """ alias wav"+STR$(check), 0, 0, 0
380 IF flag=TRUE THEN SYS "mciSendString", "play wav"+STR$(check), 0, 0, 0
390 REMIF flag=TRUE THEN PRINTTAB(5,check);"Playing layer ";check
400 REMpause=GET
410 NEXT
420
430 a=a+1
440 FOR calc=1 TO layers:REM This calculates how far each layer is through each sound & displays the results (omitted if display not required)
450 reply$(calc,1) = STRING$(16, CHR$(0))
460 reply$(calc,2) = STRING$(16, CHR$(0))
470 SYS "mciSendString", "status wav"+STR$(calc)+" length", reply$(calc,1), LEN(reply$(calc,1)), 0
480 SYS "mciSendString", "status wav"+STR$(calc)+" position", reply$(calc,2), LEN(reply$(calc,2)), 0
490 IF display=FALSE THEN 570
500 IF begun(calc)=1 THEN wavperc=INT((VAL(reply$(calc,2))/VAL(reply$(calc,1)))*100) ELSE wavperc=0
510 wavperc$(calc)=LEFT$(STR$(wavperc),4)
520 COLOUR15:PRINTTAB(3,1);INT((TIME/100)/60);"’";INT(TIME/100) MOD 60;"”";SPC(10);TAB(10,1);TIME
530 PRINTTAB((calc-1)*15+3,4);calc;" (";start(calc);")"
540 IF begun(calc)=1 THEN PRINTTAB((calc-1)*15+3,5);LEFT$(layer$(calc,currentwav(calc)),LEN(layer$(calc,currentwav(calc)))-4);SPC(10)
550 VDU19,calc,16,INT(VAL(wavperc$(calc))*2.55),INT(((VAL(wavperc$(calc)))-100)*-2.55),0:COLOURcalc:PRINTTAB((calc-1)*15+3,6);wavperc$(calc);"%";SPC(10)
560 REMPRINTTAB((calc-1)*15+2,4);calc;" (";start(calc);")";TAB((calc-1)*15+2,5);reply$(calc,1);" : ";reply$(calc,2);SPC(10)
570 NEXT
580 REMPRINT"";a;"|";SPC(5);reply$(1,1);" : ";reply$(1,2);SPC(10);reply$(2,1);" : ";reply$(2,2);SPC(10);reply$(3,1);" : ";reply$(3,2)
590
600
610 UNTIL FALSE
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Multiple WAV playback
« Reply #3 on: Oct 28th, 2013, 4:01pm » |
|
on Oct 28th, 2013, 3:38pm, 5against4 wrote:Many thanks for the quick reply Richard. Sorry for not including the whole program - see below. |
|
At a quick glance I can't see a WAIT (or other delay statement) anywhere in your program. If that's right it would imply that you have inadvertently coded an 'infinite loop' - i.e. a program which needlessly uses up 100% CPU time chasing around a loop. See this recent thread which discusses the issue:
http://bb4w.conforums.com/index.cgi?action=display&board=communication&num=1382121935&start=0#1382128956
If that's what is happening it could easily explain the symptoms you described.
We have moved on from the days of the BBC Micro, when it was reasonable for a program to keep the CPU fully occupied, despite not doing anything useful. Now, multi-tasking is the norm and every program needs to be written with due consideration for other programs and processes that share the CPU, memory etc.
Richard.
|
|
Logged
|
|
|
|
5against4
New Member
member is offline


Posts: 16
|
 |
Re: Multiple WAV playback
« Reply #4 on: Nov 5th, 2013, 10:48am » |
|
Thanks for this Richard. i confess i hadn't realised about the WAIT command, & it certainly makes a huge difference in terms of CPU drain.
Regarding the FLAC codec you mentioned, FMOD, can that be called/used from within a BASIC program? Looking at their website hasn't quite left me much the wiser...
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Multiple WAV playback
« Reply #5 on: Nov 5th, 2013, 11:28am » |
|
on Nov 5th, 2013, 10:48am, 5against4 wrote:Regarding the FLAC codec you mentioned, FMOD, can that be called/used from within a BASIC program? |
|
Oh yes, absolutely. I've not used it myself, but I know several people have, particularly in games.
I would have hoped that at least one of said people would have written a Wiki article describing how to do it, but having just looked there's nothing. I'm actually rather saddened by that.
Richard.
|
|
Logged
|
|
|
|
|