Ejemplo n.º 1
0
        public override bool Decode(IAudioConsumer consumer, int maxLength)
        {
            if (!ready) return false;

            int remaining, length, size;
            byte[] buffer = new byte[2 * BUFFER_SIZE];
            short[] data = new short[BUFFER_SIZE];

            // Samples to read to get maxLength seconds of audio
            remaining = maxLength * this.sourceChannels * this.sampleRate;

            // Bytes to read
            length = 2 * Math.Min(remaining, BUFFER_SIZE);

            while ((size = reader.Read(buffer, 0, length)) > 0)
            {
                Buffer.BlockCopy(buffer, 0, data, 0, size);

                consumer.Consume(data, size / 2);

                remaining -= size / 2;
                if (remaining <= 0)
                {
                    break;
                }

                length = 2 * Math.Min(remaining, BUFFER_SIZE);
            }

            return true;
        }
Ejemplo n.º 2
0
 public SilenceRemover(IAudioConsumer consumer, int threshold = 0)
 {
     m_start = true;
     m_threshold = threshold;
     m_average = new MovingAverage(kSilenceWindow);
     m_consumer = consumer;
 }
Ejemplo n.º 3
0
        public override bool Decode(IAudioConsumer consumer, int maxLength)
        {
            if (reader == null)
            {
                return(false);
            }

            int remaining, length, size;

            byte[]  buffer = new byte[2 * BUFFER_SIZE];
            short[] data   = new short[BUFFER_SIZE];

            // Samples to read to get maxLength seconds of audio
            remaining = maxLength * this.Format.Channels * this.sampleRate;

            // Bytes to read
            length = 2 * Math.Min(remaining, BUFFER_SIZE);

            while ((size = reader.Read(buffer, 0, length)) > 0)
            {
                Buffer.BlockCopy(buffer, 0, data, 0, size);

                consumer.Consume(data, size / 2);

                remaining -= size / 2;
                if (remaining <= 0)
                {
                    break;
                }

                length = 2 * Math.Min(remaining, BUFFER_SIZE);
            }

            return(true);
        }
Ejemplo n.º 4
0
 public SilenceRemover(IAudioConsumer consumer, int threshold = 0)
 {
     m_start     = true;
     m_threshold = threshold;
     m_average   = new MovingAverage(kSilenceWindow);
     m_consumer  = consumer;
 }
Ejemplo n.º 5
0
        private void Decode(IAudioConsumer consumer, int maxLength)
        {
            int remaining, length, size;

            byte[]  buffer = new byte[2 * BUFFER_SIZE];
            short[] data   = new short[BUFFER_SIZE];

            // Samples to read to get maxLength seconds of audio
            remaining = maxLength * channels * sampleRate;

            // Bytes to read
            length = 2 * Math.Min(remaining, BUFFER_SIZE);

            while ((size = reader.Read(buffer, 0, length)) > 0)
            {
                Buffer.BlockCopy(buffer, 0, data, 0, size);

                consumer.Consume(data, size / 2);

                remaining -= size / 2;
                if (remaining <= 0)
                {
                    break;
                }

                length = 2 * Math.Min(remaining, BUFFER_SIZE);
            }
        }
Ejemplo n.º 6
0
        public AudioProcessor(int sample_rate, IAudioConsumer consumer)
        {
            m_buffer_size = kMaxBufferSize;
            m_target_sample_rate = sample_rate;
            m_consumer = consumer;

            m_buffer = new short[kMaxBufferSize];
            m_buffer_offset = 0;
            m_resample_buffer = new short[kMaxBufferSize];
        }
Ejemplo n.º 7
0
        public AudioProcessor(int sample_rate, IAudioConsumer consumer)
        {
            m_buffer_size        = kMaxBufferSize;
            m_target_sample_rate = sample_rate;
            m_consumer           = consumer;

            m_buffer          = new short[kMaxBufferSize];
            m_buffer_offset   = 0;
            m_resample_buffer = new short[kMaxBufferSize];
        }
Ejemplo n.º 8
0
        public bool Decode(IAudioConsumer consumer, int maxLength)
        {
            if (_waveSource == null)
            {
                return(false);
            }
            if (_buffer == null)
            {
                _buffer = new byte[BUFFER_SIZE * 2];
            }
            if (_data == null)
            {
                _data = new short[BUFFER_SIZE];
            }

            // maxLength is in seconds of audio
            // Calculate maximum bytes to read
            var maxBytes = maxLength * Channels * SampleRate;

            // Calculate actual bytes we can fit in buffer
            var bytesToRead = Math.Min(maxBytes, _buffer.Length);

            int read = 0;

            while ((read = _waveSource.Read(_buffer, 0, bytesToRead)) > 0)
            {
                Buffer.BlockCopy(_buffer, 0, _data, 0, read);

                consumer.Consume(_data, read / 2);

                maxBytes -= read / 2;
                if (maxBytes <= 0)
                {
                    break;
                }
                bytesToRead = Math.Min(maxBytes, _buffer.Length);
            }

            return(true);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Decode an audio file.
        /// </summary>
        public override bool Decode(IAudioConsumer consumer, int maxLength)
        {
            if (!ready)
            {
                return(false);
            }

            // Get the right stream:
            int stream = this.resample ? bassMixer : bassStream;

            int remaining, size = 0, length;

            short[] data = new short[BUFFER_SIZE];

            // Samples to read to get maxLength seconds of audio
            remaining = maxLength * this.sampleRate * this.channels;

            // Bytes to read
            length = 2 * Math.Min(remaining, BUFFER_SIZE);

            //while (Bass.BASS_ChannelIsActive(stream) == BASSActive.BASS_ACTIVE_PLAYING)
            while (Bass.BASS_ChannelIsActive(stream) == BASSActive.BASS_ACTIVE_PLAYING || size > 0)
            {
                size = Bass.BASS_ChannelGetData(stream, data, length);
                if (size > 0)
                {
                    consumer.Consume(data, size / 2);

                    remaining -= size / 2;
                    if (remaining <= 0)
                    {
                        break;
                    }
                    length = 2 * Math.Min(remaining, BUFFER_SIZE);
                }
            }

            return(true);
        }
Ejemplo n.º 10
0
        public bool Decode(IAudioConsumer consumer, int maxLength)
        {
            if (bitsPerSample != 16)
            {
                return(false);
            }

            using (var reader = OpenWaveStream(file))
            {
                int remaining, length, size;

                // TODO: get buffers from memory pool.
                byte[]  buffer = new byte[2 * BUFFER_SIZE];
                short[] data   = new short[BUFFER_SIZE];

                // Samples to read to get maxLength seconds of audio
                remaining = maxLength * Channels * SampleRate;

                // Bytes to read
                length = 2 * Math.Min(remaining, BUFFER_SIZE);

                while ((size = reader.Read(buffer, 0, length)) > 0)
                {
                    Buffer.BlockCopy(buffer, 0, data, 0, size);

                    consumer.Consume(data, size / 2);

                    remaining -= size / 2;
                    if (remaining <= 0)
                    {
                        break;
                    }

                    length = 2 * Math.Min(remaining, BUFFER_SIZE);
                }

                return(true);
            }
        }
Ejemplo n.º 11
0
 public abstract bool Decode(IAudioConsumer consumer, int maxLength);
Ejemplo n.º 12
0
 public abstract bool Decode(IAudioConsumer consumer, int maxLength);