Exemplo n.º 1
0
        internal static void RecordToWma(string fileName)
        {
            using (var wasapiCapture = new WasapiLoopbackCapture())
            {
                wasapiCapture.Initialize();
                var wasapiCaptureSource = new SoundInSource(wasapiCapture);
                using (var stereoSource = wasapiCaptureSource.ToStereo())
                {
                    using (var writer = MediaFoundationEncoder.CreateWMAEncoder(stereoSource.WaveFormat, fileName))
                    {
                        byte[] buffer = new byte[stereoSource.WaveFormat.BytesPerSecond];
                        wasapiCaptureSource.DataAvailable += (s, e) =>
                        {
                            int read = stereoSource.Read(buffer, 0, buffer.Length);
                            writer.Write(buffer, 0, read);
                            Console.Write(".");
                        };

                        wasapiCapture.Start();

                        Console.ReadKey();

                        wasapiCapture.Stop();
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a new file based on the given filename and start recording to it.
        /// Filename must include its full path.
        /// </summary>
        /// <param name="fileName">The name of a file to be created. Include its full path</param>
        /// <param name="codec">The codec to record in</param>
        /// <param name="bitRate">The bitrate of the file</param>
        /// <param name="channels">The channels to record</param>
        public void StartCapture(string fileName, AvailableCodecs codec, int bitRate, Channels channels)
        {
            if (!ReadyToRecord())
            {
                throw new NullReferenceException("There is no SoundInSource configured for the recorder.");
            }

            fileName = $"{fileName}.{codec.ToString().ToLower()}";

            WaveFormat waveSource;

            switch (channels)
            {
            case Channels.Mono:
                waveSource = _soundInSource.ToMono().WaveFormat;
                break;

            case Channels.Stereo:
                waveSource = _soundInSource.ToStereo().WaveFormat;
                break;

            default:
                throw new ArgumentException("The selected channel option could not be found.");
            }

            switch (codec)
            {
            case AvailableCodecs.MP3:
                _writer = MediaFoundationEncoder.CreateMP3Encoder(waveSource, fileName, bitRate);
                break;

            case AvailableCodecs.AAC:
                _writer = MediaFoundationEncoder.CreateAACEncoder(waveSource, fileName, bitRate);
                break;

            case AvailableCodecs.WMA:
                _writer = MediaFoundationEncoder.CreateWMAEncoder(waveSource, fileName, bitRate);
                break;

            case AvailableCodecs.WAV:
                _writer = new WaveWriter(fileName, waveSource);
                break;

            default:
                throw new ArgumentException("The specified codec was not found.");
            }

            byte[] buffer = new byte[waveSource.BytesPerSecond];

            _soundInSource.DataAvailable += (s, e) =>
            {
                int read = _waveStream.Read(buffer, 0, buffer.Length);
                _writer.Write(buffer, 0, read);
            };

            // Start recording
            _soundInSource.SoundIn.Start();
            _state = RecordingState.Recording;
        }
 public void CanEncodeToWMA()
 {
     using (var source = GlobalTestConfig.TestWav2S())
     {
         using (var encoder = MediaFoundationEncoder.CreateWMAEncoder(source.WaveFormat, _targetfilename))
         {
             MediaFoundationEncoder.EncodeWholeSource(encoder, source);
         }
     }
 }
Exemplo n.º 4
0
        public static void StartRecording(String fileName, int bitRate = 192000)
        {
            capture = new WasapiLoopbackCapture();

            capture.Initialize();

            wasapiCaptureSource = new SoundInSource(capture);
            stereoSource        = wasapiCaptureSource.ToStereo();

            switch (System.IO.Path.GetExtension(fileName))
            {
            case ".mp3":
                encoderWriter = MediaFoundationEncoder.CreateMP3Encoder(stereoSource.WaveFormat, fileName, bitRate);
                writerType    = WriterType.EncoderWriter;
                break;

            case ".wma":
                encoderWriter = MediaFoundationEncoder.CreateWMAEncoder(stereoSource.WaveFormat, fileName, bitRate);
                writerType    = WriterType.EncoderWriter;
                break;

            case ".aac":
                encoderWriter = MediaFoundationEncoder.CreateAACEncoder(stereoSource.WaveFormat, fileName, bitRate);
                writerType    = WriterType.EncoderWriter;
                break;

            case ".wav":
                waveWriter = new WaveWriter(fileName, capture.WaveFormat);
                writerType = WriterType.WaveWriter;
                break;
            }

            switch (writerType)
            {
            case WriterType.EncoderWriter:
                capture.DataAvailable += (s, e) =>
                {
                    encoderWriter.Write(e.Data, e.Offset, e.ByteCount);
                };
                break;

            case WriterType.WaveWriter:
                capture.DataAvailable += (s, e) =>
                {
                    waveWriter.Write(e.Data, e.Offset, e.ByteCount);
                };
                break;
            }

            // Start recording
            capture.Start();
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            CommandLine.Parser.Default.ParseArguments <Options>(args)
            .WithParsed(RunOptions)
            .WithNotParsed(HandleParseError);

            Console.WriteLine("Beginning to load: {0}", InputFilePath);
            using (IWaveSource source = CodecFactory.Instance.GetCodec(InputFilePath))
            {
                Console.WriteLine("Loaded: {0}", InputFilePath);
                foreach (var jobbie in Jobs)
                {
                    Console.WriteLine("Working on job with startTime: {0} timeSpan: {1}", jobbie.StartTime.ToString(), jobbie.Span.ToString());
                    using (MediaFoundationEncoder mediaFoundationEncoder =
                               MediaFoundationEncoder.CreateWMAEncoder(source.WaveFormat, jobbie.OutFileName))
                    {
                        AddTimeSpan(source, mediaFoundationEncoder, jobbie.StartTime, jobbie.Span);
                    }
                }
            }
        }