HSPで短形波の音を出すサンプル
HSPで短形波の音を出すサンプル
// 色々準備
#include "winmm.as"
#define CALLBACK_FUNCTION 0x30000
#define CALLBACK_NULL 0
#define CALLBACK_WINDOW 0x10000
#define WHDR_BEGINLOOP 4
#define WHDR_ENDLOOP 8onexit *exit
#define samplingRate 11025
// 波形データ作成
// -32768~32767
waveDataSize=(samplingRate*2)*8
syuuhasuu=262,294,330,349,392,440,494,523
a=8000
sdim waveData,waveDataSize
repeat waveDataSize/2
data=int((((cnt*syuuhasuu(oto)*2/samplingRate)\2)*2-1)*a) // data=int((((cnt/(samplingRate/syuuhasuu(oto)/2))\2)*2-1)*a+32768)
wpoke waveData,cnt*2,data
if (cnt\samplingRate)=(samplingRate-1) : oto+
loopsdim head
poke head,0,"RIFF"
lpoke head,4,waveDataSize+44-8
poke head,8,"WAVE"
poke head,12,"fmt "
lpoke head,16,16
wpoke head,20,1
wpoke head,22,1
lpoke head,24,samplingRate
lpoke head,28,samplingRate*2
wpoke head,32,2
wpoke head,34,16
poke head,36,"data"
lpoke head,40,waveDataSizebsave "test.wav",head,44
bsave "test.wav",waveData,waveDataSize,44// 再生準備
sdim WAVEFORMATEX,18
wpoke WAVEFORMATEX,0,1
wpoke WAVEFORMATEX,2,1
lpoke WAVEFORMATEX,4,samplingRate
lpoke WAVEFORMATEX,8,samplingRate*2
wpoke WAVEFORMATEX,12,2
wpoke WAVEFORMATEX,14,16sdim WAVEHDR,32
lpoke WAVEHDR,0,varptr(wavedata)
lpoke WAVEHDR,4,wavedatasize
lpoke WAVEHDR,16,WHDR_BEGINLOOP | WHDR_ENDLOOP
lpoke WAVEHDR,20,1sdim MMTIME,16
lpoke MMTIME,0,1waveOutOpen varptr(phwo),-1,varptr(WAVEFORMATEX),0,0,CALLBACK_NULL
waveOutPrepareHeader phwo,varptr(WAVEHDR),32
waveOutWrite phwo,varptr(WAVEHDR),32
*lp
redraw 0
waveOutGetPosition phwo,varptr(MMTIME),16
color 255,255,255 : boxf
color
pos 0,0 : mes ""+(lpeek(MMTIME,4)/(samplingRate*2))+"."+((lpeek(MMTIME,4)\(samplingRate*2))*1000/(samplingRate*2))
redraw
await 50
goto *lp
*exit
waveOutReset phwo
waveOutUnprepareHeader phwo,varptr(WAVEHDR),32
waveOutClose phwo
end