From 16d543236dfc2d4de71b49c465113f7289bae400 Mon Sep 17 00:00:00 2001 From: Mark Heath Date: Tue, 10 May 2022 14:02:35 +0100 Subject: [PATCH] WASAPI loopback capture also supports sample rate conversion --- NAudio.Wasapi/WasapiLoopbackCapture.cs | 11 +- .../RecordingDemo/RecordingPanel.Designer.cs | 161 ++++++++++-------- NAudioDemo/RecordingDemo/RecordingPanel.cs | 20 ++- NAudioDemo/RecordingDemo/RecordingPanel.resx | 60 ------- 4 files changed, 100 insertions(+), 152 deletions(-) diff --git a/NAudio.Wasapi/WasapiLoopbackCapture.cs b/NAudio.Wasapi/WasapiLoopbackCapture.cs index 65dc024..9d45e86 100644 --- a/NAudio.Wasapi/WasapiLoopbackCapture.cs +++ b/NAudio.Wasapi/WasapiLoopbackCapture.cs @@ -36,22 +36,13 @@ namespace NAudio.Wave MMDeviceEnumerator devices = new MMDeviceEnumerator(); return devices.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); } - - /// - /// Capturing wave format - /// - public override WaveFormat WaveFormat - { - get { return base.WaveFormat; } - set { throw new InvalidOperationException("WaveFormat cannot be set for WASAPI Loopback Capture"); } - } /// /// Specify loopback /// protected override AudioClientStreamFlags GetAudioClientStreamFlags() { - return AudioClientStreamFlags.Loopback; + return AudioClientStreamFlags.Loopback | base.GetAudioClientStreamFlags(); } } } diff --git a/NAudioDemo/RecordingDemo/RecordingPanel.Designer.cs b/NAudioDemo/RecordingDemo/RecordingPanel.Designer.cs index d79573d..3d216e5 100644 --- a/NAudioDemo/RecordingDemo/RecordingPanel.Designer.cs +++ b/NAudioDemo/RecordingDemo/RecordingPanel.Designer.cs @@ -33,7 +33,6 @@ this.label1 = new System.Windows.Forms.Label(); this.buttonStopRecording = new System.Windows.Forms.Button(); this.progressBar1 = new System.Windows.Forms.ProgressBar(); - this.label2 = new System.Windows.Forms.Label(); this.listBoxRecordings = new System.Windows.Forms.ListBox(); this.buttonPlay = new System.Windows.Forms.Button(); this.buttonDelete = new System.Windows.Forms.Button(); @@ -48,15 +47,17 @@ this.radioButtonWasapiLoopback = new System.Windows.Forms.RadioButton(); this.radioButtonWasapi = new System.Windows.Forms.RadioButton(); this.radioButtonWaveIn = new System.Windows.Forms.RadioButton(); + this.label2 = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); this.groupBoxRecordingApi.SuspendLayout(); this.SuspendLayout(); // // buttonStartRecording // - this.buttonStartRecording.Location = new System.Drawing.Point(419, 118); - this.buttonStartRecording.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.buttonStartRecording.Location = new System.Drawing.Point(1056, 307); + this.buttonStartRecording.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.buttonStartRecording.Name = "buttonStartRecording"; - this.buttonStartRecording.Size = new System.Drawing.Size(140, 28); + this.buttonStartRecording.Size = new System.Drawing.Size(298, 72); this.buttonStartRecording.TabIndex = 0; this.buttonStartRecording.Text = "Start Recording"; this.buttonStartRecording.UseVisualStyleBackColor = true; @@ -66,19 +67,19 @@ // this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.label1.Location = new System.Drawing.Point(16, 20); - this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); + this.label1.Location = new System.Drawing.Point(34, 51); + this.label1.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0); this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(787, 54); + this.label1.Size = new System.Drawing.Size(1863, 138); this.label1.TabIndex = 1; this.label1.Text = resources.GetString("label1.Text"); // // buttonStopRecording // - this.buttonStopRecording.Location = new System.Drawing.Point(581, 118); - this.buttonStopRecording.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.buttonStopRecording.Location = new System.Drawing.Point(1401, 307); + this.buttonStopRecording.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.buttonStopRecording.Name = "buttonStopRecording"; - this.buttonStopRecording.Size = new System.Drawing.Size(140, 28); + this.buttonStopRecording.Size = new System.Drawing.Size(298, 72); this.buttonStopRecording.TabIndex = 0; this.buttonStopRecording.Text = "Stop Recording"; this.buttonStopRecording.UseVisualStyleBackColor = true; @@ -86,42 +87,32 @@ // // progressBar1 // - this.progressBar1.Location = new System.Drawing.Point(419, 191); - this.progressBar1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.progressBar1.Location = new System.Drawing.Point(1056, 494); + this.progressBar1.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.progressBar1.Maximum = 30; this.progressBar1.Name = "progressBar1"; - this.progressBar1.Size = new System.Drawing.Size(343, 28); + this.progressBar1.Size = new System.Drawing.Size(729, 72); this.progressBar1.TabIndex = 4; // - // label2 - // - this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(415, 167); - this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); - this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(131, 16); - this.label2.TabIndex = 5; - this.label2.Text = "Recording Progress:"; - // // listBoxRecordings // this.listBoxRecordings.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.listBoxRecordings.FormattingEnabled = true; - this.listBoxRecordings.ItemHeight = 16; - this.listBoxRecordings.Location = new System.Drawing.Point(20, 302); - this.listBoxRecordings.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.listBoxRecordings.ItemHeight = 41; + this.listBoxRecordings.Location = new System.Drawing.Point(42, 774); + this.listBoxRecordings.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.listBoxRecordings.Name = "listBoxRecordings"; - this.listBoxRecordings.Size = new System.Drawing.Size(673, 132); + this.listBoxRecordings.Size = new System.Drawing.Size(1617, 332); this.listBoxRecordings.TabIndex = 8; // // buttonPlay // this.buttonPlay.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonPlay.Location = new System.Drawing.Point(703, 335); - this.buttonPlay.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.buttonPlay.Location = new System.Drawing.Point(1685, 858); + this.buttonPlay.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.buttonPlay.Name = "buttonPlay"; - this.buttonPlay.Size = new System.Drawing.Size(100, 28); + this.buttonPlay.Size = new System.Drawing.Size(212, 72); this.buttonPlay.TabIndex = 9; this.buttonPlay.Text = "Play"; this.buttonPlay.UseVisualStyleBackColor = true; @@ -130,10 +121,10 @@ // buttonDelete // this.buttonDelete.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonDelete.Location = new System.Drawing.Point(703, 370); - this.buttonDelete.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.buttonDelete.Location = new System.Drawing.Point(1685, 948); + this.buttonDelete.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.buttonDelete.Name = "buttonDelete"; - this.buttonDelete.Size = new System.Drawing.Size(100, 28); + this.buttonDelete.Size = new System.Drawing.Size(212, 72); this.buttonDelete.TabIndex = 10; this.buttonDelete.Text = "Delete"; this.buttonDelete.UseVisualStyleBackColor = true; @@ -142,10 +133,10 @@ // buttonOpenFolder // this.buttonOpenFolder.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonOpenFolder.Location = new System.Drawing.Point(703, 406); - this.buttonOpenFolder.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.buttonOpenFolder.Location = new System.Drawing.Point(1685, 1040); + this.buttonOpenFolder.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.buttonOpenFolder.Name = "buttonOpenFolder"; - this.buttonOpenFolder.Size = new System.Drawing.Size(100, 28); + this.buttonOpenFolder.Size = new System.Drawing.Size(212, 72); this.buttonOpenFolder.TabIndex = 10; this.buttonOpenFolder.Text = "Open Folder"; this.buttonOpenFolder.UseVisualStyleBackColor = true; @@ -153,6 +144,7 @@ // // groupBoxRecordingApi // + this.groupBoxRecordingApi.Controls.Add(this.label3); this.groupBoxRecordingApi.Controls.Add(this.comboWasapiLoopbackDevices); this.groupBoxRecordingApi.Controls.Add(this.checkBoxEventCallback); this.groupBoxRecordingApi.Controls.Add(this.comboBoxChannels); @@ -162,11 +154,11 @@ this.groupBoxRecordingApi.Controls.Add(this.radioButtonWasapiLoopback); this.groupBoxRecordingApi.Controls.Add(this.radioButtonWasapi); this.groupBoxRecordingApi.Controls.Add(this.radioButtonWaveIn); - this.groupBoxRecordingApi.Location = new System.Drawing.Point(20, 81); - this.groupBoxRecordingApi.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.groupBoxRecordingApi.Location = new System.Drawing.Point(42, 208); + this.groupBoxRecordingApi.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.groupBoxRecordingApi.Name = "groupBoxRecordingApi"; - this.groupBoxRecordingApi.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4); - this.groupBoxRecordingApi.Size = new System.Drawing.Size(359, 213); + this.groupBoxRecordingApi.Padding = new System.Windows.Forms.Padding(8, 10, 8, 10); + this.groupBoxRecordingApi.Size = new System.Drawing.Size(966, 546); this.groupBoxRecordingApi.TabIndex = 11; this.groupBoxRecordingApi.TabStop = false; this.groupBoxRecordingApi.Text = "Recording API"; @@ -175,19 +167,21 @@ // this.comboWasapiLoopbackDevices.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboWasapiLoopbackDevices.FormattingEnabled = true; - this.comboWasapiLoopbackDevices.Location = new System.Drawing.Point(188, 178); - this.comboWasapiLoopbackDevices.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.comboWasapiLoopbackDevices.Location = new System.Drawing.Point(399, 249); + this.comboWasapiLoopbackDevices.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.comboWasapiLoopbackDevices.Name = "comboWasapiLoopbackDevices"; - this.comboWasapiLoopbackDevices.Size = new System.Drawing.Size(160, 24); + this.comboWasapiLoopbackDevices.Size = new System.Drawing.Size(551, 49); this.comboWasapiLoopbackDevices.TabIndex = 14; // // checkBoxEventCallback // this.checkBoxEventCallback.AutoSize = true; - this.checkBoxEventCallback.Location = new System.Drawing.Point(188, 50); - this.checkBoxEventCallback.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.checkBoxEventCallback.Checked = true; + this.checkBoxEventCallback.CheckState = System.Windows.Forms.CheckState.Checked; + this.checkBoxEventCallback.Location = new System.Drawing.Point(400, 99); + this.checkBoxEventCallback.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.checkBoxEventCallback.Name = "checkBoxEventCallback"; - this.checkBoxEventCallback.Size = new System.Drawing.Size(126, 20); + this.checkBoxEventCallback.Size = new System.Drawing.Size(259, 45); this.checkBoxEventCallback.TabIndex = 13; this.checkBoxEventCallback.Text = "Event Callbacks"; this.checkBoxEventCallback.UseVisualStyleBackColor = true; @@ -196,49 +190,49 @@ // this.comboBoxChannels.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxChannels.FormattingEnabled = true; - this.comboBoxChannels.Location = new System.Drawing.Point(188, 82); - this.comboBoxChannels.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.comboBoxChannels.Location = new System.Drawing.Point(386, 465); + this.comboBoxChannels.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.comboBoxChannels.Name = "comboBoxChannels"; - this.comboBoxChannels.Size = new System.Drawing.Size(160, 24); + this.comboBoxChannels.Size = new System.Drawing.Size(336, 49); this.comboBoxChannels.TabIndex = 12; // // comboBoxSampleRate // this.comboBoxSampleRate.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxSampleRate.FormattingEnabled = true; - this.comboBoxSampleRate.Location = new System.Drawing.Point(20, 82); - this.comboBoxSampleRate.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.comboBoxSampleRate.Location = new System.Drawing.Point(34, 465); + this.comboBoxSampleRate.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.comboBoxSampleRate.Name = "comboBoxSampleRate"; - this.comboBoxSampleRate.Size = new System.Drawing.Size(160, 24); + this.comboBoxSampleRate.Size = new System.Drawing.Size(336, 49); this.comboBoxSampleRate.TabIndex = 12; // // comboWaveInDevice // this.comboWaveInDevice.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboWaveInDevice.FormattingEnabled = true; - this.comboWaveInDevice.Location = new System.Drawing.Point(188, 16); - this.comboWaveInDevice.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.comboWaveInDevice.Location = new System.Drawing.Point(400, 41); + this.comboWaveInDevice.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.comboWaveInDevice.Name = "comboWaveInDevice"; - this.comboWaveInDevice.Size = new System.Drawing.Size(160, 24); + this.comboWaveInDevice.Size = new System.Drawing.Size(550, 49); this.comboWaveInDevice.TabIndex = 12; // // comboWasapiDevices // this.comboWasapiDevices.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboWasapiDevices.FormattingEnabled = true; - this.comboWasapiDevices.Location = new System.Drawing.Point(188, 143); - this.comboWasapiDevices.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.comboWasapiDevices.Location = new System.Drawing.Point(400, 172); + this.comboWasapiDevices.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.comboWasapiDevices.Name = "comboWasapiDevices"; - this.comboWasapiDevices.Size = new System.Drawing.Size(160, 24); + this.comboWasapiDevices.Size = new System.Drawing.Size(551, 49); this.comboWasapiDevices.TabIndex = 12; // // radioButtonWasapiLoopback // this.radioButtonWasapiLoopback.AutoSize = true; - this.radioButtonWasapiLoopback.Location = new System.Drawing.Point(8, 176); - this.radioButtonWasapiLoopback.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.radioButtonWasapiLoopback.Location = new System.Drawing.Point(16, 244); + this.radioButtonWasapiLoopback.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.radioButtonWasapiLoopback.Name = "radioButtonWasapiLoopback"; - this.radioButtonWasapiLoopback.Size = new System.Drawing.Size(144, 20); + this.radioButtonWasapiLoopback.Size = new System.Drawing.Size(299, 45); this.radioButtonWasapiLoopback.TabIndex = 8; this.radioButtonWasapiLoopback.Text = "WASAPI Loopback"; this.radioButtonWasapiLoopback.UseVisualStyleBackColor = true; @@ -246,30 +240,50 @@ // radioButtonWasapi // this.radioButtonWasapi.AutoSize = true; - this.radioButtonWasapi.Location = new System.Drawing.Point(8, 148); - this.radioButtonWasapi.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.radioButtonWasapi.Checked = true; + this.radioButtonWasapi.Location = new System.Drawing.Point(16, 172); + this.radioButtonWasapi.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.radioButtonWasapi.Name = "radioButtonWasapi"; - this.radioButtonWasapi.Size = new System.Drawing.Size(80, 20); + this.radioButtonWasapi.Size = new System.Drawing.Size(161, 45); this.radioButtonWasapi.TabIndex = 9; + this.radioButtonWasapi.TabStop = true; this.radioButtonWasapi.Text = "WASAPI"; this.radioButtonWasapi.UseVisualStyleBackColor = true; // // radioButtonWaveIn // this.radioButtonWaveIn.AutoSize = true; - this.radioButtonWaveIn.Checked = true; - this.radioButtonWaveIn.Location = new System.Drawing.Point(8, 21); - this.radioButtonWaveIn.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.radioButtonWaveIn.Location = new System.Drawing.Point(17, 54); + this.radioButtonWaveIn.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.radioButtonWaveIn.Name = "radioButtonWaveIn"; - this.radioButtonWaveIn.Size = new System.Drawing.Size(70, 20); + this.radioButtonWaveIn.Size = new System.Drawing.Size(147, 45); this.radioButtonWaveIn.TabIndex = 11; - this.radioButtonWaveIn.TabStop = true; this.radioButtonWaveIn.Text = "waveIn"; this.radioButtonWaveIn.UseVisualStyleBackColor = true; // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(1048, 433); + this.label2.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(283, 41); + this.label2.TabIndex = 5; + this.label2.Text = "Recording Progress:"; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(34, 392); + this.label3.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(225, 41); + this.label3.TabIndex = 15; + this.label3.Text = "Capture Format"; + // // RecordingPanel // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); + this.AutoScaleDimensions = new System.Drawing.SizeF(17F, 41F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.groupBoxRecordingApi); this.Controls.Add(this.buttonOpenFolder); @@ -281,9 +295,9 @@ this.Controls.Add(this.label1); this.Controls.Add(this.buttonStopRecording); this.Controls.Add(this.buttonStartRecording); - this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.Margin = new System.Windows.Forms.Padding(8, 10, 8, 10); this.Name = "RecordingPanel"; - this.Size = new System.Drawing.Size(819, 457); + this.Size = new System.Drawing.Size(1931, 1171); this.groupBoxRecordingApi.ResumeLayout(false); this.groupBoxRecordingApi.PerformLayout(); this.ResumeLayout(false); @@ -297,7 +311,6 @@ private System.Windows.Forms.Label label1; private System.Windows.Forms.Button buttonStopRecording; private System.Windows.Forms.ProgressBar progressBar1; - private System.Windows.Forms.Label label2; private System.Windows.Forms.ListBox listBoxRecordings; private System.Windows.Forms.Button buttonPlay; private System.Windows.Forms.Button buttonDelete; @@ -312,5 +325,7 @@ private System.Windows.Forms.ComboBox comboBoxChannels; private System.Windows.Forms.ComboBox comboBoxSampleRate; private System.Windows.Forms.ComboBox comboWasapiLoopbackDevices; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.Label label3; } } \ No newline at end of file diff --git a/NAudioDemo/RecordingDemo/RecordingPanel.cs b/NAudioDemo/RecordingDemo/RecordingPanel.cs index c894174..327f0be 100644 --- a/NAudioDemo/RecordingDemo/RecordingPanel.cs +++ b/NAudioDemo/RecordingDemo/RecordingPanel.cs @@ -32,7 +32,7 @@ namespace NAudioDemo.RecordingDemo } LoadWaveInDevicesCombo(); comboBoxSampleRate.DataSource = new[] {8000, 16000, 22050, 32000, 44100, 48000}; - comboBoxSampleRate.SelectedIndex = 0; + comboBoxSampleRate.SelectedIndex = 3; comboBoxChannels.DataSource = new[] { "Mono", "Stereo" }; comboBoxChannels.SelectedIndex = 0; outputFolder = Path.Combine(Path.GetTempPath(), "NAudioDemo"); @@ -44,6 +44,7 @@ namespace NAudioDemo.RecordingDemo radioButtonWasapiLoopback.CheckedChanged += (s, a) => Cleanup(); checkBoxEventCallback.CheckedChanged += (s, a) => Cleanup(); comboWaveInDevice.SelectedIndexChanged += (s, a) => Cleanup(); + comboBoxSampleRate.SelectedIndexChanged += (s, a) => Cleanup(); comboBoxChannels.SelectedIndexChanged += (s, a) => Cleanup(); comboWasapiDevices.SelectedIndexChanged += (s, a) => Cleanup(); comboWasapiLoopbackDevices.SelectedIndexChanged += (s, a) => Cleanup(); @@ -79,7 +80,9 @@ namespace NAudioDemo.RecordingDemo private void OnButtonStartRecordingClick(object sender, EventArgs e) { if (radioButtonWaveIn.Checked) + { Cleanup(); // WaveIn is still unreliable in some circumstances to being reused + } if (captureDevice == null) { @@ -118,25 +121,22 @@ namespace NAudioDemo.RecordingDemo { newWaveIn = new WaveIn() { DeviceNumber = deviceNumber }; } - var sampleRate = (int)comboBoxSampleRate.SelectedItem; - var channels = comboBoxChannels.SelectedIndex + 1; - newWaveIn.WaveFormat = new WaveFormat(sampleRate, channels); } else if (radioButtonWasapi.Checked) { var device = (MMDevice) comboWasapiDevices.SelectedItem; newWaveIn = new WasapiCapture(device); - // WASAPI with Sample Rate conversion! - var sampleRate = (int)comboBoxSampleRate.SelectedItem; - var channels = comboBoxChannels.SelectedIndex + 1; - newWaveIn.WaveFormat = new WaveFormat(sampleRate, channels); } else { - // can't set WaveFormat as WASAPI doesn't support SRC var device = (MMDevice)comboWasapiLoopbackDevices.SelectedItem; newWaveIn = new WasapiLoopbackCapture(device); } + // Both WASAPI and WaveIn support Sample Rate conversion! + var sampleRate = (int)comboBoxSampleRate.SelectedItem; + var channels = comboBoxChannels.SelectedIndex + 1; + newWaveIn.WaveFormat = new WaveFormat(sampleRate, channels); + newWaveIn.DataAvailable += OnDataAvailable; newWaveIn.RecordingStopped += OnRecordingStopped; return newWaveIn; @@ -167,6 +167,8 @@ namespace NAudioDemo.RecordingDemo { if (captureDevice != null) { + captureDevice.DataAvailable -= OnDataAvailable; + captureDevice.RecordingStopped -= OnRecordingStopped; captureDevice.Dispose(); captureDevice = null; } diff --git a/NAudioDemo/RecordingDemo/RecordingPanel.resx b/NAudioDemo/RecordingDemo/RecordingPanel.resx index 852b571..97a1f69 100644 --- a/NAudioDemo/RecordingDemo/RecordingPanel.resx +++ b/NAudioDemo/RecordingDemo/RecordingPanel.resx @@ -1,64 +1,4 @@ - -