Beispiel #1
0
        private byte[] RealMix(ReceivedRtp item1, ReceivedRtp item2)
        {
            if (item1 == null || item2 == null)
            {
                return(null);
            }

            if (item1.size == 0 || item2.size == 0)
            {
                return(null);
            }

            byte[] wavSrc1 = new byte[item1.size - headersize];
            byte[] wavSrc2 = new byte[item2.size - headersize];

            Array.Copy(item1.buff, headersize, wavSrc1, 0, (item1.size - headersize));
            Array.Copy(item2.buff, headersize, wavSrc2, 0, (item2.size - headersize));

            WaveMixerStream32 mixer = new WaveMixerStream32();
            // mixer.AutoStop = true;
            MemoryStream               memstrem         = new MemoryStream(wavSrc1);
            RawSourceWaveStream        rawsrcstream     = new RawSourceWaveStream(memstrem, this.codec);
            WaveFormatConversionStream conversionstream = new WaveFormatConversionStream(pcmFormat16, rawsrcstream);
            WaveChannel32              channelstream    = new WaveChannel32(conversionstream);

            mixer.AddInputStream(channelstream);

            memstrem         = new MemoryStream(wavSrc2);
            rawsrcstream     = new RawSourceWaveStream(memstrem, this.codec);
            conversionstream = new WaveFormatConversionStream(pcmFormat16, rawsrcstream);
            channelstream    = new WaveChannel32(conversionstream);
            mixer.AddInputStream(channelstream);
            mixer.Position = 0;

            Wave32To16Stream to16 = new Wave32To16Stream(mixer);
            var convStm           = new WaveFormatConversionStream(pcmFormat8, to16);

            byte[] mixedbytes = new byte[(int)convStm.Length];
            int    chk        = convStm.Read(mixedbytes, 0, (int)convStm.Length);

            //Buffer.BlockCopy(tobyte, 0, writingBuffer, 0, tobyte.Length);

            memstrem.Close();
            rawsrcstream.Close();
            conversionstream.Close();
            channelstream.Close();

            convStm.Close(); convStm.Dispose(); convStm = null;
            to16.Close(); to16.Dispose(); to16          = null;
            mixer.Close(); mixer.Dispose(); mixer       = null;

            return(mixedbytes);
        }
Beispiel #2
0
        void Wav2CWAV(string input)
        {
            SaveFileDialog sv = new SaveFileDialog();

            sv.Filter = "CTR Wave (*.bcwav)|*.bcwav";
            sv.Title  = "Save the CWAV file";
            if (sv.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (!APP_not_Optimize_Cwavs)
                {
                    Debug.Print("Optimizing CWAV: " + Path.GetTempPath() + Path.GetFileName(input) + ".tmp.wav");
                    WaveFormat New      = new WaveFormat(APP_opt_samples, 8, 1);
                    WaveStream Original = new WaveFileReader(input);
                    WaveFormatConversionStream stream = new WaveFormatConversionStream(New, Original);
                    if (System.IO.File.Exists(Path.GetTempPath() + Path.GetFileName(input) + ".tmp.wav"))
                    {
                        File.Delete(Path.GetTempPath() + Path.GetFileName(input) + ".tmp.wav");
                    }
                    WaveFileWriter.CreateWaveFile(Path.GetTempPath() + Path.GetFileName(input) + ".tmp.wav", stream);
                    stream.Dispose();
                    Original.Dispose();
                }
                Process prc = new Process();
                prc.StartInfo.FileName = @"Plugins\ctr_WaveConverter32\ctr_WaveConverter32.exe";
                if (!APP_not_Optimize_Cwavs)
                {
                    prc.StartInfo.Arguments = "-o \"" + sv.FileName + "\" \"" + Path.GetTempPath() + Path.GetFileName(input) + ".tmp.wav\"";
                }
                else
                {
                    prc.StartInfo.Arguments = "-o \"" + sv.FileName + "\" \"" + input + "\"";
                }
                prc.Start();
                prc.WaitForExit();
                if (System.IO.File.Exists(Path.GetTempPath() + Path.GetFileName(input) + ".tmp.wav"))
                {
                    File.Delete(Path.GetTempPath() + Path.GetFileName(input) + ".tmp.wav");
                }
                if (File.Exists(sv.FileName))
                {
                    MessageBox.Show("Convert Completed");
                }
                else
                {
                    MessageBox.Show("An Error while converting the file, run the command in the cmd to check the output");
                }
            }
        }
