public void Dispose()
 {
     if (_waveIn != null)
     {
         _waveIn.StopRecording();
         _waveIn.Dispose();
         _waveIn = null;
     }
     if (_writer != null)
     {
         _writer.Close();
         _writer.Dispose();
         _writer = null;
     }
 }
Beispiel #2
0
        public static async System.Threading.Tasks.Task MicrophoneToFileAsync(string fileName, TimeSpan timeout)
        {
            Console.Write("Listening for " + timeout.TotalSeconds + " seconds ...");
            // create wave input using microphone
            using (NAudio.Wave.WaveInEvent waveIn = new NAudio.Wave.WaveInEvent())
            {
                waveIn.DeviceNumber       = Options.options.audio.NAudio.inputDeviceNumber;
                waveIn.BufferMilliseconds = Options.options.audio.NAudio.waveInBufferMilliseconds;
                waveIn.WaveFormat         = new NAudio.Wave.WaveFormat(Options.options.audio.samplingRate, (int)Options.options.audio.bitDepth, Options.options.audio.channels); // usually only mono (one channel) is supported
                waveIn.DataAvailable     += WaveIn_DataAvailable;                                                                                                                // use event to fill buffer
                using (waveInFile = new NAudio.Wave.WaveFileWriter(Options.options.tempFolderPath + fileName, waveIn.WaveFormat))
                {
                    waveIn.StartRecording();

                    //Console.WriteLine("Hit enter when finished recording.");
                    //Console.ReadKey();
                    System.Threading.Tasks.Task.Delay(timeout).Wait();

                    waveIn.StopRecording();

                    waveInFile.Close();
                }
                Console.WriteLine("");
            }
        }
            private void Export_Signal_Click(object sender, EventArgs e)
            {
                SaveFileDialog SaveWave = new SaveFileDialog();

                SaveWave.Filter = " Wave Audio (*.wav) |*.wav";

                if (SaveWave.ShowDialog() == DialogResult.OK)
                {
                    NAudio.Wave.WaveFileWriter Writer = new NAudio.Wave.WaveFileWriter(SaveWave.FileName, new NAudio.Wave.WaveFormat(44100, 24, result_signals.Length));
                    for (int j = 0; j < result_signals[0].Length; j++)
                    {
                        for (int c = 0; c < result_signals.Length; c++)
                        {
                            if (j > result_signals[0].Length - 1)
                            {
                                Writer.WriteSample(0);
                            }
                            else
                            {
                                Writer.WriteSample((float)result_signals[c][j]);
                            }
                        }
                    }
                    Writer.Close();
                    Writer.Dispose();
                }
            }
Beispiel #4
0
            private void ExportFilter(object sender, EventArgs e)
            {
                SaveFileDialog SaveWave = new SaveFileDialog();

                if (Response.Length < 4)
                {
                    SaveWave.Filter = " Wave Audio (*.wav) |*.wav";
                }
                else
                {
                    SaveWave.Filter = "Extended Wave Audio (*.wavex) |*.wavex";
                }

                if (SaveWave.ShowDialog() == DialogResult.OK)
                {
                    NAudio.Wave.WaveFileWriter Writer = new NAudio.Wave.WaveFileWriter(SaveWave.FileName, new NAudio.Wave.WaveFormat(44100, 24, Response.Length));
                    for (int j = 0; j < Response[0].Length; j++)
                    {
                        for (int c = 0; c < Response.Length; c++)
                        {
                            if (j > Response[c].Length - 1)
                            {
                                Writer.WriteSample(0);
                            }
                            else
                            {
                                Writer.WriteSample((float)Response[c][j]);
                            }
                        }
                    }
                    Writer.Close();
                    Writer.Dispose();
                }
            }
        static void Main(string[] args)
        {
            string songPath = (args.Length == 1) ? args[0] : Console.ReadLine();
            string songName = Path.GetFileNameWithoutExtension(songPath);
            string simPath  = "simOut\\" + songName + "\\";

            string[] filenames = new string[] { simPath + "output" }; // TODO doesn't this mean the following loop should be removed?
            foreach (string e in filenames)
            {
                StreamReader file   = new StreamReader(e + ".txt");
                List <Int16> values = new List <Int16>();
                while (!file.EndOfStream)
                {
                    string   line         = file.ReadLine();
                    string[] stringValues = line.Split(',');
                    for (int i = 0; i < stringValues.Length; i++)
                    {
                        double h;
                        if (double.TryParse(stringValues[i], out h))
                        {
                            values.Add((Int16)Math.Round(32768 * double.Parse(stringValues[i])));
                        }
                    }
                }
                Int16[]                    buffer    = values.ToArray();
                FileStream                 outStream = new FileStream(e + ".wav", FileMode.Create);
                NAudio.Wave.WaveFormat     format    = new NAudio.Wave.WaveFormat(44100, 16, 1);
                NAudio.Wave.WaveFileWriter writer    = new NAudio.Wave.WaveFileWriter(outStream, format);
                writer.WriteSamples(buffer, 0, buffer.Length);
                writer.Close();
            }
            System.IO.File.Copy(filenames[0] + ".wav", "simOut\\" + songName + ".wav", true);
        }
