public AudioOutputDevice(WaveDataViewModel data_model, int device_index) { this.data_model = data_model; left_val = new List <double>(); right_val = new List <double>(); source_stream = new WasapiLoopbackCapture(new MMDeviceEnumerator().EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active)[device_index]); source_stream.DataAvailable += (obj, e) => { (List <Point>, List <Point>)points = convert_from_bytes(source_stream.WaveFormat.BitsPerSample, source_stream.WaveFormat.Channels, e.Buffer, e.BytesRecorded); lock ( left_val ) { left_val.AddRange(points.Item1.Select(x => x.Y).ToList()); } lock ( right_val ) { right_val.AddRange(points.Item2.Select(x => x.Y).ToList()); } /* * var truncated_buffer_L = truncate_list(points.Item1.Select(x => x.Y).ToList(), 100); * List<Point> truncated_points_L = new List<Point>(); * for (int i = 0; i < truncated_buffer_L.Count; ++i) { * truncated_points_L.Add(new Point() { Y = truncated_buffer_L[i], X = (double)i / truncated_buffer_L.Count }); * } * data_model.DataL.Clear(); * data_model.DataL.AddRange(truncated_points_L); * * var freq_point_L = getFFT_points(source_stream.WaveFormat.SampleRate, points.Item1); * data_model.FreqL.Clear(); * data_model.FreqL.AddRange(freq_point_L.GetRange(0, 200).Select(x => new Point(x.Item1, x.Item2.Magnitude))); * //*/ /* * var truncated_buffer_R = truncate_list(points.Item2.Select(x => x.Y).ToList(), 100); * List<Point> truncated_points_R = new List<Point>(); * for (int i = 0; i < truncated_buffer_R.Count; ++i) { * truncated_points_R.Add(new Point() { Y = truncated_buffer_R[i], X = (double)i / truncated_buffer_R.Count }); * } * data_model.DataR.Clear(); * data_model.DataR.AddRange(truncated_points_R); * * var freq_point_R = getFFT_points(source_stream.WaveFormat.SampleRate, points.Item2); * data_model.FreqR.Clear(); * data_model.FreqR.AddRange(freq_point_R.GetRange(0, 200).Select(x => new Point(x.Item1, x.Item2.Magnitude))); * /* * NoiseCanceler nc = new NoiseCanceler(); * var tmp = nc.apply_freq_weight(get_sample_rate(), right_val); * var truncated_tmp = truncate_list(tmp, 100); * List<Point> truncated_points_tmp = new List<Point>(); * for (int i = 0; i < truncated_tmp.Count; ++i) { * truncated_points_tmp.Add(new Point() { Y = truncated_tmp[i], X = (double)i / truncated_tmp.Count }); * } * data_model.DataOutput.Clear(); * data_model.DataOutput.AddRange(truncated_points_tmp); * * var freq_tmp = getFFT_points(source_stream.WaveFormat.SampleRate, tmp.Select(x => new Point(0, x)).ToList()); * data_model.FreqOutput.Clear(); * data_model.FreqOutput.AddRange(freq_tmp.GetRange(0, 200).Select(x => new Point(x.Item1, x.Item2.Magnitude))); * //*/ }; }
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(); }