public ReadSamples ( float buffer, int offset, int count ) : int | ||
buffer | float | The buffer to write the samples to |
offset | int | The offset into the buffer to write the samples to |
count | int | The number of samples to write |
Результат | int |
public void Mix(float[] data, int channels, float volume) { if (reader.SampleRate == 44100 && reader.Channels == channels) { float[] ndata = new float[data.Length]; int read = reader.ReadSamples(ndata, 0, ndata.Length); for (int i = 0; i < read; i++) { float b = ndata[i] * volume; float a = data[i]; data[i] = a + b - a * b; } } else if (reader.SampleRate == 44100) { float[] ndata = new float[data.Length / channels * reader.Channels]; int read = reader.ReadSamples(ndata, 0, ndata.Length) / reader.Channels; for (int i = 0; i < read; i++) { for (int c = 0; c < channels; c++) { float b = ndata[i * reader.Channels + c % reader.Channels] * volume; float a = data[i * channels + c]; data[i * channels + c] = a + b - a * b; } } } else { Debug.Log("OnlyPlay 44100,now rate is" + reader.SampleRate); } }
private static void LoadOgg(VorbisReader vorbis, out byte[] data, out ALFormat format, out uint sampleRate, out TimeSpan len) { sampleRate = (uint)vorbis.SampleRate; format = vorbis.Channels == 1 ? ALFormat.Mono16 : ALFormat.Stereo16; len = vorbis.TotalTime; float[] buffer = new float[vorbis.SampleRate / 10 * vorbis.Channels]; List<byte> bytes = new List<byte>((int)(vorbis.SampleRate * vorbis.Channels * 2 * len.TotalSeconds)); int count = 0; while ((count = vorbis.ReadSamples(buffer, 0, buffer.Length)) > 0) { for (int i = 0; i < count; i++) { int temp = (int)(short.MaxValue * buffer [i]); if (temp > short.MaxValue) { temp = short.MaxValue; } else if (temp < short.MinValue) { temp = short.MinValue; } short tempBytes = (short)temp; byte byte1 = (byte)((tempBytes >> 8) & 0x00FF); byte byte2 = (byte)((tempBytes >> 0) & 0x00FF); // Little endian bytes.Add(byte2); bytes.Add(byte1); } } // TODO: Add better implementation so that there's no need for array copying data = bytes.ToArray(); }
private static PCMData ReadAll(VorbisReader vorbisStream) { PCMData pcm; pcm.ChannelCount = (short)vorbisStream.Channels; pcm.SampleRate = vorbisStream.SampleRate; var samples = new List<float>(); var buffer = new float[DefaultBufferSize]; int samplesRead = 0; while ((samplesRead = vorbisStream.ReadSamples(buffer, 0, buffer.Length)) > 0) { if (samplesRead != buffer.Length) { Array.Resize(ref buffer, samplesRead); } samples.AddRange(buffer); buffer = new float[DefaultBufferSize]; } var samplesArray = samples.ToArray(); pcm.Data = new byte[samplesArray.Length * SampleSizeBytes]; Buffer.BlockCopy(samplesArray, 0, pcm.Data, 0, pcm.Data.Length); return pcm; }
/// <summary> /// Reads a block of audio samples from the current stream and writes the data to given sample. /// </summary> /// <param name="samples">The sample array to fill.</param> /// <param name="count">The maximum number of samples to read.</param> /// <returns>The number of samples actually read.</returns> public override long Read(short[] samples, long count) { float[] buffer = new float[count]; int read = _reader.ReadSamples(buffer, 0, (int)count); CastBuffer(buffer, samples, read); return(read); }
public OggAudioSource(Stream file, bool streaming = true) { Streaming = streaming; _fileStream = file; _reader = new VorbisReader(file, false); _sampleRate = _reader.SampleRate; _format = (_reader.Channels == 2 ? ALFormat.Stereo16 : ALFormat.Mono16); if(!Streaming) { _data = new short[_reader.TotalSamples]; float[] samples = new float[_reader.TotalSamples]; _reader.ReadSamples(samples, 0, (int)_reader.TotalSamples); CastSamples(ref samples, ref _data, samples.Length); } }
private SoundEffect LoadOgg(Stream stream) { using (var reader = new VorbisReader(stream, false)) { float[] _buffer = new float[reader.TotalSamples]; byte[] buffer = new byte[reader.TotalSamples * 2]; reader.ReadSamples(_buffer, 0, _buffer.Length); for (int i = 0; i < _buffer.Length; i++) { short val = (short)Math.Max(Math.Min(short.MaxValue * _buffer[i], short.MaxValue), short.MinValue); var decoded = BitConverter.GetBytes(val); buffer[i * 2] = decoded[0]; buffer[i * 2 + 1] = decoded[1]; } return new SoundEffect(buffer, reader.SampleRate, reader.Channels == 1 ? AudioChannels.Mono : AudioChannels.Stereo); } }
public ISoundData Decode(Stream stream) { VorbisReader reader = null; try { reader = new VorbisReader (stream, false); int channels = reader.Channels; const int BITS_PER_SAMPLE = 16; int sampleRate = reader.SampleRate; //Code from: https://github.com/AdamsLair/duality/blob/a911c46b6bc830c05d3bb1202f8e7060543eaefa/Duality/Audio/OggVorbis.cs List<float[]> allBuffers = new List<float[]>(); bool eof = false; int totalSamplesRead = 0; int dataRead = 0; while (!eof) { float[] buffer = new float[DEFAULT_BUFFER_SIZE]; int bufferSamplesRead = 0; while(bufferSamplesRead < buffer.Length) { int samplesRead; lock (_readMutex) { samplesRead = reader.ReadSamples(buffer, dataRead, buffer.Length - dataRead); } if (samplesRead > 0) { bufferSamplesRead += samplesRead; } else { eof = true; break; } } allBuffers.Add(buffer); totalSamplesRead += bufferSamplesRead; } if (totalSamplesRead > 0) { short[] data = new short[totalSamplesRead]; int offset = 0; for (int i = 0; i < allBuffers.Count; i++) { int len = Math.Min(totalSamplesRead - offset, allBuffers[i].Length); castBuffer(allBuffers[i], data, offset, len); offset += len; } return new SoundData(channels, BITS_PER_SAMPLE, sampleRate, data, Marshal.SizeOf(typeof(short)) * data.Length); } else { Debug.WriteLine("OggDecoder: No samples found in ogg file"); return null; } } catch (InvalidDataException e) { Debug.WriteLine("OggDecoder: Failed to read ogg. " + e.ToString()); return null; } finally { if (reader != null) reader.Dispose(); } }
private void Streamer (object arg) { string fileName = (string)arg; VorbisReader vreader = new VorbisReader (fileName); // 3 seconds buffering int bufLen = vreader.Channels * vreader.SampleRate * 3; float[] buffer = new float[bufLen]; if (this.bufferIds == null) { this.bufferIds = AL.GenBuffers (3); AudioDevice.CheckError ("allocating OpenAL buffers for streaming"); } ALFormat format = vreader.Channels == 2 ? ALFormat.StereoFloat32Ext : ALFormat.MonoFloat32Ext; // buffer data int i; for (i = 0; i < bufferIds.Length; i++) { int count = vreader.ReadSamples (buffer, 0, bufLen); // end of the stream ? if (count == 0) { if (!this.streamLooping) break; vreader.DecodedPosition = 0; count = vreader.ReadSamples (buffer, 0, bufLen); } AL.BufferData<float> (this.bufferIds [i], format, buffer, count * sizeof(float), vreader.SampleRate); AudioDevice.CheckError ("loading data in OpenAL buffer for streaming"); } AL.SourceQueueBuffers (this.audioSourceId, i, bufferIds); AL.SourcePlay (this.audioSourceId); while (true) { Thread.Sleep (30); int processed = 0; AL.GetSource (this.audioSourceId, ALGetSourcei.BuffersProcessed, out processed); while (processed > 0) { int bufferId = AL.SourceUnqueueBuffer (this.audioSourceId); int count = vreader.ReadSamples (buffer, 0, bufLen); // end of the straem ? if (count == 0) { if (!this.streamLooping) { this.Stop (); // to reset status AL.Source (this.audioSourceId, ALSourcei.Buffer, 0); return; } vreader.DecodedPosition = 0; count = vreader.ReadSamples (buffer, 0, bufLen); } AL.BufferData<float> (bufferId, format, buffer, count * sizeof(float), vreader.SampleRate); AudioDevice.CheckError ("loading data in OpenAL buffer for streaming"); AL.SourceQueueBuffer (this.audioSourceId, bufferId); processed--; } } }
void loadData() { if (filepath != string.Empty && File.Exists(filepath)) { #if BASS_AUDIO long trackLengthInBytes = Bass.BASS_ChannelGetLength(handle); const float FRAME_TIME = 0.002f; long frameLengthInBytes = Bass.BASS_ChannelSeconds2Bytes(handle, FRAME_TIME); int NumFrames = (int)System.Math.Round(1f * trackLengthInBytes / frameLengthInBytes); _data = new float[NumFrames * 2]; float[] levels = new float[2]; for (int i = 0; i < _data.Length && !stop; i += 2) { Bass.BASS_ChannelGetLevel(handle, levels, FRAME_TIME, BASSLevel.BASS_LEVEL_STEREO); float average = (levels[0] + levels[1]) / 2.0f; _data[i] = -average; _data[i + 1] = average; } #else byte[] bytes = File.ReadAllBytes(filepath); float[] sampleData = new float[0]; switch (Path.GetExtension(filepath)) { case (".ogg"): NVorbis.VorbisReader vorbis = new NVorbis.VorbisReader(filepath); vorbis.ClipSamples = false; _data = new float[vorbis.TotalSamples * vorbis.Channels]; //vorbis.ReadSamples(_data, 0, _data.Length); int count = 0; while ((count += vorbis.ReadSamples(_data, count, 16000)) > 0 && !stop) { } break; case (".wav"): WAV wav = new WAV(bytes); sampleData = NAudioPlayer.InterleaveChannels(wav); break; case (".mp3"): NAudioPlayer.WAVFromMp3Data(bytes, out sampleData); break; default: return; } #endif if (!stop) { #if !BASS_AUDIO _data = sampleData; #endif Debug.Log("Sample length: " + _data.Length); } else { _data = new float[0]; } } }