HSPで正弦波の音を出すサンプル
2014/11/08 追記
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(sin(2*M_PI*cnt*syuuhasuu(oto)/samplingRate)*a)
wpoke waveData,cnt*2,data
if (cnt\samplingRate)=(samplingRate-1) : oto+
loop// 再生準備
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