Beispiel #6
0
 public void WriteDataToWav(string WavFilePath)
 {
     if (File.Exists(WavFilePath))
     {
         File.Delete(WavFilePath);
     }
     NAudio.Wave.WaveFileWriter waveFileWriter = new NAudio.Wave.WaveFileWriter(WavFilePath, NAudio.Wave.WaveFormat.CreateIeeeFloatWaveFormat(44100, 2));
     try
     {
         waveFileWriter.WriteSamples(Data, 0, Data.Length);
     }
     finally
     {
         waveFileWriter.Close();
     }
 }
Beispiel #7
0
        static void Main()
        {
            var stream = new NAudio.Wave.WaveFileWriter(@"E:\C1_single.wav", new NAudio.Wave.WaveFormat(44100, 2));
            var writer = new System.IO.BinaryWriter(stream);

            for (int i = 0; i < 44100 * 10; ++i)
            {
                short value = (short)(Math.Cos(2 * Math.PI * 261 * i / 44100) * 16384); // Not too loud

                writer.Write(value);
                writer.Write(value);
            }
            writer.Flush();
            stream.Flush();
            stream.Close();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormFourier());
        }
Beispiel #8
0
 public void Stop()
 {
     recording = false;
     capture.StopRecording();
     waveWriter.Close();
 }
