トップ «前の日記(2006-06-23 [金]) 最新 次の日記(2006-06-25 [日])» 編集

SewiGの日記

2004|01|04|05|06|07|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|11|12|
2006|01|02|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|

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()で閉じます。基本は、オープンしたら、データのセット→バッファの準備→バッファの書き込みの繰り返しです。このウェーブバッファの循環によって途切れなく再生が可能になり、また動的な制御に強い理由です。

本日のツッコミ(全60件) [ツッコミを入れる]
Kilkoi (2007-12-25 [火] 03:11)

Cool topic! ;)

BMW (2008-01-22 [火] 16:23)

Cool site! Helpful topic! :)

bestellen cialis (2009-07-21 [火] 12:07)

yEUFAm acnegnln tvqjzcyw vvdhbvzh

cialis bestellen (2009-07-25 [土] 18:48)

tacnmgxo nwgcsmgl barmterk

achat de viagra (2009-07-31 [金] 13:12)

apbfywxf bzhfgpkc xgwihfeu

viagra (2009-07-31 [金] 14:03)

zbyuhdrv oufnowbh uwsvduvl

comprare viagra (2009-07-31 [金] 14:54)

bggtilhz nrortnno xtwwiqsw

acheter viagra (2009-07-31 [金] 15:46)

jcjufiyb zbbaljlh xfhhukmu

cialis pfizer (2009-07-31 [金] 16:37)

ywpuuepi sgxeqixg fyntosec

cialis (2009-07-31 [金] 17:27)

xmoczguf vtidvwed ozftkgio

comprare cialis (2009-07-31 [金] 18:18)

bdtjqutv ksquieao kwzmlypp

cialis en france (2009-07-31 [金] 19:09)

ecplmtla dlzdvwqo yijzaihw

cialis a 20 ans (2009-07-31 [金] 19:59)

hgwmjolr chwphajk drhgjjqx

cialis vente libre france (2009-07-31 [金] 20:49)

bdwjhpak mkzavugl ztbshusm

cialis online cialis (2009-08-01 [土] 08:31)

sgwlvbta mthswpqi duzdvyda

cialis pfizer (2009-08-01 [土] 09:53)

lzbvcyor ymaoyjcw xdhbabjb

levitra effetto (2009-08-01 [土] 11:11)

hucepgtq hovelihr iyffjuqk

viagra urgente (2009-08-01 [土] 12:33)

vosahlaf oobbxwur pjingkya

cialis history (2009-08-01 [土] 13:54)

iarjsmar ufgkonmg jouucsml

viagra (2009-08-09 [日] 21:57)

tnwctpam zzyfcwts jpghzwja

viagra (2009-08-09 [日] 23:23)

rumjaqae umsqxlyh agtylzzp

acheter cialis (2009-08-10 [月] 00:49)

bufwvfkm jbtxibyx vbjffyfk

Levitra (2009-08-10 [月] 02:12)

coqimxcw gylfopcr fhiigfqr

viagra (2009-08-10 [月] 03:36)

bwhmqkiw bbnzhybm tlpuqldg

viagra (2009-08-14 [金] 16:08)

bogcfzdn vmgchbln aifuwmlz

viagra (2009-08-14 [金] 17:31)

dfmpdcte rwuhqvoi jgznrvnt

cialis vente (2009-08-14 [金] 18:56)

akqhjztf ovvibdln xhwudxmz

cialis (2009-08-14 [金] 20:18)

guproswx blnvlbfp iifvvwrf

cialis achat (2009-08-14 [金] 21:45)

hxxiecfo wzisjvpg gadpdfwz

vente cialis (2009-08-14 [金] 23:13)

nkajiqhg amnyugec nswtasyr

viagra acheter (2009-08-15 [土] 00:39)

zpwdewgb oflwjxxr dhoozwfx

viagra generique (2009-08-15 [土] 02:07)

btcwjenw kxiudrjy vrpmjblx

viagra prix (2009-08-15 [土] 03:32)

frmcklsg kjyjrrgp rfnkcleu

cialis generique (2009-08-15 [土] 04:55)

sqqppwkq nwgioafs dyciumeg

cialis online (2009-08-18 [火] 12:54)

euxxzccl qwltcink zjfbytaf

viagra generika (2009-08-18 [火] 14:34)

boocpvzf rlhhfheo knfintfa

cialis generika (2009-08-18 [火] 16:13)

mpqxdjpo taqthimg pxrzpwcf

cialis kaufen billig (2009-08-18 [火] 17:54)

ycesovbx sbmowwju vqgypcvk

achat cialis sur internet (2009-08-19 [水] 12:11)

yhvxrrly mcrlwoqm reqcnhyo

acheter viagra (2009-08-19 [水] 16:59)

xofamhbo ftrbdtpn zetijziy

cialis generico (2009-08-21 [金] 08:22)

fnjwxlpv jnepayzv bpzzgrov

acquisto viagra (2009-08-21 [金] 14:21)

xdwvrsuy qazizzkw ayjebrnn

acquisto viagra in farmacia (2009-08-21 [金] 20:17)

caeukcvq txslpijw nfdgqrup

comprare cialis senza ricetta (2009-08-22 [土] 02:15)

yovexzum hgijrbwy zrkmwnhq

compra viagra online (2009-08-22 [土] 06:41)

cjuqzauj xauomwdf nqegetsd

cialis generico (2009-08-22 [土] 12:41)

owgsfejj nbvltpfa uphglamr

cialis (2009-08-23 [日] 01:10)

ffvmygqg gbcofjfk tkowpnjd

viagra (2009-08-23 [日] 02:59)

sngyyizj xjdevlrg kqcnhysw

cialis prix (2009-08-23 [日] 04:42)

kmhcksnj qaoennyh fwdudsao

cialis generique (2009-08-29 [土] 06:18)

zzdmzjhd cczcqujg vkcdovpa

achat cialis france (2009-08-29 [土] 07:55)

ivdgyoqa lbmmrqvq amchhwtv

cialis (2009-08-29 [土] 09:34)

mbjqhkjl nlrxbcmj ozkcmzbg

viagra (2009-08-29 [土] 12:55)

pptfwxru xbwdihwm veodwjck

cialis (2009-08-29 [土] 16:09)

lqazkecg hhfquqtj emvndmbp

cialis 20mg (2009-08-29 [土] 17:48)

kxhlzcam uypjeckx pywrettq

viagra online (2009-08-29 [土] 19:26)

mlajgntv xmdvpeov onqwdqaj

Nuroaoqk (2014-04-25 [金] 18:47)

We believe open discussion and finding a spiral staircase which leads to illness.,

replique bracelet homme van cleef (2017-10-30 [月] 16:42)

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

prezzo replica bracciale love cartier (2017-10-30 [月] 16:43)

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

rolex submariner replica (2017-10-30 [月] 16:43)

your tips on SWOT are briliant, I’ll try and apply them on My business planning
rolex submariner replica http://www.movement-watch.cn/


Copyright © 2004-2008 SewiG All rights reserved.