protected override void BeforePlay() { if (m_Playing) { Stop(); } try { if (m_Stream != null) { m_Stream.Close(); m_Stream = null; } m_Stream = new MP3Stream(Path, NUMBER_OF_PCM_BYTES_TO_READ_PER_CHUNK); Frequency = m_Stream.Frequency; m_Playing = true; } catch { // file in use or access denied. m_Playing = false; } }
public void MP3Stream_basic_properties() { using (var mp3 = Assembly.GetExecutingAssembly().GetManifestResourceStream("MP3Sharp.UnitTests.sample.mp3")) using (var stream = new MP3Stream(mp3)) { Assert.IsFalse(stream.IsEOF); Assert.AreEqual(stream.Length, mp3.Length); Assert.IsTrue(stream.CanRead); Assert.IsTrue(stream.CanSeek); Assert.IsFalse(stream.CanWrite); Assert.AreEqual(0, stream.ChunkSize); Assert.AreEqual(370, stream.Position); Assert.AreEqual(44100, stream.Frequency); Assert.AreEqual(2, stream.ChannelCount); Assert.AreEqual(SoundFormat.Pcm16BitStereo, stream.Format); byte[] buffer = new byte[4096]; int bytesReturned = 1; int totalBytesRead = 0; while (bytesReturned > 0) { bytesReturned = stream.Read(buffer, 0, buffer.Length); totalBytesRead += bytesReturned; } Assert.IsTrue(stream.IsEOF); } }
private unsafe short[] Decode() { using (var output = new MemoryStream()) { ushort[] endian_test = new ushort[] { 0x1234 }; fixed(ushort *ptr1 = endian_test) { byte *ptr2 = (byte *)ptr1; if (*ptr2 != 0x34) { throw new NotSupportedException("MP3 decoding is only supported on little-endian processors"); } } using (var input = new MemoryStream(Body, false)) using (var mp3 = new MP3Stream(input)) { mp3.CopyTo(output); } byte[] array = output.ToArray(); fixed(byte *ptr = array) { short *ptr16 = (short *)ptr; short[] array16 = new short[array.Length / sizeof(short)]; Marshal.Copy((IntPtr)ptr16, array16, 0, array16.Length); return(array16); } } }
public MusicStreamingMP3(byte[] data) { MP3Stream stream = new MP3Stream(new MemoryStream(data)); instance = new DynamicSoundEffectInstance(stream.Frequency, (AudioChannels)stream.ChannelCount); this.stream = stream; }
public Mp3Format(Stream stream) { var startPosition = stream.Position; try { mp3 = new MP3Stream(stream); this.stream = stream; // Make a first guess based on the file size and bitrate // This should be fine for constant bitrate files LengthInSeconds = mp3.Length * 8f / (2f * Channels * SampleRate); try { // Attempt to parse a more accurate length from the file metadata; LengthInSeconds = (float)new TagLib.Mpeg.AudioFile(new StreamAbstraction(stream)).Properties.Duration.TotalSeconds; } catch { } } finally { stream.Position = startPosition; } }
private SoundEffect LoadSound(Stream stream, int length, string extension) { switch (extension) { case ".wav": if (!stream.CanSeek) { stream = new MemoryStream(stream.ReadBytes(length)); } return(SoundEffect.FromStream(stream)); case ".mp3": using (var mp3Stream = new MP3Stream(stream)) using (var ms = new MemoryStream()) { mp3Stream.CopyTo(ms); return(new SoundEffect(ms.ToArray(), mp3Stream.Frequency, (AudioChannels)mp3Stream.ChannelCount)); } case ".ogg": using (var reader = new VorbisReader(stream, true)) { var buffer = new byte[reader.TotalSamples * 2 * reader.Channels]; var floatBuf = new float[buffer.Length / 2]; reader.ReadSamples(floatBuf, 0, floatBuf.Length); MusicStreamingOGG.Convert(floatBuf, buffer); return(new SoundEffect(buffer, reader.SampleRate, (AudioChannels)reader.Channels)); } } throw new ResourceLoadException("Unknown sound extension " + extension); }
public override void Close() { if (_stream != null) { _stream.Close(); _stream = null; } }
public void Mp3ToPcmConversionTest() { using (var mp3Stream = new MP3Stream(AudioFiles.Mp3Filename)) { var bytes = mp3Stream.ToArray(); AssertExtensions.AnyNotZero(bytes); } }
protected override void AfterStop() { if (m_Playing) { m_Playing = false; m_Stream.Close(); m_Stream = null; } }
protected override void AfterStop() { if (_playing) { _playing = false; _stream.Close(); _stream = null; } }
public override void Reset() { if (stream != null) { underlying.Position = 0; //mp3 is not designed to loop and creates static if you just reset the stream due to fourier encoding carryover //if you're really smart, you can make a looping version and PR it stream = new MP3Stream(underlying); } }
static void PlayMP3(string filename) { var buffers = Initialize(out IntPtr device, out ContextHandle context, out int source); var reader = new MP3Stream(string.Format(AudioFilesPathFormat, filename)); PlayAndDispose(reader, buffers, source, reader.Frequency, ALFormat.Stereo16); Dispose(ref device, ref context); }
protected override void PrepareStream() { underlying = stream; var mp3Stream = new MP3Stream(stream); sampleRate = mp3Stream.Frequency; channels = (AudioChannels)mp3Stream.ChannelCount; stream = mp3Stream; }
public MP3File(string Path) { if (FileManager.IsLinux) { m_Stream = new MP3Stream(File.Open(Path, FileMode.Open, FileAccess.ReadWrite)); } else //Used by NAudio, only on Windows. { RFullyStream = new ReadFullyStream(m_Stream); } }
public void Dispose() { if (_Playing) { Stop(); } _Instance.Dispose(); _Instance = null; _Stream.Close(); _Stream = null; }
public void ResetStreamPosition() { if (stream != null) { stream.Position = 0; MP3Stream mp3 = stream as MP3Stream; if (mp3 != null) { mp3.IsEOF = false; } } }
public MP3File(Stream Data) { if (FileManager.IsLinux) { m_Stream = new MP3Stream(Data); Channels = m_Stream.ChannelCount; } else //Used by NAudio, only on Windows. { RFullyStream = new ReadFullyStream(Data); } }
protected override void BeforePlay() { if (m_Playing) { Stop(); } m_Stream = new MP3Stream(Path, NUMBER_OF_PCM_BYTES_TO_READ_PER_CHUNK); Frequency = m_Stream.Frequency; m_Playing = true; }
AudioClip GetAudioClipFromMP3ByteArray(byte[] in_aMP3Data) { try { AudioClip l_oAudioClip = null; Stream l_oByteStream = new MemoryStream(in_aMP3Data); MP3Stream l_oMP3Stream = new MP3Stream(l_oByteStream); //Get the converted stream data MemoryStream l_oConvertedAudioData = new MemoryStream(); byte[] l_aBuffer = new byte[2048]; int l_nBytesReturned = -1; int l_nTotalBytesReturned = 0; while (l_nBytesReturned != 0) { l_nBytesReturned = l_oMP3Stream.Read(l_aBuffer, 0, l_aBuffer.Length); l_oConvertedAudioData.Write(l_aBuffer, 0, l_nBytesReturned); l_nTotalBytesReturned += l_nBytesReturned; } Debug.Log("MP3 file has " + l_oMP3Stream.ChannelCount + " channels with a frequency of " + l_oMP3Stream.Frequency); byte[] l_aConvertedAudioData = l_oConvertedAudioData.ToArray(); Debug.Log("Converted Data has " + l_aConvertedAudioData.Length + " bytes of data"); //Convert the byte converted byte data into float form in the range of 0.0-1.0 float[] l_aFloatArray = new float[l_aConvertedAudioData.Length / 2]; for (int i = 0; i < l_aFloatArray.Length; i++) { if (BitConverter.IsLittleEndian) { //Evaluate earlier when pulling from server and/or local filesystem - not needed here //Array.Reverse( l_aConvertedAudioData, i * 2, 2 ); } //Yikes, remember that it is SIGNED Int16, not unsigned (spent a bit of time before realizing I screwed this up...) l_aFloatArray[i] = (float)(BitConverter.ToInt16(l_aConvertedAudioData, i * 2) / 32768.0f); } //For some reason the MP3 header is readin as single channel despite it containing 2 channels of data (investigate later) l_oAudioClip = AudioClip.Create("MySound", l_aFloatArray.Length, 2, l_oMP3Stream.Frequency, false, false); l_oAudioClip.SetData(l_aFloatArray, 0); return(l_oAudioClip); } catch (Exception e) { Debug.Log(e); outText.text = e.Message; } return(null); }
public int BufferSize = 16384 * 4; //Changed to this - should be enough to hold 4 frames. public MP3File(string Path) { if (FileManager.IsLinux) { m_Stream = new MP3Stream(File.Open(Path, FileMode.Open, FileAccess.ReadWrite)); } else //Used by NAudio, only on Windows. { RFullyStream = new ReadFullyStream(File.Open(Path, FileMode.Open, FileAccess.ReadWrite)); m_BufWavProvider = new BufferedWaveProvider(new WaveFormat(new BinaryReader(RFullyStream))); } }
public void Dispose() { if (m_Playing) { Stop(); } m_Instance.Dispose(); m_Instance = null; m_Stream.Close(); m_Stream = null; }
public MP3File(Stream Data) { if (FileManager.IsLinux) { m_Stream = new MP3Stream(Data); Channels = m_Stream.ChannelCount; } else //Used by NAudio, only on Windows. { RFullyStream = new ReadFullyStream(Data); m_BufWavProvider = new BufferedWaveProvider(new WaveFormat(new BinaryReader(RFullyStream))); } }
public T FromStream <T>(Stream stream) where T : class { if (typeof(T) != typeof(SoundEffect)) { throw AssetLoadException.FromInvalidReader <MP3Reader, T>(); } using var mp3Stream = new MP3Stream(stream); using var ms = new MemoryStream(); mp3Stream.CopyTo(ms); return(new SoundEffect(ms.ToArray(), mp3Stream.Frequency, (AudioChannels)mp3Stream.ChannelCount) as T); }
public async Task <PCM16Audio> ReadFileAsync(string filename) { byte[] mp3data = File.ReadAllBytes(filename); using (var output = new MemoryStream()) using (var input = new MemoryStream(mp3data, false)) using (var mp3 = new MP3Stream(input)) { await mp3.CopyToAsync(output); byte[] array = output.ToArray(); short[] samples = ToUInt16Array(array); return(new PCM16Audio(mp3.ChannelCount, mp3.Frequency, samples) { OriginalMP3 = mp3data }); } }
public static SoundStats <byte> LoadMP3SharpData(string fileName) { //NOTE: the Mp3Sharp decoder library only deals with 16bit mp3 files. Which is 99% of what is used now days anyways var result = new SoundStats <byte>(); var reader = new MP3Stream(fileName); result.SampleRate = reader.Frequency; result.Channels = reader.ChannelCount; var dataResult = new List <byte>(); const byte bitsPerSample = 16; const byte bytesPerSample = bitsPerSample / 8; byte[] buffer = new byte[reader.ChannelCount * reader.Frequency * bytesPerSample]; while (reader.Read(buffer, 0, buffer.Length) > 0) { dataResult.AddRange(buffer); } //TODO: Need to test this out with 8 bit. //Will probably have to use the constant 4f for 16bit and 2f for 8bit //This calculate is also not completely accurate. It comes out to 1 second longer //thent he sound actually is. result.TotalSeconds = dataResult.Count / 4f / reader.Frequency; if (reader.Format == SoundFormat.Pcm16BitMono) { result.Format = AudioFormat.Mono16; } else if (reader.Format == SoundFormat.Pcm16BitStereo) { result.Format = AudioFormat.Stereo16; } result.BufferData = dataResult.ToArray(); return(result); }
protected override void BeforePlay() { if (m_Playing) { Stop(); } try { m_Stream = new MP3Stream(Path, NUMBER_OF_PCM_BYTES_TO_READ_PER_CHUNK); Frequency = m_Stream.Frequency; m_Playing = true; } catch (Exception e) { // file in use or access denied. Tracer.Error(e); m_Playing = false; } }
public void MP3Stream_read_md5() { var md5 = System.Security.Cryptography.MD5.Create(); using (var mp3 = Assembly.GetExecutingAssembly().GetManifestResourceStream("MP3Sharp.UnitTests.sample.mp3")) using (var stream = new MP3Stream(mp3)) using (var memory = new System.IO.MemoryStream()) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) { memory.Write(buffer, 0, bytesRead); } byte[] md5hash = md5.ComputeHash(memory.ToArray()); Assert.AreEqual(9576522144988971710UL, BitConverter.ToUInt64(md5hash, 0)); Assert.AreEqual(4012292588662891826UL, BitConverter.ToUInt64(md5hash, 8)); } }
private static double[] LoadMP3AsArray(String fileName = "Music\\Sample.mp3") { MP3Stream mp3 = new MP3Stream(fileName: fileName); byte[] buffer = new byte[4096]; int bytesReturned = 1; int totalBytesRead = 0; ArrayList wav = new ArrayList(); int Sum; #region :obsoleted full-sized extraction //byte[] wav = new byte[0]; //byte[] wav_new; #endregion while (bytesReturned > 0) { Sum = 0x00; bytesReturned = mp3.Read(buffer, 0, buffer.Length); totalBytesRead += bytesReturned; foreach (byte b in buffer) { Sum += (int)b; } wav.Add(Sum / buffer.Length); #region :obsoleted full-sized extraction //wav_new = new byte[wav.Length + buffer.Length]; //wav.CopyTo(wav_new, 0); //buffer.CopyTo(wav_new, wav.Length); //wav = wav_new; #endregion } mp3.Close(); int[] wav_array_int = (int[])wav.ToArray(typeof(int)); double[] wav_array = new double[wav_array_int.Length]; for (int i = 0; i < wav_array_int.Length; i++) { wav_array[i] = (double)wav_array_int[i]; } return(wav_array); }
public ISound Decode(Stream stream) { var inputStream = new MP3Stream(stream); var channels = inputStream.ChannelCount; var rate = inputStream.Frequency; var data = inputStream.ReadAllBytes(); var result = new Sound { Samples = data .Deinterleave(2, channels) .Select(bytes => new Sample { Data = bytes.AsArray().Fuse().Select(s => s / 32768f).AsArray(), [NumericData.Rate] = rate, }) .Cast <ISample>() .ToList() }; return(result); }
public StreamingAudio(string file, bool isADF = false) { // Creating AL data // Создание AL данных buffers = AL.GenBuffers(LoopBuffers); source = AL.GenSource(); AL.Source(source, ALSourcef.Pitch, 1f); AL.Source(source, ALSourcef.Gain, 0.5f); AL.Source(source, ALSourceb.Looping, false); AL.Source(source, ALSourceb.SourceRelative, true); readyData = new ConcurrentQueue <byte[]>(); Stream s = new FileStream(file, FileMode.Open, FileAccess.Read); if (isADF) { s = new AdfStream(s); } stream = new MP3Stream(s); AudioManager.Streaming.Add(this); }