Beispiel #1
0
        private bool ReadTag()
        {
            uint tagType, dataSize, timeStamp, streamID, mediaInfo;

            byte[] data;

            if (_fileLength - _fileOffset < 11)
            {
                return(false);
            }

            // Read tag header
            tagType    = ReadUInt8();
            dataSize   = ReadUInt24();
            timeStamp  = ReadUInt24();
            timeStamp |= ReadUInt8() << 24;
            streamID   = ReadUInt24();

            // Read tag data
            if (dataSize == 0)
            {
                return(true);
            }
            if (_fileLength - _fileOffset < dataSize)
            {
                return(false);
            }
            mediaInfo = ReadUInt8();
            dataSize -= 1;
            data      = ReadBytes((int)dataSize);

            if (tagType == 0x8)
            {              // Audio
                if (_audioWriter == null)
                {
                    _audioWriter   = _extractAudio ? GetAudioWriter(mediaInfo) : new DummyAudioWriter();
                    ExtractedAudio = !(_audioWriter is DummyAudioWriter);
                }
                _audioWriter.WriteChunk(data, timeStamp);
            }
            else if (tagType == 0x9 && mediaInfo >> 4 != 5)
            {             // Video
                if (_videoWriter == null)
                {
                    _videoWriter   = _extractVideo ? GetVideoWriter(mediaInfo) : new DummyVideoWriter();
                    ExtractedVideo = !(_videoWriter is DummyVideoWriter);
                }
                if (_timeCodeWriter == null)
                {
                    var path = _outputPathBase + ".txt";
                    _timeCodeWriter    = new TimeCodeWriter(_extractTimeCodes && CanWriteTo(path) ? path : null);
                    ExtractedTimeCodes = _extractTimeCodes;
                }
                _videoTimeStamps.Add(timeStamp);
                _videoWriter.WriteChunk(data, timeStamp, (int)((mediaInfo & 0xF0) >> 4));
                _timeCodeWriter.Write(timeStamp);
            }

            return(true);
        }
Beispiel #2
0
        public static void BuildParseCompareAudio(IAudioFormat audio, IAudioWriter writer, IAudioReader reader)
        {
            byte[]       builtFile   = writer.GetFile(audio);
            IAudioFormat parsedAudio = reader.ReadFormat(builtFile);

            Assert.Equal(audio, parsedAudio, new AudioFormatComparer());
        }
Beispiel #3
0
 private void CloseOutput(FractionUInt32?averageFrameRate, bool disposing)
 {
     if (_videoWriter != null)
     {
         _videoWriter.Finish(averageFrameRate ?? new FractionUInt32(25, 1));
         if (disposing && _videoWriter.Path != null)
         {
             try { File.Delete(_videoWriter.Path); }
             catch { }
         }
         _videoWriter = null;
     }
     if (_audioWriter != null)
     {
         _audioWriter.Finish();
         if (disposing && _audioWriter.Path != null)
         {
             try { File.Delete(_audioWriter.Path); }
             catch { }
         }
         _audioWriter = null;
     }
     if (_timeCodeWriter != null)
     {
         _timeCodeWriter.Finish();
         if (disposing && _timeCodeWriter.Path != null)
         {
             try { File.Delete(_timeCodeWriter.Path); }
             catch { }
         }
         _timeCodeWriter = null;
     }
 }
Beispiel #4
0
        public XnaRecorder(Stream stream, EncodingFormat format, int preset)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            _stream     = stream;
            _sampleRate = 16000;
            _bitRate    = 16;
            _channels   = 1;
            _format     = format;
            _preset     = preset;

            _size     = 0;
            _duration = 0;
            _state    = RecorderState.Unknown;

            InitializeMicrophone();

            if (_format == EncodingFormat.Wave)
            {
                _writer = new WaveWriter(_stream, _sampleRate, _bitRate, _channels);
            }
            else if (_format == EncodingFormat.MP3)
            {
                _writer = new Mp3Writer(_stream, _sampleRate, _bitRate, _channels, (LAMEPreset)_preset);
            }
        }
Beispiel #5
0
        private bool ReadTag()
        {
            uint tagType, dataSize, timeStamp, mediaInfo;

            if ((_fileLength - _fileOffset) < 11)
            {
                return(false);
            }

            // Read tag header
            tagType    = ReadUInt8();
            dataSize   = ReadUInt24();
            timeStamp  = ReadUInt24();
            timeStamp |= ReadUInt8() << 24;
            ReadUInt24();

            // Read tag data
            if (dataSize == 0)
            {
                return(true);
            }
            if ((_fileLength - _fileOffset) < dataSize)
            {
                return(false);
            }
            mediaInfo = ReadUInt8();
            dataSize -= 1;
            byte[] data = ReadBytes((int)dataSize);

            if (tagType == 0x8 && this.audioOutputStream != null)
            {
                // Audio
                if (_audioWriter == null)
                {
                    _audioWriter     = this.GetAudioWriter(mediaInfo);
                    this.AudioFormat = this._audioWriter.AudioFormat;
                }

                _audioWriter.WriteChunk(data, timeStamp);
            }

            else if ((tagType == 0x9) && ((mediaInfo >> 4) != 5) && this.videoOutputStream != null)
            {
                // Video
                if (_videoWriter == null)
                {
                    _videoWriter     = this.GetVideoWriter(mediaInfo);
                    this.VideoFormat = this._videoWriter.VideoFormat;
                }

                _videoTimeStamps.Add(timeStamp);
                _videoWriter.WriteChunk(data, timeStamp, (int)((mediaInfo & 0xF0) >> 4));
            }

            return(true);
        }
