protected override void OnFormatChange(AudioFormat audioFormat)
        {
            if (audioFormat.WaveFormat != WaveFormatType.Pcm)
                throw new InvalidOperationException("MemoryAudioSink supports only PCM audio format.");

            _format = audioFormat;
        }
Beispiel #2
0
        public static void WriteToWav(Stream RawData, Stream Output, AudioFormat AudioFormat)
        {
            if (AudioFormat.WaveFormat != WaveFormatType.Pcm) throw new ArgumentException("You can write only to WAV.");

            BinaryWriter bwOutput = new BinaryWriter(Output);

            //WAV header
            bwOutput.Write("RIFF".ToCharArray());
            bwOutput.Write((uint)(RawData.Length + 36));
            bwOutput.Write("WAVE".ToCharArray());
            bwOutput.Write("fmt ".ToCharArray());
            bwOutput.Write((uint)0x10);
            bwOutput.Write((ushort)0x01);
            bwOutput.Write((ushort)AudioFormat.Channels);
            bwOutput.Write((uint)AudioFormat.SamplesPerSecond);
            bwOutput.Write((uint)(AudioFormat.BitsPerSample * AudioFormat.SamplesPerSecond * AudioFormat.Channels / 8));
            bwOutput.Write((ushort)(AudioFormat.BitsPerSample * AudioFormat.Channels / 8));
            bwOutput.Write((ushort)AudioFormat.BitsPerSample);
            bwOutput.Write("data".ToCharArray());
            bwOutput.Write((uint)RawData.Length);


            long originalRawDataStreamPosition = RawData.Position;
            RawData.Seek(0, SeekOrigin.Begin);
            byte[] buffer = new byte[4096];
            int read;
            while ((read = RawData.Read(buffer, 0, 4096)) > 0)
            {
                bwOutput.Write(buffer, 0, read);
            }
            RawData.Seek(originalRawDataStreamPosition, SeekOrigin.Begin);
        }
        public void Load(byte[] audioData, AudioFormat format)
        {
            this.ClearGraph();
            this.format = format;
            this.audioData = audioData;
            var graph = new PolyLineSegment();
            int step = this.GetSampleStep();
            int index = 0;
            int count = 0;

            double yScale = (this.audioCanvas.Height / 2) / short.MaxValue;

            int leftSampleCount = audioData.Length / 4; //assume 2 channels
            int actualLeftSampleCount = leftSampleCount / (step / 4);

            while (index < audioData.Length)
            {
                short sample = BitConverter.ToInt16(new byte[2] { audioData[index], audioData[index + 1] }, 0);
                double yPoint = sample * yScale + (this.audioCanvas.Height / 2);
                double xPoint = count * this.audioCanvas.Width / actualLeftSampleCount;

                graph.Points.Add(new Point() { X = xPoint, Y = yPoint });
                index += step;
                count++;
            }
            this.figure.Segments.Add(graph);
        }
        protected override void OnFormatChange(AudioFormat audioFormat)
        {
            if (this.Format != null)
            {
                throw new InvalidOperationException("Cannot change the audio format after it has already been set.");
            }

            this.Format = audioFormat;
        }
 public SupportedFormat(AudioFormat format)
 {
     this.format = format;
     this.FriendlyName =
         string.Format("{0}, {1}, {2}",
         this.format.SamplesPerSecond,
         this.format.BitsPerSample,
         this.format.Channels);
 }
		protected override void OnFormatChange(AudioFormat audioFormat)
		{
			if (AudioFormat == null)
			{
				AudioFormat = audioFormat;
			}
			else
			{
				throw new InvalidOperationException();
			}
		}
 protected override void OnFormatChange(wmAudioFormat audioFormat)
 {
     // We may need to do more with this.
     ClientLogger.Debug("The audio format was changed: BitsPerSample = {0}, Channels = {1}, SamplesPerSecond = {2}",
                        AudioConstants.BitsPerSample, AudioConstants.Channels, audioFormat.SamplesPerSecond);
     if (audioFormat.WaveFormat != WaveFormatType.Pcm ||
         audioFormat.BitsPerSample != AudioConstants.BitsPerSample)
     {
         throw new ArgumentException("The audio format was not supported.");
     }
     wmAudioFormat  = audioFormat;
     RawAudioFormat = new AudioFormat(audioFormat.SamplesPerSecond);
 }
        public AudioDecoder(byte[] sourceData, AudioFormat format)
        {
            this.samplesPerSecond = format.SamplesPerSecond;
            this.bitsPerSample = format.BitsPerSample;
            this.channels = format.Channels;

            byteRate = samplesPerSecond * channels * bitsPerSample / 8;
            blockAlign = (short)(channels * (bitsPerSample / 8));
            this.sourceData = sourceData;
            this.memoryStream = new MemoryStream();
            bufferByteCount = channels *
                bitsPerSample / 8 * numSamples;
            this.AudioBufferLength = 100;
        }
        protected override void OnFormatChange(wmAudioFormat audioFormat)
        {
            // We may need to do more with this.
            if (audioFormat == null ||
                audioFormat.WaveFormat != WaveFormatType.Pcm ||
                audioFormat.BitsPerSample != AudioConstants.BitsPerSample)
            {
                throw new ArgumentException("The audio format was not supported.");
            }

            // Only change the audio context factory if the raw audio format has actually changed.
            var rawAudioFormat = new AudioFormat(audioFormat.SamplesPerSecond, AudioConstants.MillisecondsPerFrame, audioFormat.Channels, audioFormat.BitsPerSample);

            if (_audioContextFactory == null || _audioContextFactory.RawAudioFormat != rawAudioFormat)
            {
                _audioContextFactory   = GetAudioContextFactory(rawAudioFormat, _playedAudioFormat, _mediaConfig, _mediaEnvironment);
                _logger.RawAudioFormat = rawAudioFormat;
                ClientLogger.Debug("The recorded audio format was changed: BitsPerSample = {0}, Channels = {1}, SamplesPerSecond = {2}",
                                   rawAudioFormat.BitsPerSample, rawAudioFormat.Channels, rawAudioFormat.SamplesPerSecond);
            }
        }
