Waveデータを作って再生する

あい今度はオンメモリーのデータを作ってすぐ再生。
今度は矩形波にしてみたよスクウェアウェイブ。
(i>>7&1)*255ここね。128サンプルごとに0と255を繰り返すデータになります。
関係ないけど矩形波倶楽部のCDにT-SQUAREのメンバーが参加してるのは
やっぱり矩形とSQUARE掛けてるのか。


メモとしてPythonの配列はinsertメソッドで拡張と設定ができるのね。
慣れねえ。

import clr

clr.AddReference('System')
clr.AddReference('System.Windows.Forms')
clr.AddReference('Microsoft.DirectX.DirectSound')

from System import *
from System.Windows.Forms import *
from Microsoft.DirectX.DirectSound import *

class PlaySound2(Form):
    def format(self):
        fmt = WaveFormat()
        fmt.SamplesPerSecond = 44100
        fmt.BitsPerSample = Int16(8)
        fmt.Channels = Int16(1)

        fmt.FormatTag = Int16(WaveFormatTag.Pcm)
        fmt.BlockAlign = Int16(fmt.Channels * fmt.BitsPerSample / 8)
        fmt.AverageBytesPerSecond = fmt.BlockAlign * fmt.SamplesPerSecond
        return fmt

    def description(self, length):
        desc = BufferDescription(self.format())
        desc.BufferBytes = length
        desc.ControlPositionNotify = True
        desc.GlobalFocus = True
        return desc

    def __init__(self):
        wave = []
        for i in range(44100 * 2):
            wave.insert(i, (i>>7&1)*255)

        self.device = Device()
        self.device.SetCooperativeLevel(self, CooperativeLevel.Normal)

        self.buffer = SecondaryBuffer(self.description(len(wave)), self.device)
        self.buffer.Write(0, Array[Byte](wave), LockFlag.EntireBuffer)
        self.buffer.Play(0, BufferPlayFlags.Default)

    def Dispose(self, disposing):
        if(disposing):
            if(self.buffer):
                self.buffer.Stop()
                self.buffer.Dispose()

            if(self.device):
                self.device.Dispose()

        Form.Dispose(self, disposing)

Application.Run(PlaySound2())