Beispiel #3
0
 public void Stop()
 {
     try
     {
         if (mBufferedStream != null)
         {
             mBufferedStream.Dispose();
             mBufferedStream = null;
         }
         if (mConvStream != null)
         {
             mConvStream.Dispose();
             mConvStream = null;
         }
     }
     catch (Exception ex)
     {
         SubDebug(string.Format("Converter stop fail.\t{0}", ex.Message));
     }
 }
Beispiel #4
0
        private void AudioSourceDataAvailable(object sender, DataAvailableEventArgs e)
        {
            try
            {
                lock (_obj)
                {
                    if (_bTalking && _avstream != null)
                    {
                        byte[] bSrc     = e.RawData;
                        int    totBytes = bSrc.Length;

                        if (!_audioSource.RecordingFormat.Equals(_waveFormat))
                        {
                            var ws      = new TalkHelperStream(bSrc, totBytes, _audioSource.RecordingFormat);
                            var helpStm = new WaveFormatConversionStream(_waveFormat, ws);
                            totBytes = helpStm.Read(bSrc, 0, 25000);
                            ws.Close();
                            ws.Dispose();
                            helpStm.Close();
                            helpStm.Dispose();
                        }
                        var enc = new byte[totBytes / 2];
                        ALawEncoder.ALawEncode(bSrc, totBytes, enc);

                        try {
                            _avstream.Write(enc, 0, enc.Length);
                            _avstream.Flush();
                        }
                        catch (SocketException)
                        {
                            StopTalk();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("", ex);//MainForm.LogExceptionToFile(ex);
                StopTalk();
            }
        }
Beispiel #5
0
        public static bool ChangeWavFormat(string wavFile, WaveFormat wavFormat)
        {
            try
            {
                string tempFile = Path.GetTempFileName() + ".wav";
                File.Move(wavFile, tempFile);

                using (var reader = new WaveFileReader(tempFile))
                    using (var conversionStream = new WaveFormatConversionStream(wavFormat, reader))
                    {
                        WaveFileWriter.CreateWaveFile(wavFile, conversionStream);
                        conversionStream.Dispose();
                    }

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Beispiel #6
0
        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Wave != null)
            {
                Wave.Close();
                Wave.Dispose();
                Wave = null;
            }

            if (Recorder != null)
            {
                Recorder.StopRecording();
                Recorder = null;
            }

            SetupChart();

            processToolStripMenuItem.Enabled = false;
            startToolStripMenuItem.Enabled   = true;
            stopToolStripMenuItem.Enabled    = false;
            openToolStripMenuItem.Enabled    = true;
            clearToolStripMenuItem.Enabled   = false;
            saveToolStripMenuItem.Enabled    = false;
        }
Beispiel #7
0
 private void menuItem12_Click(object sender, EventArgs e)
 {
     //I use to some of the YATA-PLUS's code:https://github.com/exelix11/YATA-PLUS/edit/master/Form1.cs
     st.StartInfo.FileName = @"Plugins\ctr_WaveConverter32\ctr_WaveConverter32.exe";
     try
     {
         OpenFileDialog opn = new OpenFileDialog();
         opn.Filter      = "Wave File (*.wav)|*.wav";
         opn.Title       = "Select a WAV file";
         opn.Multiselect = true;
         if (opn.ShowDialog() == System.Windows.Forms.DialogResult.OK)
         {
             if (opn.FileNames.Length == 1)
             {
                 Wav2CWAV(opn.FileName);
             }
             else
             {
                 int error = 0;
                 for (int i = 0; i < opn.FileNames.Length; i++)
                 {
                     if (!APP_not_Optimize_Cwavs)
                     {
                         Debug.Print("Optimizing CWAV: " + Path.GetTempPath() + Path.GetFileName(opn.FileNames[i]) + ".tmp.wav");
                         WaveFormat New      = new WaveFormat(APP_opt_samples, 8, 1);
                         WaveStream Original = new WaveFileReader(opn.FileNames[i]);
                         WaveFormatConversionStream stream = new WaveFormatConversionStream(New, Original);
                         if (System.IO.File.Exists(Path.GetTempPath() + Path.GetFileName(opn.FileNames[i]) + ".tmp.wav"))
                         {
                             File.Delete(Path.GetTempPath() + Path.GetFileName(opn.FileNames[i]) + ".tmp.wav");
                         }
                         WaveFileWriter.CreateWaveFile(Path.GetTempPath() + Path.GetFileName(opn.FileNames[i]) + ".tmp.wav", stream);
                         stream.Dispose();
                         Original.Dispose();
                     }
                     Process prc = new Process();
                     prc.StartInfo.FileName = @"Plugins\ctr_WaveConverter32\ctr_WaveConverter32.exe";
                     if (!APP_not_Optimize_Cwavs)
                     {
                         prc.StartInfo.Arguments = "-o \"" + opn.FileNames[i] + ".bcwav\" \"" + Path.GetTempPath() + Path.GetFileName(opn.FileNames[i]) + ".tmp.wav\"";
                     }
                     else
                     {
                         prc.StartInfo.Arguments = "-o \"" + opn.FileNames[i] + ".bcwav\" \"" + opn.FileNames[i] + "\"";
                     }
                     Debug.Print("Converting CWAV: " + Path.GetTempPath() + Path.GetFileName(opn.FileNames[i]) + ".tmp.wav");
                     prc.Start();
                     prc.WaitForExit();
                     if (System.IO.File.Exists(Path.GetTempPath() + Path.GetFileName(opn.FileNames[i]) + ".tmp.wav"))
                     {
                         File.Delete(Path.GetTempPath() + Path.GetFileName(opn.FileNames[i]) + ".tmp.wav");
                     }
                     if (!File.Exists(opn.FileNames[i] + ".bcwav"))
                     {
                         error++;
                     }
                 }
                 if (error != 0)
                 {
                     MessageBox.Show("Convert Completed");
                 }
                 else
                 {
                     MessageBox.Show("Batch Convert Completed");
                 }
             }
         }
     }
     catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); }
 }
Beispiel #8
0
        public static void Convert(PlayItem item, FileInfo wavFi)
        {
            if (wavFi == null)
            {
                throw new ArgumentNullException(nameof(wavFi));
            }
            var    convertFormat = SettingsManager.Options.CacheAudioFormat;
            var    fileName      = Path.GetFileNameWithoutExtension(wavFi.FullName);
            string fullName;

            if (convertFormat == CacheFileFormat.ULaw || convertFormat == CacheFileFormat.ALaw)
            {
                fullName = Path.Combine(wavFi.Directory.FullName, fileName + "." + convertFormat.ToString().ToLower() + ".wav");
            }
            else if (convertFormat == CacheFileFormat.MP3)
            {
                fullName = Path.Combine(wavFi.Directory.FullName, fileName + ".copy.mp3");
            }
            else if (convertFormat == CacheFileFormat.WAV)
            {
                fullName = Path.Combine(wavFi.Directory.FullName, fileName + ".copy.wav");
            }
            else
            {
                // Do nothing if format do not match.
                return;
            }
            // Exit if file already exists.
            if (File.Exists(fullName))
            {
                return;
            }
            // Write whole WAV (head and data) into one stream.
            var source = new MemoryStream();

            Write(item, source);
            source.Position = 0;
            // Create directory if not exists.
            if (!wavFi.Directory.Exists)
            {
                wavFi.Directory.Create();
            }
            // https://www.codeproject.com/Articles/501521/How-to-convert-between-most-audio-formats-in-NET
            source.Position = 0;
            var reader = new WaveFileReader(source);

            if (convertFormat == CacheFileFormat.ULaw || convertFormat == CacheFileFormat.ALaw)
            {
                // The ACM mu-law encoder expects its input to be 16 bit.
                // If you're working with mu or a-law, the sample rate is likely to be low as well.
                // The following two lines of code will create a zero-length stream of PCM 16 bit and
                // pass it into a WaveFormatConversionStream to convert it to a-law.
                // It should not throw a "conversion not possible" error unless for some reason you don't have the G.711 encoder installed on your machine.
                var wavFormat = convertFormat == CacheFileFormat.ULaw
                                                ? WaveFormatEncoding.MuLaw
                                                : WaveFormatEncoding.ALaw;
                var destinationFormat = WaveFormat.CreateCustomFormat(
                    wavFormat,
                    SettingsManager.Options.CacheAudioSampleRate,
                    (int)SettingsManager.Options.CacheAudioChannels,
                    SettingsManager.Options.CacheAudioAverageBitsPerSecond / 8,
                    SettingsManager.Options.CacheAudioBlockAlign,
                    SettingsManager.Options.CacheAudioBitsPerSample
                    );
                var conversionStream1 = new WaveFormatConversionStream(new WaveFormat(destinationFormat.SampleRate, 16, destinationFormat.Channels), reader);
                using (var conversionStream2 = new WaveFormatConversionStream(destinationFormat, conversionStream1))
                    WaveFileWriter.CreateWaveFile(fullName, conversionStream2);
                conversionStream1.Dispose();
            }
            else if (convertFormat == CacheFileFormat.MP3)
            {
                // If media foundation is not started then you will get this exception during MP3 encoding:
                // System.Runtime.InteropServices.COMException:
                // 'The request is invalid because Shutdown() has been called. (Exception from HRESULT: 0xC00D3E85)'
                MediaFoundationApi.Startup();
                MediaFoundationEncoder.EncodeToMp3(reader, fullName, SettingsManager.Options.CacheAudioAverageBitsPerSecond);
                MediaFoundationApi.Shutdown();
            }
            else if (convertFormat == CacheFileFormat.WAV)
            {
                var destWav = new WaveFormat(
                    SettingsManager.Options.CacheAudioSampleRate,
                    SettingsManager.Options.CacheAudioBitsPerSample,
                    (int)SettingsManager.Options.CacheAudioChannels
                    );
                using (var conversionStream2 = new WaveFormatConversionStream(destWav, reader))
                    WaveFileWriter.CreateWaveFile(fullName, conversionStream2);
            }
            reader.Dispose();
        }
Beispiel #9
0
        private void AudioSourceDataAvailable(object sender, DataAvailableEventArgs e)
        {
            try
            {
                lock (_obj)
                {
                    if (_bTalking && _avstream != null)
                    {
                        byte[] bSrc     = e.RawData;
                        int    totBytes = bSrc.Length;

                        if (!_audioSource.RecordingFormat.Equals(_waveFormat))
                        {
                            var ws      = new TalkHelperStream(bSrc, totBytes, _audioSource.RecordingFormat);
                            var helpStm = new WaveFormatConversionStream(_waveFormat, ws);
                            totBytes = helpStm.Read(bSrc, 0, 25000);

                            ws.Close();
                            ws.Dispose();
                            helpStm.Close();
                            helpStm.Dispose();
                        }

                        if (_needsencodeinit)
                        {
                            _enc.EncodeInit(BitConverter.ToInt16(e.RawData, 0), BitConverter.ToInt16(e.RawData, 2));
                            _needsencodeinit = false;
                        }

                        var buff = new byte[25000];
                        int c;
                        unsafe
                        {
                            fixed(byte *src = bSrc)
                            {
                                fixed(byte *dst = buff)
                                {
                                    c = (int)_enc.EncodeFoscam(src, totBytes, dst);
                                }
                            }
                        }
                        Buffer.BlockCopy(buff, 0, _talkBuffer, _talkDatalen, c);
                        _talkDatalen += c;

                        var dtms = (int)(DateTime.Now - _dt).TotalMilliseconds;
                        int i    = 0;
                        int j    = 0;
                        try
                        {
                            while (j + 160 < _talkDatalen)
                            {
                                //need to write out in 160 byte packets for 40ms
                                byte[] cmd = SInit(TalkData, MoIPAvFlag);

                                cmd = AddNext(cmd, dtms + (i * 40));
                                cmd = AddNext(cmd, _seq);
                                cmd = AddNext(cmd, (int)(DateTime.Now - _dt).TotalSeconds);
                                cmd = AddNext(cmd, (byte)0x0);
                                cmd = AddNext(cmd, 160);

                                var pkt = new byte[160];
                                Buffer.BlockCopy(_talkBuffer, j, pkt, 0, 160);
                                cmd = AddNext(cmd, pkt, 160);
                                Encode(ref cmd);

                                _avstream.Write(cmd, 0, cmd.Length);
                                j += 160;
                                _seq++;
                                i++;
                            }
                            if (j < _talkDatalen)
                            {
                                Buffer.BlockCopy(_talkBuffer, j, _talkBuffer, 0, _talkDatalen - j);
                                _talkDatalen = _talkDatalen - j;
                            }
                        }
                        catch (SocketException)
                        {
                            StopTalk(true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("", ex);//MainForm.LogExceptionToFile(ex);
                StopTalk(true);
            }
        }
Beispiel #10
0
 public void Dispose()
 {
     ulawStm?.Dispose();
     converter?.Dispose();
     alawFile?.Dispose();
 }