Esempio n. 1
0
        /// <summary>
        /// Writes to file.
        /// </summary>
        public void Write(byte[] Data, int Offset, int Count)
        {
            lock (_syncLock)
            {
                if (_riff && _writer.BaseStream.Length + Count > uint.MaxValue)
                {
                    throw new ArgumentException("WAV file too large", nameof(Count));
                }

                if (_encoder != null)
                {
                    _encoder.EnsureBufferIsSufficient(ref _encodedBuffer, Count);

                    var encodedLength = _encoder.Encode(Data, Offset, Count, _encodedBuffer, 0);

                    if (encodedLength <= 0)
                    {
                        return;
                    }

                    _writer.Write(_encodedBuffer, 0, encodedLength);
                    Length += encodedLength;
                }
                else
                {
                    _writer.Write(Data, Offset, Count);
                    Length += Count;
                }
            }
        }
Esempio n. 2
0
 public Stream Encode(FileInfo sourceFile, int compressionLevel)
 {
     using (Stream rawAudio = fileReader.Read(sourceFile))
     {
         return(encoder.Encode(rawAudio, compressionLevel));
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Encodes a signal from the specified file.
        /// </summary>
        ///
        /// <param name="fileName">File name to save the signal to.</param>
        /// <param name="signal">The audio signal that should be saved to disk.</param>
        ///
        public static void EncodeToFile(string fileName, Signal signal)
        {
            string fileExtension = FormatHandlerAttribute.GetNormalizedExtension(fileName);

            IAudioEncoder encoder = FormatEncoderAttribute.GetEncoder(fileExtension, encoderTypes, encoders.Value);

            if (encoder != null)
            {
                // open stream
                using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                {
                    // open decoder
                    encoder.Open(stream);

                    // write all audio frames
                    encoder.Encode(signal);

                    encoder.Close();
                }

                return;
            }

            throw new ArgumentException(String.Format("No suitable encoder has been found for the file format {0}. If ", fileExtension) +
                                        "you are trying to encode .wav files, please add a reference to Accord.Audio.DirectSound, and make sure you " +
                                        "are using at least one type from assembly in your code (to make sure the assembly is loaded).", "fileName");
        }
        void AudioProviderOnDataAvailable(object Sender, DataAvailableEventArgs DataAvailableEventArgs)
        {
            _audioEncoder.EnsureBufferIsSufficient(ref _encodedBuffer, DataAvailableEventArgs.Length);

            var encodedLength = _audioEncoder.Encode(DataAvailableEventArgs.Buffer, 0, DataAvailableEventArgs.Length, _encodedBuffer, 0);

            DataAvailable?.Invoke(this, new DataAvailableEventArgs(_encodedBuffer, encodedLength));
        }