public static Wave FromArray(byte[] Bytes) { Wave Result = new Wave(); byte[] FileBytes = Bytes; if (Bytes.Length == 0) return Result; try { Result.FileHeader.ChunkID = GetDataFromByteArray(FileBytes, 0, 0, 4); Result.FileHeader.ChunkSize = BitConverter.ToUInt32(FileBytes, 4); Result.FileHeader.Format = GetDataFromByteArray(FileBytes, 0, 8, 4); Result.FileFormatSubChunk.Subchunk1ID = GetDataFromByteArray(FileBytes, 0, 12, 4); Result.FileFormatSubChunk.Subchunk1Size = BitConverter.ToUInt32(FileBytes, 16); Result.FileFormatSubChunk.AudioFormat = BitConverter.ToUInt16(FileBytes, 20); Result.FileFormatSubChunk.NumChannels = BitConverter.ToUInt16(FileBytes, 22); Result.FileFormatSubChunk.SampleRate = BitConverter.ToUInt32(FileBytes, 24); Result.FileFormatSubChunk.ByteRate = BitConverter.ToUInt32(FileBytes, 28); Result.FileFormatSubChunk.BlockAlign = BitConverter.ToUInt16(FileBytes, 32); Result.FileFormatSubChunk.BitsPerSample = BitConverter.ToUInt16(FileBytes, 34); Result.FileDataSubChunk.Subchunk2ID = GetDataFromByteArray(FileBytes, 0, 36, 4); Result.FileDataSubChunk.Subchunk2Size = BitConverter.ToUInt32(FileBytes, 40); Result.FileDataSubChunk.Data = GetDataFromByteArray(FileBytes, 0, 44, Result.FileDataSubChunk.Subchunk2Size); return Result; } catch { Result = new Wave(); return Result; } }
public static Wave FromFile(string FileName) { Wave Result = new Wave(); if (!File.Exists(FileName)) return Result; byte[] FileBytes = File.ReadAllBytes(FileName); try { Result.FileHeader.ChunkID = GetDataFromByteArray(FileBytes, 0, 0, 4); Result.FileHeader.ChunkSize = BitConverter.ToUInt32(FileBytes, 4); Result.FileHeader.Format = GetDataFromByteArray(FileBytes, 0, 8, 4); Result.FileFormatSubChunk.Subchunk1ID = GetDataFromByteArray(FileBytes, 0, 12, 4); Result.FileFormatSubChunk.Subchunk1Size = BitConverter.ToUInt32(FileBytes, 16); Result.FileFormatSubChunk.AudioFormat = BitConverter.ToUInt16(FileBytes, 20); Result.FileFormatSubChunk.NumChannels = BitConverter.ToUInt16(FileBytes, 22); Result.FileFormatSubChunk.SampleRate = BitConverter.ToUInt32(FileBytes, 24); Result.FileFormatSubChunk.ByteRate = BitConverter.ToUInt32(FileBytes, 28); Result.FileFormatSubChunk.BlockAlign = BitConverter.ToUInt16(FileBytes, 32); Result.FileFormatSubChunk.BitsPerSample = BitConverter.ToUInt16(FileBytes, 34); Result.FileDataSubChunk.Subchunk2ID = GetDataFromByteArray(FileBytes, 0, 36, 4); Result.FileDataSubChunk.Subchunk2Size = BitConverter.ToUInt32(FileBytes, 40); Result.FileDataSubChunk.Data = GetDataFromByteArray(FileBytes, 0, 44, Result.FileDataSubChunk.Subchunk2Size); return Result; } catch { return null; } }
/// <summary> /// Load audio from a wave object /// </summary> /// <param name="Title">Title of the audio</param> /// <param name="Author">Author of the audio</param> /// <param name="WaveObject">Wave object</param> /// <returns></returns> public static Audio FromWave(string Title, string Author, Wave WaveObject) { return FromWave(Title, Author, "", WaveObject); }
/// <summary> /// Load audio from a wave object /// </summary> /// <param name="Title">Title of the audio</param> /// <param name="Author">Author of the audio</param> /// <param name="Comment">Comment to the audio</param> /// <param name="WaveObject">Wave object</param> /// <returns></returns> public static Audio FromWave(string Title, string Author, string Comment, Wave WaveObject) { // Get speed ushort speed = (ushort)WaveObject.FileFormatSubChunk.SampleRate; // Allocate space for the samples byte[] samples = new byte[WaveObject.FileDataSubChunk.Data.Length / WaveObject.FileFormatSubChunk.NumChannels]; // Allocate counter variables uint sourceSample = 0; uint targetSample = 0; byte byteOfSample = 0; // Copy over samples (only channel 0 - ignoring other channels) while (targetSample < samples.Length) { samples[targetSample] = WaveObject.FileDataSubChunk.Data[sourceSample]; // copy wave sample over targetSample++; // advance target pointer if (byteOfSample == 0) { sourceSample++; // advance source pointer byteOfSample++; // advance byte of sample pointer } else { sourceSample += (ushort)((WaveObject.FileFormatSubChunk.NumChannels - 1) * 2 + 1); // 1 channel (mono) is output, therefore skip all samples of the other channels byteOfSample = 0; // reset byte of sample pointer and start with the next sample } } // Return new audio object return new Audio(Title, Author, Comment, speed, samples); }
/// <summary> /// Load audio from a wave object /// </summary> /// <param name="WaveObject">Wave object</param> /// <returns></returns> public static Audio FromWave(Wave WaveObject) { return FromWave("", "", WaveObject); }