private void init_event() { this.comboBox_input.SelectionChanged += (sender, e) => { ComboBox cur_cb = (ComboBox)sender; input_device = new AudioInputDevice(wave_data_model_input, env_device, cur_cb.SelectedIndex, comboBox_output.SelectedIndex); }; this.comboBox_speaker.SelectionChanged += (sender, e) => { ComboBox cur_cb = (ComboBox)sender; env_device = new AudioOutputDevice(wave_data_model_env, cur_cb.SelectedIndex); }; this.comboBox_output.SelectionChanged += (sender, e) => { ComboBox cur_cb = (ComboBox)sender; input_device = new AudioInputDevice(wave_data_model_input, env_device, comboBox_input.SelectedIndex, cur_cb.SelectedIndex); }; this.button_start.Click += (sender, e) => { input_device.start(); env_device.start(); }; this.button_stop.Click += (sender, e) => { input_device.stop(); env_device.stop(); }; }
public AudioInputDevice(WaveDataViewModel data_model, AudioOutputDevice environment, int input_device_index, int output_device_index) { this.data_model = data_model; noise_canceler = new NoiseCanceler(); source_stream = new NAudio.Wave.WaveIn(); source_stream.DeviceNumber = input_device_index; source_stream.WaveFormat = new WaveFormat(44100, 24, 1); dest = new NAudio.Wave.WaveOut(); dest.DeviceNumber = output_device_index; dest.DesiredLatency = 100; wave_provider = new BufferedWaveProvider(source_stream.WaveFormat); wave_provider.DiscardOnBufferOverflow = true; dest.Init(wave_provider); int cnt = 0; List <double> buffers = new List <double>(); source_stream.DataAvailable += (obj, e) => { /* * var truncated_points = get_point_from_bytes(e.Buffer, e.BytesRecorded); * data_model.DataL.Clear(); * data_model.DataL.AddRange(truncated_points); * * var freq_point = getFFT_points(source_stream.WaveFormat.SampleRate, * convert_from_bytes(source_stream.WaveFormat.BitsPerSample, source_stream.WaveFormat.Channels, e.Buffer, e.BytesRecorded).Item1); * data_model.FreqL.Clear(); * data_model.FreqL.AddRange(freq_point.GetRange(0, 200).Select(x => new Point(x.Item1, x.Item2.Magnitude))); * //*/ //////////////////////////////////////////////////////////////////////////////////////////////////// List <double> source_point = convert_from_bytes(source_stream.WaveFormat.BitsPerSample, source_stream.WaveFormat.Channels, e.Buffer, e.BytesRecorded).Item1 .Select(x => x.Y) .ToList(); List <double> canceled_buf = noise_canceler.get_noise_canceled_buffer(environment.get_sample_rate(), environment.get_left_val(), environment.get_right_val(), source_stream.WaveFormat.SampleRate, source_point); byte[] result = convert_from_list(dest.OutputWaveFormat.BitsPerSample, dest.OutputWaveFormat.Channels, canceled_buf).Item1; wave_provider.AddSamples(result, 0, result.Length); /* * var truncated_points = get_point_from_bytes(result, result.Length); * data_model.DataOutput.Clear(); * data_model.DataOutput.AddRange(truncated_points); * * var freq_point = getFFT_points(dest.OutputWaveFormat.SampleRate, * convert_from_bytes(dest.OutputWaveFormat.BitsPerSample, dest.OutputWaveFormat.Channels, result, result.Length).Item1); * data_model.FreqOutput.Clear(); * data_model.FreqOutput.AddRange(freq_point.GetRange(0, 200).Select(x => new Point(x.Item1, x.Item2.Magnitude))); * //*/ }; }
public MainWindow() { InitializeComponent(); wave_data_model_env = new WaveDataViewModel(); env_device = new AudioOutputDevice(wave_data_model_env, 0); audio_env_grid.DataContext = wave_data_model_env; wave_data_model_input = new WaveDataViewModel(); input_device = new AudioInputDevice(wave_data_model_input, env_device, 0, 0); audo_input_grid.DataContext = wave_data_model_input; init_event(); }
private void init_audio_device() { AudioInputDevices = AudioInputDevice.getInputDevices(); AudioEnvironmentDevices = AudioOutputDevice.getOutputDevices(); AudioOutputDevices = AudioInputDevice.getOutputDevices(); }