public WaveDevices(Waverly _signals) { signals = _signals; //input devices int deviceID; uint incount = waveInGetNumDevs(); inputDeviceNames = new List <String>(); inputDeviceNames.Add("no input"); WAVEINCAPS inCaps = new WAVEINCAPS(); for (deviceID = 0; deviceID < incount; deviceID++) { MMRESULT result = waveInGetDevCaps(deviceID, ref inCaps, Marshal.SizeOf(inCaps)); String inName = inCaps.szPname; inputDeviceNames.Add(inName); } //output devices uint outcount = waveOutGetNumDevs(); outputDeviceNames = new List <String>(); outputDeviceNames.Add("no output"); WAVEOUTCAPS outCaps = new WAVEOUTCAPS(); for (deviceID = 0; deviceID < outcount; deviceID++) { MMRESULT result = waveOutGetDevCaps(deviceID, ref outCaps, Marshal.SizeOf(outCaps)); String outName = outCaps.szPname; outputDeviceNames.Add(outName); } }
static private string MakeMessage(MMRESULT mmResult) { var text = new System.Text.StringBuilder(256); NativeMethods.waveOutGetErrorText(mmResult, text, (uint)text.Capacity); return($"{text}({mmResult})"); }
private static string GetMessage(MMRESULT code) { switch(code) { case MMRESULT.MMSYSERR_INVALPARAM: return "An invalid parameter was passed to the function."; case MMRESULT.MMSYSERR_INVALHANDLE: return "The associated handle is invalid. Did you dispose it?"; case MMRESULT.MMSYSERR_ERROR: return "The function returned a generic error. Please check that you are using the functions correctly."; case MMRESULT.MMSYSERR_NOERROR: return "The function did not throw an error. If you are seeing this, Berkin was obviously high while coding."; case MMRESULT.MMSYSERR_NOMEM: return "There was insufficnent memory available to allocate the requested resources."; case MMRESULT.MMSYSERR_ALLOCATED: return "The requested resources are already in use somewhere else."; case MMRESULT.WAVERR_BADFORMAT: return "Wave output device does not support request format."; case MMRESULT.MMSYSERR_BADDEVICEID: return "Device ID out of range."; case MMRESULT.MMSYSERR_NODRIVER: return "No wave output device present."; default: return code.ToString(); } }
public static void CheckThrow(MMRESULT result) { if (result != MMRESULT.NOERROR) { throw new MmException(result); } }
private static string GetMessage(MMRESULT code) { switch (code) { case MMRESULT.MMSYSERR_INVALPARAM: return("An invalid parameter was passed to the function."); case MMRESULT.MMSYSERR_INVALHANDLE: return("The associated handle is invalid. Did you dispose it?"); case MMRESULT.MMSYSERR_ERROR: return("The function returned a generic error. Please check that you are using the functions correctly."); case MMRESULT.MMSYSERR_NOERROR: return("The function did not throw an error. If you are seeing this, Berkin was obviously high while coding."); case MMRESULT.MMSYSERR_NOMEM: return("There was insufficnent memory available to allocate the requested resources."); case MMRESULT.MMSYSERR_ALLOCATED: return("The requested resources are already in use somewhere else."); case MMRESULT.WAVERR_BADFORMAT: return("Wave output device does not support request format."); case MMRESULT.MMSYSERR_BADDEVICEID: return("Device ID out of range."); case MMRESULT.MMSYSERR_NODRIVER: return("No wave output device present."); default: return(code.ToString()); } }
private static void CheckError(MMRESULT error) { if (error != MMRESULT.MMSYSERR_NOERROR) { MessageBox.Show("An error occurred while initializing MIDI: " + error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private static void Check(MMRESULT code) { if (code != MMRESULT.MMSYSERR_NOERROR) { throw new FonixTalkException(code); } }
private static void MMErrCheck(MMRESULT mmresult) { if (mmresult != MMRESULT.MMSYSERR_NOERROR) { throw new MMException(mmresult); } }
/// <summary> /// Throws an exception when return code is not MMSYSERR_NOERROR /// </summary> /// <param name="err"></param> public static void Try(MMRESULT err) { if (err != MMRESULT.MMSYSERR_NOERROR) { throw new Exception(err.ToString()); } }
private static void CheckResult(MMRESULT result, string function, string parameters) { const uint maxErrorDescriptionLength = 256; if (result == MMRESULT.MMSYSERR_NOERROR) return; var description = new StringBuilder((int)maxErrorDescriptionLength); midiOutGetErrorText((uint)result, description, (uint)description.Capacity); throw new InvalidOperationException($"{function}({parameters}) returned {result}({(int)result}): {description}"); }
/// <summary> /// 入力を中断する関数。 /// 未使用バッファは処理されます。 /// もし、複数のバッファが追加されていた場合、バッファの数だけMM_WIM_DATAメッセージが送られます。 /// </summary> public void WaveInReset() { MMRESULT result = waveInReset(this.WaveInHandle); //エラーが発生したら例外を投げる if (result != MMRESULT.MMSYSERR_NOERROR) { Exception e = new Exception("WaveInReset:" + result + "\r\nエラーが発生しました。"); } }
/// <summary> /// WaveInPrepareHeader()メソッドで準備したヘッダを解放します。 /// </summary> public void WaveInUnprepareHeader() { //エラーが発生したら例外を投げる MMRESULT result = waveInUnprepareHeader(this.WaveInHandle, this.WaveHeaderPtr, (uint)Marshal.SizeOf(typeof(WAVEHDR))); if (result != MMRESULT.MMSYSERR_NOERROR) { Exception e = new Exception("WaveInStop:" + result + "\r\nエラーが発生しました。"); } }
internal static MIDIINCAPS midiInGetDevCaps(int uDeviceID) { MIDIINCAPS res = new MIDIINCAPS(); MMRESULT err = midiInGetDevCaps(uDeviceID, ref res, (uint)Marshal.SizeOf(typeof(MIDIINCAPS))); if (err != MMRESULT.MMSYSERR_NOERROR) { throw new Exception("err: " + err.ToString()); } return(res); }
/// <summary> /// 再生を停止します。 /// </summary> public void WaveOutReset() { MMRESULT result = waveOutReset(this.WaveOutHandle); //エラーが発生したら例外を投げる if (result != MMRESULT.MMSYSERR_NOERROR) { Exception e = new Exception("waveOutReset:" + result); e.Data.Add("waveOutReset", result); throw e; } }
public void close() { if (opened) { MMRESULT result = midiOutClose(devHandle); if (result != MMRESULT.MMSYSERR_NOERROR) { throw new MidiSystemException("couldn't close output device " + devName); } opened = false; //Console.WriteLine("closed device " + devName + " result = " + result); } }
// midi funcs ----------------------------------------------------------------- public void open() { if (!opened) { MMRESULT result = midiOutOpen(out devHandle, devID, IntPtr.Zero, IntPtr.Zero, CALLBACK_NULL); if (result != MMRESULT.MMSYSERR_NOERROR) { throw new MidiSystemException("couldn't open output device " + devName); } opened = true; //Console.WriteLine("opened device " + devName + " result = " + result); } }
private static void CheckReturnCode(MMRESULT rc) { if (rc != MMRESULT.MMSYSERR_NOERROR) { var errorMsg = new StringBuilder(128); rc = Win32API.midiOutGetErrorText(rc, errorMsg); if (rc != MMRESULT.MMSYSERR_NOERROR) { throw new DeviceException("no error details"); } throw new DeviceException(errorMsg.ToString()); } }
//start the input device sending midi event msgs to HandleMessage() until stop() is called public void start() { if (!started) { MMRESULT result = midiInStart(devHandle); if (result != MMRESULT.MMSYSERR_NOERROR) { throw new MidiSystemException("couldn't start input device " + devName); } started = true; //Console.WriteLine("started device " + devName + " result = " + result); } }
// midi funcs ----------------------------------------------------------------- //open the input device and set its event handler to HandleMessage() public void open() { if (!opened) { midiInProc = HandleMessage; MMRESULT result = midiInOpen(out devHandle, devID, midiInProc, IntPtr.Zero, CALLBACK_FUNCTION); if (result != MMRESULT.MMSYSERR_NOERROR) { throw new MidiSystemException("couldn't open input device " + devName); } opened = true; //Console.WriteLine("opened device " + devName + " result = " + result); } }
public HeartBeat() //constructor { // For documentation on the correct values to use, please refer to the MSDN library. waveFormat = new WAVEFORMATEX(); waveFormat.wFormatTag = WAVE_FORMAT_PCM; waveFormat.nChannels = 1; waveFormat.nSamplesPerSec = nSamplesPerSec; waveFormat.nAvgBytesPerSec = nSamplesPerSec * 2; waveFormat.nBlockAlign = 2; waveFormat.wBitsPerSample = 16; waveFormat.cbSize = 0; MMRESULT res = waveInOpen(ref hwWaveIn, WAVE_MAPPER, ref waveFormat, dwCallBack, 0, WaveInOpenFlags.CALLBACK_NULL); if (res != MMRESULT.MMSYSERR_NOERROR) { GpsUtils.Utils.log.Error("waveInOpen", null); } //whdrsize = 2 * Marshal.SizeOf(Type.GetType("IntPtr")) + 6 * sizeof(Int32); for (int i = 0; i < nBuffers; i++) { buffer[i] = Marshal.AllocHGlobal(nSamples * 2); whdr[i] = Marshal.AllocHGlobal(whdrsize); Marshal.WriteInt32(whdr[i], lpData, (int)buffer[i]); //lpData = buffer[i]; Marshal.WriteInt32(whdr[i], dwBufferLength, nSamples * 2); //dwBufferLength = nSamples * 2; Marshal.WriteInt32(whdr[i], dwFlags, 0); //dwFlags = 0; res = waveInPrepareHeader(hwWaveIn, whdr[i], whdrsize); if (res != MMRESULT.MMSYSERR_NOERROR) { GpsUtils.Utils.log.Error("waveInPrepareHeader", null); } res = waveInAddBuffer(hwWaveIn, whdr[i], whdrsize); if (res != MMRESULT.MMSYSERR_NOERROR) { GpsUtils.Utils.log.Error("waveInAddBuffer", null); } } cur_whdr = -1; res = waveInStart(hwWaveIn); if (res != MMRESULT.MMSYSERR_NOERROR) { GpsUtils.Utils.log.Error("waveInStart", null); } PowerPolicyNotify(PPN_UNATTENDEDMODE, 1); wavPowerHandle = SetPowerRequirement("wav1:", CedevicePowerState.D0, POWER_NAME, null, 0); //HTC diamond don't works with POWER_FORCE thresh = Int16.MaxValue; lastbeepidx = -2 * nSamplesPerSec; }
/// <summary> /// データを再生バッファに送ります。 /// </summary> public void WaveOutWrite() { MMRESULT result = waveOutWrite( this.WaveOutHandle, this.WaveHeaderPtr[this.BuffCount], (uint)Marshal.SizeOf(typeof(WAVEHDR)) ); if (result != MMRESULT.MMSYSERR_NOERROR) { Exception e = new Exception("waveOutWrite:" + result); e.Data.Add("waveOutWrite", result); throw e; } }
/// <summary> /// 次に使われるヘッダを再生デバイスか解放します。 /// </summary> public void WaveOutUnprepareNextHeader() { MMRESULT result = waveOutUnprepareHeader( this.WaveOutHandle, this.WaveHeaderPtr[this.NextBuffNum], (uint)Marshal.SizeOf(typeof(WAVEHDR)) ); if (result != MMRESULT.MMSYSERR_NOERROR) { Exception e = new Exception("waveOutUnprepareNextHeader:" + result); e.Data.Add("waveOutUnprepareNextHeader", result); throw e; } }
/// <summary> /// 次のバッファを書き込みます。 /// </summary> public void WaveOutWriteNext() { MMRESULT result = waveOutWrite( this.WaveOutHandle, this.WaveHeaderPtr[this.NextBuffNum], (uint)Marshal.SizeOf(typeof(WAVEHDR)) ); //エラーが発生したら例外を投げる if (result != MMRESULT.MMSYSERR_NOERROR) { Exception e = new Exception("waveOutWriteNext:" + result); e.Data.Add("waveOutWriteNext", result); throw e; } }
public bool Open(int id) { DeviceId = id; IntPtr h; MMRESULT res = NativeMethods.midiInOpen( out h, id, midiInProc, IntPtr.Zero, NativeMethods.CALLBACK_FUNCTION); Handle = h; bool ret = res == MMRESULT.MMSYSERR_NOERROR && Start(); return(ret); }
/// <summary> /// wavInヘッダをひとつ準備します。 /// 使用前にWAVEHDR構造体のlpData,dwBufferLength,dwFlagsを初期化してください。 /// dwFlags = 0にしてください。 /// </summary> public void WaveInPrepareHeader(ref WAVEHDR whdr) { //初期化 whdr.dwBytesRecorded = 0; whdr.dwFlags = 0; //ヘッダの中身をポインタに写す。写す前にアンマネージ領域 Marshal.StructureToPtr(whdr, this.WaveHeaderPtr, true); //エラーが発生したら例外を投げる MMRESULT result = waveInPrepareHeader(this.WaveInHandle, this.WaveHeaderPtr, (uint)Marshal.SizeOf(whdr)); if (result != MMRESULT.MMSYSERR_NOERROR) { Exception e = new Exception("WaveInPrepareHeader:" + result + "\r\nエラーが発生しました。"); } }
/// <summary> /// 再生デバイスをオープンします。 /// </summary> public void WaveOutOpen() { MMRESULT result = waveOutOpen( ref this.WaveOutHandle, 0, ref this.wfe, this.WindowHandle, 0, CALLBACK_WINDOW ); //エラーが発生したら例外を投げる if (result != MMRESULT.MMSYSERR_NOERROR) { Exception e = new Exception("waveOutOpen:" + result); e.Data.Add("waveOutOpen", result); throw e; } }
/// <summary> /// 次に使われるヘッダの初期化を行います。 /// </summary> public void WaveOutPrepareNextHeader() { //次のヘッダを初期化 this.whdr[this.NextBuffNum].dwBytesRecorded = 0; this.whdr[this.NextBuffNum].dwFlags = 0; //初期化したヘッダをアンマネージ領域に書き込み Marshal.StructureToPtr(this.whdr[this.NextBuffNum], this.WaveHeaderPtr[this.NextBuffNum], true); MMRESULT result = waveOutPrepareHeader( this.WaveOutHandle, this.WaveHeaderPtr[this.NextBuffNum], (uint)Marshal.SizeOf(typeof(WAVEHDR)) ); //エラーが発生したら例外を投げる if (result != MMRESULT.MMSYSERR_NOERROR) { Exception e = new Exception("waveOutPrepareNextHeader:" + result); e.Data.Add("waveOutPrepareNextHeader", result); throw e; } }
/// <summary> /// クラス作成時に指定したハンドルを使って入力デバイスをオープンします。 /// MM_WIM_OPENメッセージが指定されたハンドルに送られます。 /// </summary> public void WaveInOpen(ref WAVEFORMATEX wfe) { //音声デバイスをオープン MMRESULT result = waveInOpen(ref this.WaveInHandle, 0, ref wfe, this.WindowHandle, 0, CALLBACK_WINDOW); //エラーが発生したら例外を発生させる if (result != MMRESULT.MMSYSERR_NOERROR) { //録音デバイスが見つからない場合 if (result == MMRESULT.MMSYSERR_BADDEVICEID) { Exception e = new Exception("WaveInOpen:" + result + "\r\n録音デバイスが見つかりません。\r\n端子を確認してください。"); throw e; } //それ以外 else { Exception e = new Exception("WaveInOpen:" + result + "\r\n録音デバイスのオープンに失敗しました。"); throw e; } } }
public MidiSystem() { //input devices int deviceID; uint incount = midiInGetNumDevs(); inputDevices = new List <InputDevice>(); MIDIINCAPS inCaps = new MIDIINCAPS(); for (deviceID = 0; deviceID < incount; deviceID++) { MMRESULT result = midiInGetDevCaps(deviceID, ref inCaps, Marshal.SizeOf(inCaps)); //if we get an error, just skip the device if (result == MMRESULT.MMSYSERR_NOERROR) { InputDevice indev = new InputDevice(deviceID, inCaps.szPname); inputDevices.Add(indev); } } //output devices uint outcount = midiOutGetNumDevs(); outputDevices = new List <OutputDevice>((int)outcount); MIDIOUTCAPS outCaps = new MIDIOUTCAPS(); for (deviceID = 0; deviceID < outcount; deviceID++) { MMRESULT result = midiOutGetDevCaps(deviceID, ref outCaps, Marshal.SizeOf(outCaps)); //if we get an error, just skip the device if (result == MMRESULT.MMSYSERR_NOERROR) { OutputDevice outdev = new OutputDevice(deviceID, outCaps.szPname); outputDevices.Add(outdev); } } }
/// <summary> /// Gets the error text for a return code related to an output device. /// </summary> /// NOTE: This is adapted from the original Win32 function in order to make it typesafe. /// /// Win32 docs: http://msdn.microsoft.com/en-us/library/ms711622(VS.85).aspx public static MMRESULT midiOutGetErrorText(MMRESULT mmrError, StringBuilder lpText) { return midiOutGetErrorText(mmrError, lpText, (UInt32)lpText.Capacity); }
private static extern MMRESULT midiOutGetErrorText(MMRESULT mmrError, StringBuilder lpText, UInt32 cchText);
public MmException(MMRESULT Result) : base(Result.ToString()) { result = Result; }
private static void Check(MMRESULT code) { if (code != MMRESULT.MMSYSERR_NOERROR) { throw new FonixTalkException(code); } }
internal FonixTalkException(MMRESULT code) : base(GetMessage(code)) { }
public static void CheckThrow(MMRESULT result) { if (result != MMRESULT.NOERROR) throw new MmException(result); }
internal WaveException(MMRESULT mmResult) : base(MakeMessage(mmResult)) { }
/// <summary> /// Gets the error text for a return code related to an input device. /// </summary> /// NOTE: This is adapted from the original Win32 function in order to make it typesafe. /// /// Win32 docs: http://msdn.microsoft.com/en-us/library/ms711605(VS.85).aspx public static MMRESULT midiInGetErrorText(MMRESULT mmrError, StringBuilder lpText) { return(midiInGetErrorText(mmrError, lpText, (UInt32)lpText.Capacity)); }
private static extern MMRESULT midiInGetErrorText(MMRESULT mmrError, StringBuilder lpText, UInt32 cchText);