Saving a wave pattern to a .WAV file
Posted: Sun Dec 31, 2017 11:14 am
Long ago (dec. 4th, 2016) i was fooling around with waveforms, FFT, and things like that. At last i have made a function to encapsulate such waveforms in a .WAV file, in order that they can be played.
In this little test program a very simple sine wave is generated. More elaborate wave patterns could be made, using a mix of other basic waveforms and ADSR techniques, but those i leave for the real sound freaks.
In this little test program a very simple sine wave is generated. More elaborate wave patterns could be made, using a mix of other basic waveforms and ADSR techniques, but those i leave for the real sound freaks.
Code: Select all
N=16000 ! dim v(N) ! pi=4*atan(1)
signal(N,v) ' generate a wavepattern
wav_file(N,v,"out.wav") ' encapsulate it in a .WAV file
play_it("out.wav") ' play it
end
' function in which a waveform is made up by the user.
' nb = the number of values in the wave array s().
' s() = the array with samples, being numerical values.
' the values may have any value. they are
' normalized to integers in the range 0-255 by the
' wav_file() function.
' in this test program, a very simple wave pattern is
' generated: a simple sine wave.
'
def signal(nb,s()) ' a very simple (sine) wavepattern
freq=440 ! sample_rate=11025
fac=2*.pi*freq/sample_rate
for i=0 to nb-1 ! s(i)=sin(fac*i) ! next i
end def
' function to encapsulate a series of samples in a .WAV file.
' the sample values are normalized to integers in the 0-255 range.
' the .WAV file is made up as mono, 11025 sample rate, and 8 bits
' per sample. A more elaborate encapsulation function would allow
' choises for these characteristics.
' nbytes = the number of samplevalues in the s() array.
' file$ = the .WAV file to wich the data are to be saved.
def wav_file(nbytes,s(),file$)
dim snd(nbytes),out(44),x(4)
bmax=-1000000 ! bmin=1000000
for i=0 to nbytes-1
bmax=max(bmax,s(i)) ! bmin=min(bmin,s(i))
next i
fac=255/(bmax-bmin)
for i=0 to nbytes-1
snd(i)=s(i)-bmin ! snd(i)=floor(fac*snd(i))
next i
for i=0 to 43 ! read out(i) ! next i
num2hex4(nbytes,x)
for i=0 to 3 ! out(i+40)=x(i) ! next i
num2hex4(nbytes+36,x)
for i=0 to 3 ! out(i+4)=x(i) ! next i
file file$ writedim out,44,0
file file$ writedim snd,nbytes,0
file file$ setpos 0
data 82,73,70,70,156,145,0,0,87,65,86
data 69,102,109,116,32,16,0,0,0,1,0
data 1,0,17,43,0,0,17,43,0,0,1
data 0,8,0,100,97,116,97,180,1,0,0
return
end def
def play_it(mus$)
music m$ load mus$
ml=music_length(m$)
music m$ play
pause ml
end def
def num2hex4(x,a())
a(0)=0 ! a(1)=0 ! a(2)=0 ! a(3)=0
f1=256 ! f2=f1*f1 ! f3=f1*f2
a(3)=floor(x/f3)
if a(3) then x-=a(3)*f3
a(2)=floor(x/f2)
if a(2) then x-=a(2)*f2
a(1)=floor(x/f1)
if a(1) then x-=a(1)*f1
a(0)=x
return
end def
{signal_util} ' code to be found in a post dd dec.4,2016
' def sine_signal(N,v(),ampl,freq,shift,vdc,mode$)
' def saw_signal(N,v(),ampl,freq,shift,vdc,mode$)
' def tri_signal(N,v(),ampl,freq,shift,vdc,mode$)
' def block_signal(N,v(),ampl,freq,shift,vdc,mode$)
' def noise(N,v(),ampl,distr$,mode$)
' def hex2dec(h$)
' def dec2hex$(num,npos)
' def h2d(h$)
' def d2h$(num)
' def play_music(mus$)
' def wav(m$)
' def wav_info(m$,xs,ys,ww,hh,R,G,B,alpha)
' def r_fft(N,x(),reX(),imX())
' def c_fft(M,x())
' def graph(txt$,N,v(),ytop,sc)
' def graph_magn(M,rx(),ix(),ytop)
' def sigstat(N,v(),st())
' def box(ytop)
' def box2(ytop)
' def cntrl(xs,ys)
' def sbar(xs,ys,ww,ss,sw)
' def b_p(b$)