トップ «前の日記(2006-06-26 [月]) 最新 次の日記(2006-06-28 [水])» 編集

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-27 [火] [長年日記]

[C#][Programming] C#でサウンド(5)

DirectSoundを使う

C#でDirectXを利用する場合はDirectX9が前提になります。しかし、DirectXなら複数のサウンドも勝手にミックスしてくれたりしますし、もし描画にDirect3Dを使ってるならぜひあわせて使うといいでしょう。

まず、初期化。

Microsoft.DirectX.DirectSound.Deviceがデバイスのオブジェクトです。この辺はDirect3Dなんかと設計が統一されているので分かりやすいですね。それから、SecondaryBuffer。SetCooperativeLevelは強調レベルという優先度みたいな設定があるのですが、なぜか、引数にSystem.Windows.Forms.Controlを要求してきます。new Form()でも与えれば良さそうだけど、ここは真面目に親フォームを指定しておきましょう。SecondaryBufferがあるのだからPrimaryBufferも当然あるのですが、PrimaryBufferは意識する必要がありません。SecondaryBufferに書き込んだデータがしかるべきタイミングでPrimaryBufferに書き込まれこれが再生されます。

using Microsoft.DirectX.DirectSound;

Device device;
SecondaryBuffer buffer;

device = new Device();
device.SetCooperativeLevel(owner, CooperativeLevel.Normal);
BufferDescription desc = new BufferDescription();
buffer = new SecondaryBuffer(filename, desc, device);

なんか、SecondaryBufferのコンストラクタにファイル名を書き込むあたりに抽象度の高さを感じざるを得ませんが変更しないオーディオを再生させるだけならこれでもOK。もちろんメモリをオーディオの大きさだけ消費します。

再生は

buffer.Play(0, BufferPlayFlags.Default);

ループ再生なら

buffer.Play(0, BufferPlayFlags.Looping);

停止は

buffer.Stop();

ただし、停止後の再生箇所は停止位置からとなるので、もし最初から再生したければ、再生位置を変更します。

buffer.SetCurrentPosition(0);

使い終わったら解放します。解放するのはSecondaryBufferとDeviceの両方です。

まとめると以下のようになります。

using System;
using System.Windows.Forms;
using Microsoft.DirectX.DirectSound;

namespace Sample {
	public class DSPlay {
		private Device device;
		private SecondaryBuffer buffer;

		public DSPlay(Control owner, string filename) {
			device = new Device();

			device.SetCooperativeLevel(owner, CooperativeLevel.Normal);
			BufferDescription desc = new BufferDescription();
			buffer = new SecondaryBuffer(filename, desc, device);
		}

		public void close() {
			if (buffer != null) {
				buffer.Stop();
				buffer.SetCurrentPosition(0);
				buffer.Dispose();
			}
			if (device != null) {
				device.Dispose();
			}
		}

		public void play() {
			buffer.Play(0, BufferPlayFlags.Default);
			// buffer.Play(0, BufferPlayFlags.Looping);
		}

		public void stop() {
			buffer.Stop();
		}
	}
}
本日のツッコミ(全3件) [ツッコミを入れる]
replica cartier?anello (2018-04-25 [水] 06:04)

I’ve deleted all of my partial albums before adding the entire album from Apple Music. I figured the partial albums had different metadata formatting. Works 100% fine for me doing it this way and I’m happy.
replica cartier?anello http://www.clovejs.ru/it/cartier-love-ring-replica-c136/

collier cartier imitation love (2018-04-25 [水] 06:04)

In your dreams mate! Talk about what I say, not about me. That would be futile anyway, because you don’t know me. Are you up to such intellectual exertion? Surprise me!
collier cartier imitation love http://www.bestcalovebracelet.cn/fr/

anello luna pomellato falso (2018-04-25 [水] 06:05)

I would like to thanks for the efforts you have contributed in composing this blog post. I am hoping the same high-grade blog post from you in the upcoming as well. In fact your creative writing abilities has inspired me to start my own blog now. Truly the blogging is spreading its wings rapidly. Your write up is a good model of it.
anello luna pomellato falso http://www.nudoring.ru/fake-pomellato-nudo-ring-in-pink-gold-with-ruby-p75/


Copyright © 2004-2008 SewiG All rights reserved.