/// <summary> /// Creates a new RIFF header for an audio stream. /// </summary> /// <param name="sampleRate">The sample rate for the header.</param> /// <param name="bitRate">The bit rate for the header.</param> /// <returns>The constructed RIFF header.</returns> public RIFFHeader CreateHeader(int sampleRate, short bitRate) { RIFFHeader hdr = new RIFFHeader(); hdr.ChunkID = Encoding.ASCII.GetBytes("RIFF"); hdr.RiffType = Encoding.ASCII.GetBytes("WAVE"); hdr.FmtID = Encoding.ASCII.GetBytes("fmt "); hdr.SubChunkID = Encoding.ASCII.GetBytes("data"); hdr.SubChunkSize = 0; hdr.FileSize = 0; hdr.FmtSize = 16; hdr.FmtCode = 1; hdr.Channels = 1; hdr.SampleRate = sampleRate; hdr.BitsPerSample = bitRate; hdr.BlockAlign = (short)(hdr.Channels * bitRate / 8); hdr.ByteRate = sampleRate * hdr.BlockAlign; return(hdr); }
/// <summary> /// Saves a wave file. /// </summary> /// <param name="data">The data to save.</param> /// <param name="hdr">The RIFF header of the data.</param> public void SaveWave(List <byte> data, RIFFHeader hdr) { using (FileStream fs = new FileStream(fileName, FileMode.Create)) { using (BinaryWriter writer = new BinaryWriter(fs)) { writer.Write(hdr.ChunkID); writer.Write(Marshal.SizeOf(hdr) + data.Count); writer.Write(hdr.RiffType); writer.Write(hdr.FmtID); writer.Write(hdr.FmtSize); writer.Write(hdr.FmtCode); writer.Write(hdr.Channels); writer.Write(hdr.SampleRate); writer.Write(hdr.ByteRate); writer.Write(hdr.BlockAlign); writer.Write(hdr.BitsPerSample); writer.Write(hdr.SubChunkID); writer.Write(data.Count); writer.Write(data.ToArray()); } } }
/// <summary> /// Pastes the copied data from the clipboard to the selected position in the time domain. /// If the copied data has two channels, it will paste the right channel to the right channel if the window to paste in has a right channel. /// If the copied data has one channel, and the window to paste in has two channels, it will paste the single channel into both channels. /// </summary> /// <param name="window">The audio panel that the time domain is in.</param> /// <param name="copyData">The copied data on the clipboard.</param> /// <param name="dualChannel">Whether the audio stream has two channels or not.</param> public static void Paste(AudioPanel window, ClipboardData copyData, bool dualChannel) { // Paste to the selected position if the time domain is not empty if (window.LChannel != null) { // Gets the position of the selection int cursorPos = (int)window.LTimeChannel.ChartArea.CursorX.Position; // Maps the selected position to and index in the data int pastePos; if (cursorPos < window.LTimeChannel.Series.Points.Count) { pastePos = (int)window.LTimeChannel.Series.Points[cursorPos].XValue; } else { pastePos = (int)window.LTimeChannel.Series.Points[window.LTimeChannel.Series.Points.Count - 1].XValue; } List <short> data = new List <short>(copyData.LChannel); // Converts bit rate if needed if (copyData.Header.BitsPerSample > window.Header.BitsPerSample) { for (int i = 0; i < data.Count; i++) { data[i] = BitMapper.To8Bit(data[i]); } } else if (copyData.Header.BitsPerSample < window.Header.BitsPerSample) { for (int i = 0; i < data.Count; i++) { data[i] = BitMapper.To16Bit(data[i]); } } // Inserts the data at the selected position window.LChannel.InsertRange(pastePos, data); if (dualChannel) { if (copyData.RChannel != null) { data = new List <short>(copyData.RChannel); if (copyData.Header.BitsPerSample > window.Header.BitsPerSample) { for (int i = 0; i < data.Count; i++) { data[i] = BitMapper.To8Bit(data[i]); } } else if (copyData.Header.BitsPerSample < window.Header.BitsPerSample) { for (int i = 0; i < data.Count; i++) { data[i] = BitMapper.To16Bit(data[i]); } } } window.RChannel.InsertRange(pastePos, data); } // Create a new header and paste the data if the time domain is empty } else { // Creates the new RIFF header for the data RIFFHeader hdr = copyData.Header; // Copies the data to the channels window.LChannel = copyData.LChannel; if (copyData.RChannel != null) { if (!dualChannel) { window.AddRChannel(); } window.RChannel = copyData.RChannel; } else { if (dualChannel) { window.RChannel = copyData.LChannel; hdr.Channels = 2; } } window.RawData = window.DataToBytes(); window.MenuStrip.InfoLabel.Text = hdr.SampleRate + " Sample rate" + " | " + hdr.BitsPerSample + " Bit rate"; //Assigns new header to the window hdr.SubChunkSize = window.RawData.Count; window.Header = hdr; } }