SewiGの日記
2006-06-24 [土] [長年日記]
■ [C#][Programming] C#でサウンド(4)
Win32APIのwaveOutやPlaySoundを使う
まず、PlaySound。音関連はwinmm.dllにあるので、DllImportを使います。
[DllImport("winmm.dll")] extern static int PlaySound(string str, IntPtr ip1, int ip2);
このextern〜で使いたいメソッドが外部に定義されているよ、と教えておきます。
それから、使いそうな定数もあらかじめ調べておきます。
private const int SND_SYNC = 0x00000; private const int SND_ASYNC = 0x00001; private const int SND_LOOP = 0x00008; private const int SND_FILENAME = 0x20000; private const int SND_RESOURCE = 0x40004;
まとめると、以下のような感じでしょうか。
using System; using System.Runtime.InteropServices; namespace Sample { public class SndPlay { [DllImport("winmm.dll")] extern static int PlaySound(string str, IntPtr ip1, int i2); private const int SND_SYNC = 0x00000; private const int SND_ASYNC = 0x00001; private const int SND_LOOP = 0x00008; private const int SND_FILENAME = 0x20000; private const int SND_RESOURCE = 0x40004; public void play(string filename) { PlaySound(filename, IntPtr.Zero, SND_FILENAME|SND_ASYNC); } public void stop() { PlaySound(null, IntPtr.Zero, 0); } } }
で、waveOutですが、これが面倒。面倒だけど音をミックスして出力したり、ストリーム再生できたりと便利。そこで、Interop Declarations for Windows.hを利用することで、Win32プログラミングっぽいことをC#で可能にします。しかし、ポインタを使わざるをえません。そしてそれに伴ってunsafeの指定が必要になります。とりあえず、サイン波でも鳴らしてみましょう。こちらのページは今現在日本語で書いてある数少ない参考ページです。
というか、ほとんど一緒。
using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace Sample { public class WavePlay { unsafe public void play() { // 波形生成 short *wave_data = stackalloc short[44100]; double dr = 2 * Math.PI / (44100.0 / 440.0); for (int t = 0; t < 44100; t++) { *(wave_data + t) = (short)(Math.Sin(dr * t) * 32767); } // WAVEデバイス設定 WAVEFORMATEX wf = new WAVEFORMATEX(); wf.wFormatTag = 0x0001; // PCM wf.nChannels = 1; // モノラル wf.nSamplesPerSec = 44100; // 周波数 wf.wBitsPerSample = 16; // 16bit wf.nBlockAlign = (ushort)(wf.nChannels * wf.wBitsPerSample / 8); wf.nAvgBytesPerSec = wf.nSamplesPerSec * wf.nBlockAlign; wf.cbSize = 0; HWAVEOUT__ hwo = new HWAVEOUT__(); HWAVEOUT__ *hWOut = &hwo; windows.waveOutOpen(&hWOut, 0x0000, &wf, 0, 0, 0x0000); // WAVE情報設定 wavehdr_tag wt = new wavehdr_tag(); wt.lpData = (sbyte *)wave_data; wt.dwBufferLength = sizeof(short) * 44100; wt.dwFlags = 0; //バッファの追加情報 wt.dwLoops = 1; //1回再生 wt.dwBytesRecorded = 0; //録音用 wt.dwUser = 0; //オプションのユーザ領域 wt.lpNext = null; //使用しない wt.reserved = 0; //使用しない windows.waveOutPrepareHeader(hWOut, &wt, (uint)sizeof(wavehdr_tag)); windows.waveOutWrite(hWOut, &wt, (uint)sizeof(wavehdr_tag)); MessageBox.Show("OK", "確認", MessageBoxButtons.OK); windows.waveOutClose(hWOut); } } }
何か、もうC#じゃないみたい。
簡単に動作説明すると、waveOutOpen()で再生デバイスを開きます。ウェーブの情報はWAVEHDRに書き込みます。生のウェーブデータは適当な配列を用意します。waveOutWrite()で出力します。使い終わったらwaveOutClose()で閉じます。基本は、オープンしたら、データのセット→バッファの準備→バッファの書き込みの繰り返しです。このウェーブバッファの循環によって途切れなく再生が可能になり、また動的な制御に強い理由です。
Cool topic! ;)
Cool site! Helpful topic! :)
yEUFAm acnegnln tvqjzcyw vvdhbvzh
tacnmgxo nwgcsmgl barmterk
apbfywxf bzhfgpkc xgwihfeu
zbyuhdrv oufnowbh uwsvduvl
bggtilhz nrortnno xtwwiqsw
jcjufiyb zbbaljlh xfhhukmu
ywpuuepi sgxeqixg fyntosec
xmoczguf vtidvwed ozftkgio
bdtjqutv ksquieao kwzmlypp
ecplmtla dlzdvwqo yijzaihw
hgwmjolr chwphajk drhgjjqx
bdwjhpak mkzavugl ztbshusm
sgwlvbta mthswpqi duzdvyda
lzbvcyor ymaoyjcw xdhbabjb
hucepgtq hovelihr iyffjuqk
vosahlaf oobbxwur pjingkya
iarjsmar ufgkonmg jouucsml
tnwctpam zzyfcwts jpghzwja
rumjaqae umsqxlyh agtylzzp
bufwvfkm jbtxibyx vbjffyfk
coqimxcw gylfopcr fhiigfqr
bwhmqkiw bbnzhybm tlpuqldg
bogcfzdn vmgchbln aifuwmlz
dfmpdcte rwuhqvoi jgznrvnt
akqhjztf ovvibdln xhwudxmz
guproswx blnvlbfp iifvvwrf
hxxiecfo wzisjvpg gadpdfwz
nkajiqhg amnyugec nswtasyr
zpwdewgb oflwjxxr dhoozwfx
btcwjenw kxiudrjy vrpmjblx
frmcklsg kjyjrrgp rfnkcleu
sqqppwkq nwgioafs dyciumeg
euxxzccl qwltcink zjfbytaf
boocpvzf rlhhfheo knfintfa
mpqxdjpo taqthimg pxrzpwcf
ycesovbx sbmowwju vqgypcvk
yhvxrrly mcrlwoqm reqcnhyo
xofamhbo ftrbdtpn zetijziy
fnjwxlpv jnepayzv bpzzgrov
xdwvrsuy qazizzkw ayjebrnn
caeukcvq txslpijw nfdgqrup
yovexzum hgijrbwy zrkmwnhq
cjuqzauj xauomwdf nqegetsd
owgsfejj nbvltpfa uphglamr
ffvmygqg gbcofjfk tkowpnjd
sngyyizj xjdevlrg kqcnhysw
kmhcksnj qaoennyh fwdudsao
zzdmzjhd cczcqujg vkcdovpa
ivdgyoqa lbmmrqvq amchhwtv
mbjqhkjl nlrxbcmj ozkcmzbg
pptfwxru xbwdihwm veodwjck
lqazkecg hhfquqtj emvndmbp
kxhlzcam uypjeckx pywrettq
mlajgntv xmdvpeov onqwdqaj
We believe open discussion and finding a spiral staircase which leads to illness.,
It’s an iterative method. Say you have a pose (P, O) (position, orientation) from camera frame n. Now you receive frame n+1, and use pose (P, O) and the camera’s known intrinsic parameters to project the known 3D positions of the LEDs in headset space to camera image space. You then compare those positions to the recognized LED blobs in the camera frame n+1 and calculate some error value e, usually sum of squared distances. Then you slightly tweak (P, O) and re-calculate the error value, which is now (hopefully) smaller. You keep doing that until the error drops below some threshold, stops dropping, or you run out of time. The result is pose (P’, O’) for time step n+1.
replique bracelet homme van cleef http://www.topvcabijoux.cn/the-fashion-vintage-replica-van-cleef-bracelet-alhambra-yellow-gold-onyx-5-motifs-p300.html
The district in which I work does not have many ELL students. Though many of the ideas for targeting and aiding ELL students are just good teaching practices for all students.
prezzo replica bracciale love cartier http://www.mercibassocosto.com/cartier-amore-bracciale-luce-di-lusso.html
your tips on SWOT are briliant, I’ll try and apply them on My business planning
rolex submariner replica http://www.movement-watch.cn/