private static WaveFile MixWave(WaveFile wf1, WaveFile wf2, double fadeTime) { int FadeSamples = (int)(fadeTime * wf1.ByteRate / wf1.NumChannels); // number of samples affected by the crossfading int FadeBytes = (int)(fadeTime * wf1.ByteRate); // number of affected bytes WaveFile Result = new WaveFile(); // resulting Wave file Result.FileSize = wf1.FileSize + wf2.DataSize - 2 * FadeBytes; // file size Result.Format = "WAVE"; // copy information from the fmt chunk Result.FmtChunkSize = wf1.FmtChunkSize; Result.AudioFormat = wf1.AudioFormat; Result.NumChannels = wf1.NumChannels; Result.SampleRate = wf1.SampleRate; Result.ByteRate = wf1.ByteRate; Result.BlockAlign = wf1.BlockAlign; Result.BitsPerSample = wf1.BitsPerSample; Result.DataSize = wf1.DataSize + wf2.DataSize - 2 * FadeBytes; // new size of the data chunk Result.Data = new int[wf1.NumChannels][]; // copy number of channels int NumSamples = Result.DataSize / (Result.NumChannels * (Result.BitsPerSample / 8)); // number of samples in resulting file // initialize data arrays for all samples and channels for (int i = 0; i < Result.Data.Length; i++) { Result.Data[i] = new int[NumSamples]; } int PosCounter = 0; // position of the current sample in the Wave file // copy the samples from the first Wave file into the data field of the result file for (int i = 0; i < wf1.Data[0].Length; i++) { // copy the current sample for all channels for (int j = 0; j < wf1.NumChannels; j++) { // if the current sample is in the crossfading time, mix the amplitude value of the 1st file with the amplitude value of the 2nd file if (i > wf1.Data[0].Length - FadeSamples) { Result.Data[j][PosCounter] = (int)(wf1.Data[j][i] * Factor(i - (wf1.Data[0].Length - FadeSamples), FadeSamples, 0) + wf2.Data[j][i - (wf1.Data[0].Length - FadeSamples)] * Factor(i - (wf1.Data[0].Length - FadeSamples), FadeSamples, 1)); //Result.Data[j][PosCounter] = (int)(wf1.Data[j][i] + wf2.Data[j][i - (wf1.Data[0].Length - FadeSamples)]); } else Result.Data[j][PosCounter] = wf1.Data[j][i]; } PosCounter++; } // copy the remaining samples for (int i = FadeSamples; i < wf2.Data[0].Length; i++) { for (int j = 0; j < wf1.NumChannels; j++) { Result.Data[j][PosCounter] = wf2.Data[j][i]; } PosCounter++; } return Result; }
int SampleRate; // 6 #endregion Fields #region Methods public static void StoreMixWave(string path, WaveFile wf1, WaveFile wf2, double fadeTime) { WaveFile Mixed = MixWave(wf1, wf2, fadeTime); // Ergebnisdatei mischen Mixed.StoreWave(path); // Ergebnisdatei auf Festplatte speichern }
private void backgroundWorker4_DoWork(object sender, DoWorkEventArgs e) { while (bgw4_done < aantal_liedjes) { if (bgw3_done > 0) { if (bgw3_done > bgw4_done || bgw3_done == aantal_liedjes) { if (bgw4_done == 0) { System.IO.File.Copy(File.defaultDir + "\\faded\\" + (bgw4_done + 1) + ".wav", File.defaultDir + "\\total\\total.wav", true); bgw4_done += 1; } else { if (System.IO.File.Exists(File.defaultDir + "\\faded\\" + (bgw4_done + 1).ToString() + ".wav")) { //MusicEditor.MixMp3(File.defaultDir + "\\total\\total.mp3", File.defaultDir + "\\faded\\" + (bgw4_done + 1) + ".mp3", (liedjes[bgw4_done - 1].FadeOut - new DateTime(2000, 1, 1)).TotalSeconds, File.defaultDir + "\\total\\total2.mp3"); WaveFile wf1 = new WaveFile(); wf1.LoadWave(File.defaultDir + "\\total\\total.wav"); WaveFile wf2 = new WaveFile(); wf2.LoadWave(File.defaultDir + "\\faded\\" + (bgw4_done + 1) + ".wav"); WaveFile.StoreMixWave(File.defaultDir + "\\total\\total2.wav", wf1, wf2, (liedjes[bgw4_done - 1].FadeOut - new DateTime(2000, 1, 1)).TotalSeconds); System.IO.File.Delete(File.defaultDir + "\\total\\total.wav"); System.IO.File.Copy(File.defaultDir + "\\total\\total2.wav", File.defaultDir + "\\total\\total.wav"); System.IO.File.Delete(File.defaultDir + "\\total\\total2.wav"); bgw4_done += 1; } else bgw4_done += 1; } backgroundWorker4.ReportProgress(1); } } } }