I was thinking along similar lines, but I thought I could use a timer interrupt to stop SOUND from playing at regular intervals, load the next 4096 samples, and so on. I get something, but nothing like the originals, and not as sweet sounding @Gordons_Treehouse77 s version.
Setting sample to 1 does a complex sine sound wave at 440 Hz, which I just used to see how PLAY LOAD SOUND works, sample 2 and 3 are “algorithmic”. I plot the first 4096 samples.
It’s not quite right but a work in progress, perhaps someone can spot the issues.
Option base 0
Option explicit
Const BITS12 = (1<<12)
Const BITS8 = (1<<8)
Const SAMPLE_LENGTH=4096
Const k=50 'scale graph
Dim wdata%(SAMPLE_LENGTH-1)
Dim pwdata%(SAMPLE_LENGTH\4-1)
Dim i%, t%, amp, x, freq, vol
Dim x0,x1,y0,y1
'modify for different samples
' 1 = complex sine example
' 2 and 3 algorithmic waveforms
Dim integer sample=3
CLS
If sample=1 Then
load_simple_waveform
Else
load_waveform
End If
plot_waveform
'Do : Loop While Inkey$=""
vol=25
SetTick 1/freq*1000,play_sample
Do
'do nothing
Loop
End
Sub play_sample
Play stop
Play load sound pwdata%()
Play sound 1,B,U,freq,vol
If sample=1 Then
load_simple_waveform
Else
load_waveform
End If
End Sub
Sub load_waveform
Local integer z,i
Local x,y
freq = 2
For i%=0 To SAMPLE_LENGTH-1
If sample=2 Then
z = t%*((t%>>12 Or t%>>8) And 63 And t%>>4)
ElseIf sample=3 Then
z = ((t%>>6) Or t% Or (t%>>16))*10 + ((t%>>11) And 7)
End If
wdata%(i%) = z And 255 'fit to 8BITS
Inc t%
Next i%
' shape the data to 12 bit values
Math window wdata%(),0,BITS12,wdata%()
' pack the data down
Memory pack wdata%(),pwdata%(),4096,16
End Sub
Sub load_simple_waveform
Local integer x
freq = 440
amp=100
For i%=0 To SAMPLE_LENGTH-1
x = amp*Sin(2*Pi/SAMPLE_LENGTH*i%)
x = x + amp*Sin(2*Pi*2/SAMPLE_LENGTH*i%)
x = x + amp*Sin(2*Pi*4/SAMPLE_LENGTH*i%)
wdata%(i%) = x
Next i%
' shape the data to 12 bit values
Math window wdata%(),0,BITS12,wdata%()
' pack the data down
Memory pack wdata%(),pwdata%(),4096,16
End Sub
Sub plot_waveform
Local x0,x1,y0,y1,i%
x0=0
y0=wdata%(0)/k+160
Line 0,160,320,160,1,RGB(white)
Line 0,160+BITS12/k,320,160+BITS12/k,1,RGB(white)
For i%=1 To SAMPLE_LENGTH-1
x1 = i%*320/SAMPLE_LENGTH
y1 = wdata%(i%)/k+160
Line x0,y0,x1,y1
x0=x1
y0=y1
Next i%
End Sub