Ejemplo n.º 1
0
        public AudioRecorder(string streamSourceName, Common.SignalRecordingType recordingType, int recordingKickTime, WaveFormat sourceWaveFormat, WaveFormat fileWaveFormat, bool recordingEnabled)
        {
            _recordingType = recordingType;
            switch (_recordingType)
            {
                case SignalRecordingType.Fixed:
                    {
                        RecordingKickTimeTicks = TimeSpan.FromMinutes(recordingKickTime).Ticks;
                        break;
                    }
                default:
                    {
                        RecordingKickTimeTicks = TimeSpan.FromSeconds(recordingKickTime).Ticks;
                        break;
                    }
            }
            RecordingPrefix = RadioSignalLogger.MakeSourceFilePrefix(streamSourceName);
            _sourceWaveFormat = sourceWaveFormat;
            if (fileWaveFormat == null)
                _fileWaveFormat = sourceWaveFormat;
            else
                _fileWaveFormat = fileWaveFormat;
            _recordingEnabled = recordingEnabled;

            if (_sourceWaveFormat.Equals(_fileWaveFormat))
            {
                _resampleStream = null;
                _useResampler = false;
            }
            else
            {
                _resampleStream = new NAudio.Wave.Compression.AcmStream(_sourceWaveFormat, _fileWaveFormat);
                _useResampler = true;
            }
        }
Ejemplo n.º 2
0
 internal WasapiLoopbackRecorder(INetworkChatCodec c)
 {
     deviceNumber = -1;
     codec = c;
     waveIn = new WasapiLoopbackCapture();
     convertionStream = new AcmStream(new WaveFormat(waveIn.WaveFormat.SampleRate, 16, waveIn.WaveFormat.Channels), codec.RecordFormat);
 }
Ejemplo n.º 3
0
    // Trigger this with keystroke, VR input, UI button, etc.
    // Don't do this every time the person starts/stops sending data, instead,
    // use this method when the game starts up, or when you want to make the mic
    // available. Audio won't actually go through unless the code in Update()
    // can successfully give the networking system an AudioPacket. Interrupt
    // it before it delivers the packet, and no data will be sent. VoipReceivers
    // on the other side will intelligently interpret this as packet loss and
    // wait for new data.
    public void StartTransmitting()
    {
        if (isTransmitting)
        {
            return;
        }
        Debug.Log("Voip Test Transmit");
        Debug.Log("Beginning VOIP transmit");
        lastSamplePosition = 0;
        lastSampleTime     = 0;


        int    maxFreq    = 0;
        int    minFreq    = 0;
        string deviceName = Microphone.devices[0];

        Microphone.GetDeviceCaps(deviceName, out minFreq, out maxFreq);

        //Record at lowest frequency possible.
        //This makes the downsampling process a little less painful.
        SOURCE_FREQUENCY = minFreq;
        DEVICE           = deviceName;
        lastClip         = Microphone.Start(deviceName, true, 60, SOURCE_FREQUENCY);
        Debug.Log("Mic name: " + deviceName + ", freq: " + SOURCE_FREQUENCY);

        downsampler   = new NAudio.Wave.Compression.AcmStream(new NAudio.Wave.WaveFormat(SOURCE_FREQUENCY, 16, 1), new NAudio.Wave.WaveFormat(TRANSMIT_FREQUENCY, 16, 1));
        bitreduce     = new NAudio.Wave.Compression.AcmStream(new NAudio.Wave.WaveFormat(TRANSMIT_FREQUENCY, 16, 1), new NAudio.Wave.WaveFormat(TRANSMIT_FREQUENCY, 8, 1));
        lowPassFilter = NAudio.Dsp.BiQuadFilter.LowPassFilter(SOURCE_FREQUENCY, TRANSMIT_FREQUENCY * 0.5f, lowPassQuality);


        Debug.Log("Channels: " + lastClip.channels);
        Debug.Log("Bitconvert is little endian? " + BitConverter.IsLittleEndian);
        isTransmitting = true;
    }
Ejemplo n.º 4
0
 public void Dispose()
 {
     if (this.conversionStream != null)
     {
         this.conversionStream.Dispose();
         this.conversionStream = null;
     }
 }
Ejemplo n.º 5
0
 public byte[] Decode(byte[] data, int offset, int length)
 {
     if (this.decodeStream == null)
     {
         this.decodeStream = new AcmStream(this.encodeFormat, this.RecordFormat);
     }
     return Convert(decodeStream, data, offset, length, ref decodeSourceBytesLeftovers);
 }
Ejemplo n.º 6
0
 public byte[] Decode(byte[] data, int offset, int length)
 {
     if (_decodeStream == null)
     {
         _decodeStream = new AcmStream(_encodeFormat, RecordFormat);
     }
     return Convert(_decodeStream, data, offset, length, ref _decodeSourceBytesLeftovers);
 }
