/******************************************************/ /// <summary> /// レコーディング開始 /// </summary> /// <example href="https://stackoverrun.com/ja/q/3869314"> /// デフォルトWaveInコンストラクタは、コールバックのWindowsメッセージを使用しています。 /// ただし、コンソールアプリケーションやバックグラウンドスレッドを実行している場合、 /// これらのWindowsメッセージは処理されません。最も簡単な解決策は、 /// /// 代わりにWaveInEventクラスを使用することです。 /// waveInStream = new WaveInEvent(); /// </example> public void Start(int deviceId) { // waveInインスタンスの生成 try { //waveIn = new NAudio.Wave.WaveIn(); waveIn = new NAudio.Wave.WaveInEvent { DeviceNumber = deviceId, WaveFormat = new NAudio.Wave.WaveFormat(FrameSamples, Bits, Channels), NumberOfBuffers = 2, BufferMilliseconds = (int)(BufferLength * 1f / FrameSamples * 1000f) }; } catch (Exception ex) { this.Clean(); throw ex; } // コールバックを追加 waveIn.DataAvailable += OnDataAvailable; // 録音開始 waveIn.StartRecording(); }
public static string stringer(NAudio.Wave.IWaveIn wp) { if (wp == null) { return("F****D"); } return(stringer(wp.WaveFormat)); }
public SoundCardRecorder(string filePath, string song) { FilePath = filePath; Song = song; _waveIn = new NAudio.Wave.WasapiLoopbackCapture(); _writer = new NAudio.Wave.WaveFileWriter(FilePath, _waveIn.WaveFormat); _waveIn.DataAvailable += OnDataAvailable; }
/// <summary> /// インスタンスの開放 /// </summary> private void Clean() { if (waveIn != null) { waveIn.DataAvailable -= OnDataAvailable; } waveIn?.StopRecording(); waveIn?.Dispose(); waveIn = null; }
public void Dispose() { if (_waveIn != null) { _waveIn.StopRecording(); _waveIn.Dispose(); _waveIn = null; } if (_writer != null) { _writer.Close(); _writer.Dispose(); _writer = null; } }
public void Stop() { System.TimeSpan timeSpan = System.DateTime.Now.Subtract(_startTime); _lastRecordingDuration = (int)timeSpan.TotalSeconds; if (_waveIn != null) { _waveIn.StopRecording(); _waveIn.Dispose(); _waveIn = null; } if (_writer != null) { _writer.Close(); _writer.Dispose(); _writer = null; } }
public bool test() { tested = true; wf = null; try { if (mm == null || mm.State == NAudio.CoreAudioApi.DeviceState.NotPresent || mm.State == NAudio.CoreAudioApi.DeviceState.Unplugged) { return(false); } capt1 = mm.FriendlyName; // windows name capt2 = mm.DeviceFriendlyName; // just device if (capt1.EndsWith(capt2 + ")")) { capt1 = capt1.Substring(0, capt1.Length - (capt2.Length + 3)); } isRec = mm.DataFlow == NAudio.CoreAudioApi.DataFlow.All || mm.DataFlow == NAudio.CoreAudioApi.DataFlow.Capture; isPlay = mm.DataFlow == NAudio.CoreAudioApi.DataFlow.All || mm.DataFlow == NAudio.CoreAudioApi.DataFlow.Render; NAudio.Wave.IWaveIn dev = isPlay ? new NAudio.Wave.WasapiLoopbackCapture(mm) : new NAudio.CoreAudioApi.WasapiCapture(mm); if (dev != null) { wf = dev.WaveFormat; makeSerializationData(); dev.Dispose(); return(true); } } catch (System.Runtime.InteropServices.COMException) { mm = null; } return(false); }
public bool test() { tested = true; wf = null; try { if (mm == null) { return(false); } NAudio.Wave.IWaveIn dev = null; capt1 = mm.FriendlyName; // windows name capt2 = mm.DeviceFriendlyName; // just device if (capt1.EndsWith(capt2 + ")")) { capt1 = capt1.Substring(0, capt1.Length - (capt2.Length + 3)); } isRec = mm.DataFlow == NAudio.CoreAudioApi.DataFlow.All || mm.DataFlow == NAudio.CoreAudioApi.DataFlow.Capture; isPlay = mm.DataFlow == NAudio.CoreAudioApi.DataFlow.All || mm.DataFlow == NAudio.CoreAudioApi.DataFlow.Render; dev = isPlay ? dev = new NAudio.Wave.WasapiLoopbackCapture(mm) : dev = new NAudio.CoreAudioApi.WasapiCapture(mm); if (dev != null) { wf = dev.WaveFormat; makeSerializationData(); dev.Dispose(); return(true); } } catch { mm = null; } return(false); }
void helloworld(object sender, EventArgs e) { if (settings.devRec == null || settings.devRec.mm == null) { MessageBox.Show("I'm about to open the settings window.\n\n" + "In the second dropdown (input Music), please select the speakers output you use when listening to music.\n\nPress apply when done."); gSettings_Click(sender, e); if (settings.devRec == null || settings.devRec.mm == null) { MessageBox.Show("Sorry, but the settings are still invalid. Please try again or something."); return; } } string app, str = ""; app = "CAPTURE_ENDPOINT = " + settings.devRec.mm.ToString() + "\r\n"; File.AppendAllText("Loopstream.log", app); str += app; wdt_waveIn = new NAudio.Wave.WasapiLoopbackCapture(settings.devRec.mm); app = "WASAPI_FMT = " + LSDevice.stringer(wdt_waveIn) + "\r\n"; File.AppendAllText("Loopstream.log", app); str += app; Clipboard.Clear(); Clipboard.SetText(str); MessageBox.Show( "Capture will begin when you press OK; please open a media player and start listening to some music.\n\n" + "While you wait, the following text is on your clipboard... Paste it in irc (Ctrl-V)\n\n" + str); wdt_v = File.OpenWrite("Loopstream.raw"); wdt_writer = new NAudio.Wave.WaveFileWriter("Loopstream.wav", wdt_waveIn.WaveFormat); wdt_waveIn.DataAvailable += wdt_OnDataAvailable; wdt_waveIn.StartRecording(); while (true) { if (val < 0) break; gMic.level = val; Application.DoEvents(); System.Threading.Thread.Sleep(110); val += 2; } gMic.level = 0; if (DialogResult.Yes == MessageBox.Show("Test finished! The soundclip has been recorded to Loopstream.wav in the " + "same folder as this .exe, more specifically here:\n\n" + Application.StartupPath + "\n\n" + "Could you uploading this to pomf.se? Thanks!", "Open browser?", MessageBoxButtons.YesNo)) { System.Diagnostics.Process.Start("http://pomf.se/"); } }