その5:ストリーム再生音^〜^♪

むむ、やっぱし、動的ストリーム再生やるのにはwaveフォーマット指定もやらんといけんぽ´〜`
静的だけど、指定付きでやってるのがここですな。

効果音(その3)

マイクロソフトのドラムマシンより説明わかりやすい^〜^!
あっちは初歩の初歩ないからの´〜`

その4

結局DirectSoundのNotifyメソッドを使うのがらくちんということで待機ハンドルを使ってイベント処理しました。って、昨日の(その3)の方法(id:parlin:20051209)と一緒ですなあ(..;)
動くサンプルがあるとなにかと嬉しいので書いときますかの^〜^
以下ソースコードなり。

続きを読む

その2

なんとかDirectSoundを使った再生用サウンドクラス完成かな^〜^

  1. 使うときはフォーム側で先にクラス宣言しとかないと一回目の再生だけコントロール操作が入るとおわっちゃうかも。(宣言と同時にインスタンス作成するとあうち)でも、コンストラクタと再生部を分離したからええのかもw<チェキしろよ!
  2. Microsoft.DirectX.AudioVideoPlaybackのAudioだと、再生中かどうかのチェック不可なのがあんぽんたん´〜`(常に再生フラグオン)
  3. DeviceやSecondaryBufferの自動Dispose()機能つけたのでnewしまくってもそうそうおかしくならないはず。

以下ソースコードなり。

続きを読む

その1

Framework 2.0になったらサウンド再生も簡単らしいけど、今はCLRにないのでめんどくさ〜´〜`
winmm.dll呼ぶ方法が普通みたいじゃが、ストリームでやるときめんどくさそうなのでDirectXでやるのじゃ!まず、オーソドックスな奴から。


using Microsoft.DirectX.AudioVideoPlayback;

string soundFileName = "SUCCESS.WAV";
Audio audio = new Audio(soundFileName);
audio.Play();

ファイル名で与えられる時はらくちんまん。でも、ストリーム渡す時はこれだとダメなのら´〜`
まずはファイル名指定でのDirectSoundバージョン。

using Microsoft.DirectX.DirectSound;

private Device device = null;
private SecondaryBuffer sound = null;
string soundFileName = "SUCCESS.WAV";

BufferDescription desc = new BufferDescription();
desc.ControlPan = true;
desc.GlobalFocus = true;

device = new Device();
device.SetCooperativeLevel(this, CooperativeLevel.Normal);
sound = new SecondaryBuffer(soundFileName, desc, device);
sound.Play(0, BufferPlayFlags.Default);

さて、本題。アセンブリに埋め込んだファイルを再生するのだ。

using System.Reflection;
using Microsoft.DirectX.DirectSound;

// 現在実行中のアセンブリを取得
Assembly thisExe = Assembly.GetExecutingAssembly();
string assemblyName = thisExe.GetName().Name;
string soundFileName = "SUCCESS.WAV";
//埋め込みファイルのストリームを取得
Stream stream = thisExe.GetManifestResourceStream(assemblyName + "." + soundFileName);

BufferDescription desc = new BufferDescription();
desc.ControlPan = true;
desc.GlobalFocus = true;

device = new Device();
device.SetCooperativeLevel(this, CooperativeLevel.Normal);
sound = new SecondaryBuffer(stream, desc, device);
//ストリームを閉じる!
stream.Close();
sound.Play(0, BufferPlayFlags.Default);

半日がかりでなんとかいけた!

アセンブリ情報まわり

アセンブリ情報を使うとファイル埋め込みとか色々でけて便利!


//現在実行中のアセンブリを取得
Assembly thisExe = Assembly.GetExecutingAssembly();
こうやって取得したあと、

foreach (string resourceName in thisExe.GetManifestResourceNames())
{
Console.WriteLine(resourceName);
}
これでリソース名げった〜。何故かハンドルが欲しい時は、

//ハンドル取得
Module[] ms = thisExe.GetModules();
IntPtr hInstance = Marshal.GetHINSTANCE(ms[0]);
Console.WriteLine(hInstance.ToString());
こんなのでいけますの。