Beispiel #9
0
            private void RenderBtn_Click(object sender, System.EventArgs e)
            {
                if (Response == null || Response.Length == 0)
                {
                    Rhino.RhinoApp.WriteLine("No impulse response found to render...");
                    return;
                }

                int SamplesPerSec;

                double[] SignalBuffer;
                OpenWaveFile(out SamplesPerSec, out SignalBuffer);

                float maxvalue = 0;

                //Normalize input signal...
                for (int j = 0; j < SignalBuffer.Length; j++)
                {
                    maxvalue = (float)Math.Max(maxvalue, Math.Abs(SignalBuffer[j]));
                }
                for (int j = 0; j < SignalBuffer.Length; j++)
                {
                    SignalBuffer[j] /= maxvalue;
                }
                //Convert pressure response to a 24-bit dynamic range:
                //double mod24 = Math.Pow(10, -50 / 10);
                //for (int i = 0; i < Response.Length; i++) for(int j = 0; j < Response[i].Length; j++) Response[i][j] *= mod24;

                float[][] NewSignal = new float[(int)Response.Length][];
                for (int i = 0; i < Response.Length; i++)
                {
                    NewSignal[i] = Pach_SP.FFT_Convolution(SignalBuffer, Response[i], 0);
                    for (int j = 0; j < NewSignal[i].Length; j++)
                    {
                        NewSignal[i][j] *= 1E-5f;
                    }
                }

                SrcRendered = new int[SourceList.CheckedIndices.Count];
                for (int j = 0; j < SourceList.CheckedIndices.Count; j++)
                {
                    SrcRendered[j] = SourceList.CheckedIndices[j];
                }
                RecRendered    = int.Parse(Receiver_Choice.Text);
                SFreq_Rendered = SamplesPerSec;

                SaveFileDialog SaveWave = new SaveFileDialog();

                if (NewSignal.Length < 4)
                {
                    SaveWave.Filter = " Wave Audio (*.wav) |*.wav";
                }
                else
                {
                    SaveWave.Filter = "Extended Wave Audio (*.wavex) |*.wavex";
                }

                if (SaveWave.ShowDialog() == DialogResult.OK)
                {
                    if (Response == null || Response.Length == 0)
                    {
                        Rhino.RhinoApp.WriteLine("No impulse response found to render...");
                        return;
                    }
                    NAudio.Wave.WaveFileWriter Writer = new NAudio.Wave.WaveFileWriter(SaveWave.FileName, new NAudio.Wave.WaveFormat(SamplesPerSec, 24, NewSignal.Length));

                    for (int j = 0; j < NewSignal[0].Length; j++)
                    {
                        for (int i = 0; i < Channel_View.Items.Count; i++)
                        {
                            Writer.WriteSample(NewSignal[i][j]);
                        }
                    }
                    Writer.Close();
                    Writer.Dispose();
                    System.Media.SoundPlayer Player = new System.Media.SoundPlayer(SaveWave.FileName);
                    Player.Play();
                }
            }
            private void saveVRSpectraToolStripMenuItem_Click(object sender, EventArgs e)
            {
                SaveFileDialog SaveWave = new SaveFileDialog();
                SaveWave.Filter = " Pachyderm VR (*.pacvr) |*.pacvr";
                if (SaveWave.ShowDialog() != DialogResult.OK) return;

                System.IO.StreamWriter sw = new System.IO.StreamWriter(System.IO.File.Open(SaveWave.FileName, System.IO.FileMode.Create));

                //A new standard...
                //1. Write pachyderm version
                sw.Write(PachydermAc_PlugIn.Instance.Version);

                double[] dresponse = Pach_SP.Expand_Response(Direct_Data, IS_Data, Receiver, (double)CO_TIME.Value / 1000, SampleRate, 0, new List<int>{0}, 24);//RecID, SrcIDs,
                float[] Response = new float[dresponse.Length];

                for (int i = 0; i < dresponse.Length; i++)
                {
                    Response[i] = (float)dresponse[i];
                }

                NAudio.Wave.WaveFileWriter Writer = new NAudio.Wave.WaveFileWriter(SaveWave.FileName, new NAudio.Wave.WaveFormat(44100, 24, 1));//, 44100, 1, 16
                for (int j = 0; j < Response.Length; j++)
                {
                    Writer.WriteSample(Response[j]);
                }
                Writer.Close();
                Writer.Dispose();
            }
            public void Save_IR(List<int> SrcIDs, int RecID, string Path)
            {
                double[] dresponse = Pach_SP.Expand_Response(Direct_Data, IS_Data, Receiver, (double)CO_TIME.Value / 1000, SampleRate, RecID, SrcIDs, 24);
                float[] Response = new float[dresponse.Length];

                for (int i = 0; i < dresponse.Length; i++)
                {
                    Response[i] = (float)dresponse[i];
                }

                NAudio.Wave.WaveFileWriter Writer = new NAudio.Wave.WaveFileWriter(Path, new NAudio.Wave.WaveFormat(44100, 24, 1));
                for (int j = 0; j < Response.Length; j++)
                {
                    Writer.WriteSample(Response[j]);
                }
                Writer.Close();
                Writer.Dispose();
            }
            private void RenderBtn_Click(object sender, System.EventArgs e)
            {
                if ((SelectedSources().Count < 1) || ((string)Receiver_Choice.Text == "No Results Calculated..."))
                {
                    Rhino.RhinoApp.WriteLine("Select Source and Receiver objects to render");
                    return;
                }

                double[] SignalBuffer;
                int SamplesPerSec;
                this.OpenWaveFile(out SamplesPerSec, out SignalBuffer);
                if (!IsRendered())
                {
                    Response = Pach_SP.Expand_Response(Direct_Data, IS_Data, Receiver, (double)(CO_TIME.Value / 1000), SamplesPerSec, int.Parse(Receiver_Choice.Text), SelectedSources(), 24);
                    SrcRendered = new int[SourceList.CheckedIndices.Count];
                    for(int i = 0 ; i < SourceList.CheckedIndices.Count; i++)
                    {
                        SrcRendered[i] = SourceList.CheckedIndices[i];
                    }
                    RecRendered = int.Parse(Receiver_Choice.Text);
                }
                float[] NewSignal = Pach_SP.FFT_Convolution(SignalBuffer, Response);

                SaveFileDialog SaveWave = new SaveFileDialog();
                SaveWave.Filter = " Wave Audio (*.wav) |*.wav";

                if (SaveWave.ShowDialog() == DialogResult.OK)
                {
                    NAudio.Wave.WaveFileWriter Writer = new NAudio.Wave.WaveFileWriter(SaveWave.FileName, new NAudio.Wave.WaveFormat(SamplesPerSec, 24, 1));
                    
                    for (int j = 0; j < NewSignal.Length; j++)
                    {
                        Writer.WriteSample(NewSignal[j]);
                    }
                    Writer.Close();
                    Writer.Dispose();
                    System.Media.SoundPlayer Player = new System.Media.SoundPlayer(SaveWave.FileName);
                    Player.Play();
                }
            }
            private void Export_Signal_Click(object sender, EventArgs e)
            {
                SaveFileDialog SaveWave = new SaveFileDialog();

                SaveWave.Filter = " Wave Audio (*.wav) |*.wav";

                if (SaveWave.ShowDialog() == DialogResult.OK)
                {
                    NAudio.Wave.WaveFileWriter Writer = new NAudio.Wave.WaveFileWriter(SaveWave.FileName, new NAudio.Wave.WaveFormat(44100, 24, result_signals.Length));
                    for (int j = 0; j < result_signals[0].Length; j++)
                    {
                        for (int c = 0; c < result_signals.Length; c++) if (j > result_signals[0].Length - 1) Writer.WriteSample(0); else Writer.WriteSample((float)result_signals[c][j]);
                    }
                    Writer.Close();
                    Writer.Dispose();
                }
            }
            private void ExportFilter(object sender, EventArgs e)
            {
                SaveFileDialog SaveWave = new SaveFileDialog();
                if (Response.Length < 4)
                {
                    SaveWave.Filter = " Wave Audio (*.wav) |*.wav";
                }
                else 
                {
                    SaveWave.Filter = "Extended Wave Audio (*.wavex) |*.wavex";
                }

                if (SaveWave.ShowDialog() == DialogResult.OK)
                {
                    NAudio.Wave.WaveFileWriter Writer = new NAudio.Wave.WaveFileWriter(SaveWave.FileName, new NAudio.Wave.WaveFormat(44100, 24, Response.Length));
                    for (int j = 0; j < Response[0].Length; j++)
                    {
                        for (int c = 0; c < Response.Length; c++) if (j > Response[c].Length - 1) Writer.WriteSample(0); else Writer.WriteSample((float)Response[c][j]);
                    }
                    Writer.Close();
                    Writer.Dispose();
                }
            }
            private void RenderBtn_Click(object sender, System.EventArgs e)
            {
                if (Response == null || Response.Length == 0)
                {
                    Rhino.RhinoApp.WriteLine("No impulse response found to render...");
                    return;
                }

                int SamplesPerSec;
                double[] SignalBuffer;
                OpenWaveFile(out SamplesPerSec, out SignalBuffer);

                float maxvalue = 0;
                //Normalize input signal...
                for (int j = 0; j < SignalBuffer.Length; j++) maxvalue = (float)Math.Max(maxvalue, Math.Abs(SignalBuffer[j]));
                for (int j = 0; j < SignalBuffer.Length; j++) SignalBuffer[j] /= maxvalue;
                //Convert pressure response to a 24-bit dynamic range:
                //double mod24 = Math.Pow(10, -50 / 10);
                //for (int i = 0; i < Response.Length; i++) for(int j = 0; j < Response[i].Length; j++) Response[i][j] *= mod24;

                float[][] NewSignal = new float[(int)Response.Length][];
                for (int i = 0; i < Response.Length; i++)
                {
                    NewSignal[i] = Pach_SP.FFT_Convolution(SignalBuffer, Response[i], 0);
                    for (int j = 0; j < NewSignal[i].Length; j++) NewSignal[i][j] *= 1E-5f;
                }

                SrcRendered = new int[SourceList.CheckedIndices.Count];
                for (int j = 0; j < SourceList.CheckedIndices.Count; j++)
                {
                    SrcRendered[j] = SourceList.CheckedIndices[j];
                }
                RecRendered = int.Parse(Receiver_Choice.Text);
                SFreq_Rendered = SamplesPerSec;

                SaveFileDialog SaveWave = new SaveFileDialog();
                if (NewSignal.Length < 4)
                {
                    SaveWave.Filter = " Wave Audio (*.wav) |*.wav";
                }
                else 
                {
                    SaveWave.Filter = "Extended Wave Audio (*.wavex) |*.wavex";
                }

                if (SaveWave.ShowDialog() == DialogResult.OK)
                {
                    if (Response == null || Response.Length == 0)
                    {
                        Rhino.RhinoApp.WriteLine("No impulse response found to render...");
                        return;
                    }
                    NAudio.Wave.WaveFileWriter Writer = new NAudio.Wave.WaveFileWriter(SaveWave.FileName, new NAudio.Wave.WaveFormat(SamplesPerSec, 24, NewSignal.Length));

                    for (int j = 0; j < NewSignal[0].Length; j++)
                    {
                        for (int i = 0; i < Channel_View.Items.Count; i++) Writer.WriteSample(NewSignal[i][j]);
                    }
                    Writer.Close();
                    Writer.Dispose();
                    System.Media.SoundPlayer Player = new System.Media.SoundPlayer(SaveWave.FileName);
                    Player.Play();
                }
            }