Ejemplo n.º 7
0
		public void Dispose()
		{
			if (conversionStream != null)
			{
				conversionStream.Dispose();
				conversionStream = null;
			}
		}
Ejemplo n.º 8
0
 public byte[] Decode(byte[] data, int offset, int length)
 {
     if (this.decodeStream == null)
     {
         this.decodeStream = new AcmStream(this.encodeFormat, this.RecordFormat);
     }
     //Debug.WriteLine(String.Format("Decoding {0} + {1} bytes", data.Length, decodeSourceBytesLeftovers));
     return Convert(decodeStream, data, offset, length, ref decodeSourceBytesLeftovers);
 }
Ejemplo n.º 9
0
 public byte[] Encode(byte[] data, int offset, int length)
 {
     if (encodeStream == null)
     {
         encodeStream = new AcmStream(RecordFormat, encodeFormat);
     }
     //Debug.WriteLine(String.Format("Encoding {0} + {1} bytes", length, encodeSourceBytesLeftovers));
     return Convert(encodeStream, data, offset, length, ref encodeSourceBytesLeftovers);
 }
        /// <summary>
        /// Create a new WaveFormat conversion stream
        /// </summary>
        /// <param name="targetFormat">Desired output format</param>
        /// <param name="sourceProvider">Source Provider</param>
        public WaveFormatConversionProvider(WaveFormat targetFormat, IWaveProvider sourceProvider)
        {
            this.sourceProvider = sourceProvider;
            this.targetFormat = targetFormat;

            conversionStream = new AcmStream(sourceProvider.WaveFormat, targetFormat);

            preferredSourceReadSize = Math.Min(sourceProvider.WaveFormat.AverageBytesPerSecond, conversionStream.SourceBuffer.Length);
            preferredSourceReadSize -= (preferredSourceReadSize% sourceProvider.WaveFormat.BlockAlign);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Create a new WaveFormat conversion stream
        /// </summary>
        /// <param name="targetFormat">Desired output format</param>
        /// <param name="sourceStream">Source stream</param>
        public WaveFormatConversionStream(WaveFormat targetFormat, WaveStream sourceStream)
        {
            this.sourceStream = sourceStream;
            this.targetFormat = targetFormat;

            conversionStream = new AcmStream(sourceStream.WaveFormat, targetFormat);
            // work out how many bytes the entire input stream will convert to
            length = SourceToDest((int)sourceStream.Length);
            blockAlign = SourceToDest(sourceStream.BlockAlign);
            position = 0;
        }
Ejemplo n.º 12
0
 public void Dispose()
 {
     if (encodeStream != null)
     {
         encodeStream.Dispose();
         encodeStream = null;
     }
     if (decodeStream != null)
     {
         decodeStream.Dispose();
         decodeStream = null;
     }
 }
Ejemplo n.º 13
0
 public void Dispose()
 {
     if (_encodeStream != null)
     {
         _encodeStream.Dispose();
         _encodeStream = null;
     }
     if (_decodeStream != null)
     {
         _decodeStream.Dispose();
         _decodeStream = null;
     }
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Creates a new ACM frame decompressor
 /// </summary>
 /// <param name="sourceFormat">The MP3 source format</param>
 public AcmMp3FrameDecompressor(WaveFormat sourceFormat)
 {
     this.pcmFormat = AcmStream.SuggestPcmFormat(sourceFormat);
     try
     {
         conversionStream = new AcmStream(sourceFormat, pcmFormat);
     }
     catch (Exception)
     {
         disposed = true;
         GC.SuppressFinalize(this);
         throw;
     }
 }
Ejemplo n.º 15
0
        public ProcessorWaveProvider(string sourceName, IWaveProvider sourceWaveProvider, string waveFilePath, WaveFormat outFormat, Common.ProcessRadioSignalingItemDelegate sigDelegate, Action<bool> hasPropertyChanged, bool recordEnabled, Common.SignalRecordingType recordType, int recordKickTime, Common.NoiseFloor noiseFloor, int customNoiseFloor,bool removeNoise, bool decodeMDC1200, bool decodeGEStar, bool decodeFleetSync, bool decodeP25)
            : base(sourceWaveProvider, waveFilePath)
        {
            LastValidStreamTitle = string.Empty;
            _sourceName = sourceName;
            _sourceFormat = sourceWaveProvider.WaveFormat;
            _outFormat = outFormat;
            _hasPropertyChanged = hasPropertyChanged;

            _silenceHelper = new SilenceHelper(outFormat.AverageBytesPerSecond / (outFormat.BitsPerSample / 8), noiseFloor, removeNoise, customNoiseFloor);

            if (outFormat.Equals(sourceWaveProvider.WaveFormat))
            {
                _resampleStream = null;
                _useResampler = false;
            }
            else
            {
                if (Common.AppSettings.Instance.DiagnosticMode)
                {
                    Common.ConsoleHelper.ColorWriteLine(ConsoleColor.Magenta, "{0}: Source Format <> Out Format [{1}] <> [{2}]", sourceName, sourceWaveProvider.WaveFormat, outFormat);
                }
                _resampleStream = new NAudio.Wave.Compression.AcmStream(sourceWaveProvider.WaveFormat, outFormat);
                _useResampler = true;
            }
            if (decodeMDC1200)
            {
                _mdc = new Decoders.MDC1200(outFormat.SampleRate, ProcessMDC1200, sourceName);
            }
            else
            {
                _mdc = null;
            }
            if (decodeGEStar)
            {
                _star = new Decoders.STAR(outFormat.SampleRate, ProcessSTAR, Decoders.STAR.star_format.star_format_1_16383, sourceName);
            }
            else
            {
                _star = null;
            }
            _rootDecoder = new Decoders.RootDecoder(outFormat.SampleRate, decodeFleetSync, decodeP25, ProcessRootDecoder);

            _recorder = new AudioRecorder(sourceName, recordType, recordKickTime, outFormat, AudioProcessingGlobals.DefaultSaveFileWaveFormat, recordEnabled);
            _bytesPerSample = outFormat.BitsPerSample / 8;
            _encoding = outFormat.Encoding;
            _sigDelegate = sigDelegate;
        }
Ejemplo n.º 16
0
 private static byte[] Convert(AcmStream conversionStream, byte[] data, int offset, int length, ref int sourceBytesLeftovers)
 {
     int bytesInSourceBuffer = length + sourceBytesLeftovers;
     System.Array.Copy(data, offset, conversionStream.SourceBuffer, sourceBytesLeftovers, length);
     int sourceBytesConverted;
     int bytesConverted = conversionStream.Convert(bytesInSourceBuffer, out sourceBytesConverted);
     sourceBytesLeftovers = bytesInSourceBuffer - sourceBytesConverted;
     if (sourceBytesLeftovers > 0)
     {
         // shift the leftovers down
         System.Array.Copy(conversionStream.SourceBuffer, sourceBytesConverted, conversionStream.SourceBuffer, 0, sourceBytesLeftovers);
     }
     byte[] encoded = new byte[bytesConverted];
     System.Array.Copy(conversionStream.DestBuffer, 0, encoded, 0, bytesConverted);
     return encoded;
 }
Ejemplo n.º 17
0
 private static byte[] Convert(AcmStream conversionStream, byte[] data, int offset, int length, ref int sourceBytesLeftovers)
 {
     int bytesInSourceBuffer = length + sourceBytesLeftovers;
     Array.Copy(data, offset, conversionStream.SourceBuffer, sourceBytesLeftovers, length);
     int sourceBytesConverted;
     int bytesConverted = conversionStream.Convert(bytesInSourceBuffer, out sourceBytesConverted);
     sourceBytesLeftovers = bytesInSourceBuffer - sourceBytesConverted;
     if (sourceBytesLeftovers > 0)
     {
         //Debug.WriteLine(String.Format("Asked for {0}, converted {1}", bytesInSourceBuffer, sourceBytesConverted));
         // shift the leftovers down
         Array.Copy(conversionStream.SourceBuffer, sourceBytesConverted, conversionStream.SourceBuffer, 0, sourceBytesLeftovers);
     }
     byte[] encoded = new byte[bytesConverted];
     Array.Copy(conversionStream.DestBuffer, 0, encoded, 0, bytesConverted);
     return encoded;
 }
Ejemplo n.º 18
0
        /// <summary>
        /// Create a new WaveFormat conversion stream
        /// </summary>
        /// <param name="targetFormat">Desired output format</param>
        /// <param name="sourceStream">Source stream</param>
        public WaveFormatConversionStream(WaveFormat targetFormat, WaveStream sourceStream)
        {
            this.sourceStream = sourceStream;
            this.targetFormat = targetFormat;

            conversionStream = new AcmStream(sourceStream.WaveFormat, targetFormat);
            try
            {
                // work out how many bytes the entire input stream will convert to
                length = SourceToDest((int)sourceStream.Length);
                GetBlockAlign(targetFormat, sourceStream);
            }
            catch
            {
                Dispose();
                throw;
            }
            position = 0;
        }
        /// <summary>
        /// Create a new WaveFormat conversion stream
        /// </summary>
        /// <param name="targetFormat">Desired output format</param>
        /// <param name="sourceStream">Source stream</param>
        public WaveFormatConversionStream(WaveFormat targetFormat, WaveStream sourceStream)
        {
            this.sourceStream = sourceStream;
            this.targetFormat = targetFormat;

            conversionStream = new AcmStream(sourceStream.WaveFormat, targetFormat);
            /*try
            {
                // work out how many bytes the entire input stream will convert to
                length = conversionStream.SourceToDest((int)sourceStream.Length);
            }
            catch
            {
                Dispose();
                throw;
            }*/
            length = EstimateSourceToDest((int)sourceStream.Length);

            position = 0;
            preferredSourceReadSize = Math.Min(sourceStream.WaveFormat.AverageBytesPerSecond, conversionStream.SourceBuffer.Length);
            preferredSourceReadSize -= (preferredSourceReadSize%sourceStream.WaveFormat.BlockAlign);
        }
Ejemplo n.º 20
0
		public AcmMp3FrameDecompressor(WaveFormat sourceFormat)
		{
			pcmFormat = AcmStream.SuggestPcmFormat(sourceFormat);
			conversionStream = new AcmStream(sourceFormat, pcmFormat);
		}
Ejemplo n.º 21
0
 /// <summary>
 /// Disposes this stream
 /// </summary>
 /// <param name="disposing">true if the user called this</param>
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         // Release managed resources.
         if (conversionStream != null)
         {
             conversionStream.Dispose();
             conversionStream = null;
         }
         if (sourceStream != null)
         {
             sourceStream.Dispose();
             sourceStream = null;
         }
     }
     else
     {
         System.Diagnostics.Debug.Assert(false, "WaveFormatConversionStream was not disposed");
     }
     // Release unmanaged resources.
     // Set large fields to null.
     // Call Dispose on your base class.
     base.Dispose(disposing);
 }