Beispiel #6
0
 private void CloseOutput(FractionUInt32? averageFrameRate)
 {
     if (_videoWriter != null)
     {
         _videoWriter.Finish(averageFrameRate ?? new FractionUInt32(25, 1));
         _videoWriter = null;
     }
     if (_audioWriter != null)
     {
         _audioWriter.Dispose();
         _audioWriter = null;
     }
 }
Beispiel #7
0
        public WasapiRecorder(Stream stream, int sampleRate, int bitRate, int channels, EncodingFormat format, int preset)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (channels != 1 && channels != 2)
            {
                throw new ArgumentException(string.Format("Unsupported number of channels {0}", channels), "channels");
            }

            if (bitRate != 16)
            {
                throw new ArgumentException(string.Format("Unsupported PCM sample size {0}", bitRate), "bitRate");
            }

            if (sampleRate < 8000 || sampleRate > 48000)
            {
                throw new ArgumentException(string.Format("Unsupported Sample Rate {0}", sampleRate), "sampleRate");
            }

            _stream     = stream;
            _sampleRate = sampleRate;
            _bitRate    = bitRate;
            _channels   = channels;
            _format     = format;
            _preset     = preset;

            _capture = new AudioCapture();
            _capture.SetCaptureFormat(_sampleRate, (ushort)_channels, (ushort)_bitRate);

            _size     = 0;
            _duration = 0;
            _state    = RecorderState.Unknown;

            if (_format == EncodingFormat.Wave)
            {
                _writer = new WaveWriter(_stream, _sampleRate, _bitRate, _channels);
            }
            else if (_format == EncodingFormat.MP3)
            {
                _writer = new Mp3Writer(_stream, _sampleRate, _bitRate, _channels, (LAMEPreset)_preset);
            }

            _timer          = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromMilliseconds(33);
            _timer.Tick    += OnSampleReceived;
        }
Beispiel #8
0
        private bool ReadTag()
        {
            uint tagType, dataSize, timeStamp, streamID, mediaInfo;

            byte[] data;

            if ((_fileLength - _fileOffset) < 11)
            {
                return(false);
            }

            // Read tag header
            tagType    = ReadUInt8();
            dataSize   = ReadUInt24();
            timeStamp  = ReadUInt24();
            timeStamp |= ReadUInt8() << 24;
            streamID   = ReadUInt24();

            // Read tag data
            if (dataSize == 0)
            {
                return(true);
            }
            if ((_fileLength - _fileOffset) < dataSize)
            {
                return(false);
            }

            mediaInfo = ReadUInt8();
            dataSize -= 1;
            data      = ReadBytes((int)dataSize);

            if (tagType == 0x8)
            {  // Audio
                if (_audioWriter == null)
                {
                    _audioWriter    = GetAudioWriter(mediaInfo);
                    _extractedAudio = !(_audioWriter is DummyAudioWriter);
                }
                _audioWriter.WriteChunk(data, timeStamp);
            }
            else if ((tagType == 0x9) && ((mediaInfo >> 4) != 5))
            { /* Video*/
            }

            return(true);
        }
