Other than using SetSoundFrequency() which you already know about, I don't know how to change the frequency of a created sound.einander wrote:Is there a way to change the sound frequency of a created sound?
Edit: made better code for experimentation.eddy wrote:I want to load custom wave data.
For example, you can design wave data in a canvas gadget right after you test your sound.
I think this will help you.
Code: Select all
; for eddy
;
; Don't change the default sound parameters with SetSoundParameters() or this code will break.
; defaults are sample rate = 44100, resolution = 2 bytes (16 bit), channels = 1 (mono)
; place your experimental waveform formulas in the FillBuffer() procedure at the location indicated.
IncludeFile "SoundEasy.pbi"
flags.i = #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget
If Not OpenWindow(0, 0, 0, 800, 250, "plot audio data", flags) : End : EndIf
If Not InitSound() : End : EndIf
CanvasGadget(0, 0, 0, 800, 250)
Declare PlotBuffer(*Buffer)
Declare FillBuffer(frequency.f, amplitude)
*Buffer = InitAudioBuffer(1) ; create 1 second empty audio buffer
FillBuffer(220, 50) ; choose frequency and amplitude
PlotBuffer(*buffer)
CatchSound(0, *buffer)
PlaySound(0)
While WaitWindowEvent() <> #PB_Event_CloseWindow : Wend
End
Procedure FillBuffer(frequency.f, amplitude)
Static PIx2.f = #PI * 2
*buffer = InitAudioBuffer(-1)
duration.f = InitAudioBuffer(-2)
; tweak frequency so waveform loops without glitches
frequency = Round(frequency * duration,#PB_Round_Nearest)/duration
*FirstSample = *Buffer + 44
*LastSample = *FirstSample + PeekL(*Buffer + 40) - 1
ang.f = 0 ; starting phase angle (radians)
angStep.f = frequency / 44100 * PIx2 ; angle step size (radians)
; if waveform formulas produce values from -1 to +1 use this scale factor
sf.f = (Pow(2,16)/2 - 1) * (amplitude / 100)
For *n = *FirstSample To *LastSample Step 2 ; because each sample is 2 bytes (16 bits)
; ***** place your experimental waveform formulas here *****
;
value = Sin(ang) * sf
;
; **********************************************************
PokeW(*n, value)
ang + angStep
If ang > PIx2 : ang - PIx2 : EndIf
Next *n
EndProcedure
Procedure PlotBuffer(*buffer)
*FirstSample = *Buffer + 44 ; memory location of first audio sample
*LastSample = *FirstSample + PeekL(*Buffer + 40) - 1 ; location of last audio sample
StartDrawing(CanvasOutput(0))
w = OutputWidth()
h = OutputHeight()
mid = h / 2
Box(0,0,w,h,$0B5117)
scale.f = (h - 1) / (Pow(2,16) - 1)
LineXY(0, mid, w, mid, $0BF417)
lastY = mid + (scale * PeekW(*FirstSample))
For n = *FirstSample To *LastSample Step 2
sampleData.w = PeekW(n)
y = mid + (sampleData * scale)
LineXY(lastX, lastY, x, y, $FFFFFF)
x + 1
lastX = x
lastY = y
If x > w - 1 : Break : EndIf
Next n
StopDrawing()
EndProcedure