Ejemplo n.º 22
0
 internal static void Dispose()
 {
     if (resampleChannelStream != null)
     {
         resampleChannelStream.Dispose();
         resampleChannelStream = null;
     }
     if (resampleRateStream != null)
     {
         resampleRateStream.Dispose();
         resampleRateStream = null;
     }
 }
Ejemplo n.º 23
0
        private static byte[] ResamplePcm(ref byte[] toResample, ref int sourceLength, WaveFormat sourceFormat, WaveFormat destPcmFormat, out int resultLength)
        {
            Debug.Assert(destPcmFormat.Encoding == WaveFormatEncoding.Pcm, "Codec format must be PCM");

            if (resampleRateStream != null && (!lastResampleSourceFormat.Equals(sourceFormat) || !lastResampleDestFormat.Equals(destPcmFormat)))
            {
                resampleRateStream.Dispose();
                resampleRateStream = null;
            }
            if (resampleRateStream == null)
            {
                WaveFormat sourceRateFormat = new WaveFormat(sourceFormat.SampleRate, sourceFormat.BitsPerSample, destPcmFormat.Channels);
                resampleRateStream = new AcmStream(sourceRateFormat, destPcmFormat);
                if (sourceFormat.Channels != destPcmFormat.Channels)
                {
                    WaveFormat destChanFormat = new WaveFormat(sourceFormat.SampleRate, sourceFormat.BitsPerSample, destPcmFormat.Channels);
                    if (resampleChannelStream != null)
                        resampleChannelStream.Dispose();
                    resampleChannelStream = new AcmStream(sourceFormat, destChanFormat);
                }
                lastResampleSourceFormat = sourceFormat;
                lastResampleDestFormat = destPcmFormat;
            }

            int bytesConverted;

            if (sourceFormat.Channels != destPcmFormat.Channels)
            {
                if (destPcmFormat.Channels == 1 && sourceFormat.Channels == 2)
                {
                    toResample = MixStereoToMono(toResample, sourceLength);
                    sourceLength = toResample.Length;
                }
                else
                {
                    Buffer.BlockCopy(toResample, 0, resampleChannelStream.SourceBuffer, 0, sourceLength);
                    sourceLength = resampleChannelStream.Convert(sourceLength, out bytesConverted);
                    if (bytesConverted >> 1 != sourceLength)
                    {
                        Console.WriteLine("WARNING: All input bytes were not converted.");
                    }
                    toResample = resampleChannelStream.DestBuffer;
                }
            }

            Buffer.BlockCopy(toResample, 0, resampleRateStream.SourceBuffer, 0, sourceLength);
            resultLength = resampleRateStream.Convert(sourceLength, out bytesConverted);
            if (bytesConverted != sourceLength)
            {
                Console.WriteLine("WARNING: All input bytes were not converted.");
                return null;
            }

            return resampleRateStream.DestBuffer;
        }