Beispiel #10
0
        protected override void OnFormatChange(AudioFormat audioFormat)
        {
            if (audioFormat.WaveFormat == WaveFormatType.Pcm)
            {
                speexEncoder.init(2, 8, audioFormat.SamplesPerSecond, audioFormat.Channels);
                pcmPacketSize = 2 * speexEncoder.getChannels() * speexEncoder.getFrameSize();
                temp = new byte[pcmPacketSize];
                tempOffset = 0;

                if (writer != null)
                    writer.close();

                writer = new org.xiph.speex.OggSpeexWriter(2, audioFormat.SamplesPerSecond, audioFormat.Channels, 1, false);

                memFile = new RandomOutputStream(new MemoryStream(2 * 1024 * pcmPacketSize));

                writer.open(memFile);
                writer.writeHeader("Encoded with Speex");
            }
            else
            {
                throw new Exception("Codec not supported");
            }
        }
 protected override void OnFormatChange(AudioFormat Format)
 {
     
 }
 protected override void OnFormatChange(AudioFormat audioFormat)
 {
     if (this.audioFormatSet)
         throw new AudioFormatChangedException(
             "Cannot change the audio format after it has already been assigned. I'm lazy like that.");
     this.audioFormatSet = true;
     this.AudioFormat = audioFormat;
 }
Beispiel #13
0
 /// <summary>
 /// 音频格式更改
 /// 第一次捕获时会调用一次,此后便确定格式
 /// </summary>
 /// <param name="audioFormat"></param>
 protected override void OnFormatChange(AudioFormat audioFormat)
 {
     Format = audioFormat;
 }
Beispiel #14
0
 public static void WriteHeaderStart(Stream str, AudioFormat fmt) {
   WriteHeader(str, (short) fmt.Channels, (short) fmt.BitsPerSample, (uint) fmt.SamplesPerSecond, 0);
 }
Beispiel #15
0
 protected override void OnFormatChange(AudioFormat audioFormat)
 {
     throw new NotImplementedException();
 }
Beispiel #16
0
 protected override void OnFormatChange(AudioFormat audioFormat) {
   Recorder.audioFormat = audioFormat;
 }
Beispiel #17
0
		protected abstract void OnFormatChange (AudioFormat audioFormat);
Beispiel #18
0
 public AudioFormatEx(AudioFormat audioFormat)
     : this(audioFormat.WaveFormat, audioFormat.Channels, audioFormat.BitsPerSample, audioFormat.SamplesPerSecond
         )
 {
 }
Beispiel #19
0
        /***************************************下面是音频保存相关操作****************************************/
        /// <summary>
        /// 添加WAV header
        /// </summary>
        /// <param name="audioByteLength"></param>
        /// <param name="audioFormat"></param>
        /// <returns></returns>
        public void SavePcmToWav(Stream rawData, Stream output, AudioFormat audioFormat)
        {
            if (audioFormat.WaveFormat != WaveFormatType.Pcm)
                throw new ArgumentException("Only PCM coding is supported.");

            BinaryWriter bwOutput = new BinaryWriter(output);

            bwOutput.Write("RIFF".ToCharArray());
            bwOutput.Write((uint)(rawData.Length + 36));
            bwOutput.Write("WAVE".ToCharArray());
            bwOutput.Write("fmt ".ToCharArray());
            bwOutput.Write((uint)0x10);
            bwOutput.Write((ushort)0x01);
            bwOutput.Write((ushort)audioFormat.Channels);
            bwOutput.Write((uint)audioFormat.SamplesPerSecond);
            bwOutput.Write((uint)(audioFormat.BitsPerSample * audioFormat.SamplesPerSecond * audioFormat.Channels / 8));
            bwOutput.Write((ushort)(audioFormat.BitsPerSample * audioFormat.Channels / 8));
            bwOutput.Write((ushort)audioFormat.BitsPerSample);
            bwOutput.Write("data".ToCharArray());
            bwOutput.Write((uint)rawData.Length);
            long originalRawDataStreamPosition = rawData.Position;
            rawData.Seek(0, SeekOrigin.Begin);
            byte[] buffer = new byte[4096];
            int read;      
            while ((read = rawData.Read(buffer, 0, 4096)) > 0)
            {
                bwOutput.Write(buffer, 0, read);
            }
            rawData.Seek(originalRawDataStreamPosition, SeekOrigin.Begin);
        }
Beispiel #20
0
 public static AudioFormat ToAudioFormat(this SLAudioFormat self)
 {
     return(new AudioFormat(self.WaveFormat.ToRawAudioEncoding(), self.BitsPerSample, self.Channels, self.SamplesPerSecond));
 }