Beispiel #9
0
        private async Task CloseOutput(bool disposing)
        {
            if (_audioWriter != null)
            {
                var isFinish = await _audioWriter.Finish();

                if (disposing && (_audioWriter.FileName != null))
                {
                    try
                    {
                        if (_fs != null)
                        {
                            _fs.Dispose();
                            _fs = null;
                        }
                    }
                    catch { }
                }
                _audioWriter = null;
            }
        }
		private bool ReadTag() {
			uint tagType, dataSize, timeStamp, streamID, mediaInfo;
			byte[] data;

			if ((_fileLength - _fileOffset) < 11) {
				return false;
			}

			// Read tag header
			tagType = ReadUInt8();
			dataSize = ReadUInt24();
			timeStamp = ReadUInt24();
			timeStamp |= ReadUInt8() << 24;
			streamID = ReadUInt24();

			// Read tag data
			if (dataSize == 0) {
				return true;
			}
			if ((_fileLength - _fileOffset) < dataSize) {
				return false;
			}
			mediaInfo = ReadUInt8();
			dataSize -= 1;
			data = ReadBytes((int)dataSize);

			if (tagType == 0x8) {  // Audio
				if (_audioWriter == null) {
					_audioWriter = _extractAudio ? GetAudioWriter(mediaInfo) : new DummyAudioWriter();
					_extractedAudio = !(_audioWriter is DummyAudioWriter);
				}
				_audioWriter.WriteChunk(data, timeStamp);
			}
			else if ((tagType == 0x9) && ((mediaInfo >> 4) != 5)) { // Video
				if (_videoWriter == null) {
					_videoWriter = _extractVideo ? GetVideoWriter(mediaInfo) : new DummyVideoWriter();
					_extractedVideo = !(_videoWriter is DummyVideoWriter);
				}
				if (_timeCodeWriter == null) {
					string path = _outputPathBase + ".txt";
					_timeCodeWriter = new TimeCodeWriter((_extractTimeCodes && CanWriteTo(path)) ? path : null);
					_extractedTimeCodes = _extractTimeCodes;
				}
				_videoTimeStamps.Add(timeStamp);
				_videoWriter.WriteChunk(data, timeStamp, (int)((mediaInfo & 0xF0) >> 4));
				_timeCodeWriter.Write(timeStamp);
			}

			return true;
		}
		private void CloseOutput(FractionUInt32? averageFrameRate, bool disposing) {
			if (_videoWriter != null) {
				_videoWriter.Finish(averageFrameRate ?? new FractionUInt32(25, 1));
				if (disposing && (_videoWriter.Path != null)) {
					try { File.Delete(_videoWriter.Path); }
					catch { }
				}
				_videoWriter = null;
			}
			if (_audioWriter != null) {
				_audioWriter.Finish();
				if (disposing && (_audioWriter.Path != null)) {
					try { File.Delete(_audioWriter.Path); }
					catch { }
				}
				_audioWriter = null;
			}
			if (_timeCodeWriter != null) {
				_timeCodeWriter.Finish();
				if (disposing && (_timeCodeWriter.Path != null)) {
					try { File.Delete(_timeCodeWriter.Path); }
					catch { }
				}
				_timeCodeWriter = null;
			}
		}
Beispiel #12
0
        private bool ReadTag()
        {
            uint tagType, dataSize, timeStamp, streamID, mediaInfo;
            byte[] data;

            if ((_fileLength - _fileOffset) < 11)
            {
                return false;
            }

            // Read tag header
            tagType = ReadUInt8();
            dataSize = ReadUInt24();
            timeStamp = ReadUInt24();
            timeStamp |= ReadUInt8() << 24;
            streamID = ReadUInt24();

            // Read tag data
            if (dataSize == 0)
            {
                return true;
            }
            if ((_fileLength - _fileOffset) < dataSize)
            {
                return false;
            }
            mediaInfo = ReadUInt8();
            dataSize -= 1;
            data = ReadBytes((int)dataSize);

            if (tagType == 0x8)
            {  // Audio
                if (_audioWriter == null)
                {
                    _audioWriter = _extractAudio ? GetAudioWriter(mediaInfo) : new DummyAudioWriter();
                    _extractedAudio = !(_audioWriter is DummyAudioWriter);
                }
                _audioWriter.WriteChunk(data, timeStamp);
            }
            return true;
        }
Beispiel #13
0
 private void CloseOutput(bool disposing)
 {
     if (_audioWriter != null)
     {
         _audioWriter.Finish();
         if (disposing && (_audioWriter.Path != null))
         {
             try { File.Delete(_audioWriter.Path); }
             catch { }
         }
         _audioWriter = null;
     }
 }
Beispiel #14
0
        private bool ReadTag()
        {
            uint tagType, dataSize, timeStamp, mediaInfo;

            if ((_fileLength - _fileOffset) < 11)
            {
                return false;
            }

            // Read tag header
            tagType = ReadUInt8();
            dataSize = ReadUInt24();
            timeStamp = ReadUInt24();
            timeStamp |= ReadUInt8() << 24;
            ReadUInt24();

            // Read tag data
            if (dataSize == 0)
            {
                return true;
            }
            if ((_fileLength - _fileOffset) < dataSize)
            {
                return false;
            }
            mediaInfo = ReadUInt8();
            dataSize -= 1;
            byte[] data = ReadBytes((int)dataSize);

            if (tagType == 0x8 && this.audioOutputStream != null)
            {
                // Audio
                if (_audioWriter == null)
                {
                    _audioWriter = this.GetAudioWriter(mediaInfo);
                    this.AudioFormat = this._audioWriter.AudioFormat;
                }

                _audioWriter.WriteChunk(data, timeStamp);
            }

            else if ((tagType == 0x9) && ((mediaInfo >> 4) != 5) && this.videoOutputStream != null)
            {
                // Video
                if (_videoWriter == null)
                {
                    _videoWriter = this.GetVideoWriter(mediaInfo);
                    this.VideoFormat = this._videoWriter.VideoFormat;
                }

                _videoTimeStamps.Add(timeStamp);
                _videoWriter.WriteChunk(data, timeStamp, (int)((mediaInfo & 0xF0) >> 4));
            }

            return true;
        }
Beispiel #15
0
        private void CloseOutput(FractionUInt32? averageFrameRate)
        {
            if (_videoWriter != null)
            {
                _videoWriter.Finish(averageFrameRate ?? new FractionUInt32(25, 1));
                _videoWriter = null;
            }

            if (_audioWriter != null)
            {
                _audioWriter.Dispose();
                _audioWriter = null;
            }
        }