/// <summary> /// takes wav file / mp3 file as input and converts it to wav file with audio format info supplied as parameter /// </summary> /// <param name="SourceFilePath"></param> /// <param name="destinationDirectory"></param> /// <param name="destinationFormatInfo"></param> /// <returns> full file path of converted file </returns> private string ConvertToDefaultFormat(string SourceFilePath, string destinationDirectory, PCMFormatInfo destinationFormatInfo, bool skipACM) { if (!File.Exists(SourceFilePath)) { throw new FileNotFoundException(SourceFilePath); } if (!Directory.Exists(destinationDirectory)) { FileDataProvider.CreateDirectory(destinationDirectory); } AudioFileType sourceFileType = GetAudioFileType(SourceFilePath); switch (sourceFileType) { case AudioFileType.WavUncompressed: case AudioFileType.WavCompressed: { if (FirstDiscoveredPCMFormat == null) { Stream wavStream = null; try { wavStream = File.Open(SourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); uint dataLength; AudioLibPCMFormat pcmInfo = null; pcmInfo = AudioLibPCMFormat.RiffHeaderParse(wavStream, out dataLength); //FirstDiscoveredPCMFormat = new PCMFormatInfo(pcmInfo); FirstDiscoveredPCMFormat = new AudioLibPCMFormat(); FirstDiscoveredPCMFormat.CopyFrom(pcmInfo); } finally { if (wavStream != null) { wavStream.Close(); } } } WavFormatConverter formatConverter1 = new WavFormatConverter(true, skipACM); AddSubCancellable(formatConverter1); // Preserve existing WAV PCM format, the call below to ConvertSampleRate detects the equality of PCM formats and copies the audio file instead of resampling. AudioLibPCMFormat pcmFormat = m_autoDetectPcmFormat ? FirstDiscoveredPCMFormat : (destinationFormatInfo != null ? destinationFormatInfo.Data : new AudioLibPCMFormat()); string result = null; try { AudioLibPCMFormat originalPcmFormat; result = formatConverter1.ConvertSampleRate(SourceFilePath, destinationDirectory, pcmFormat, out originalPcmFormat); if (originalPcmFormat != null && FirstDiscoveredPCMFormat != null) { DebugFix.Assert(FirstDiscoveredPCMFormat.Equals(originalPcmFormat)); } } finally { RemoveSubCancellable(formatConverter1); } return(result); } case AudioFileType.Mp4_AAC: case AudioFileType.Mp3: { WavFormatConverter formatConverter2 = new WavFormatConverter(true, skipACM); AddSubCancellable(formatConverter2); string result = null; try { AudioLibPCMFormat pcmFormat = m_autoDetectPcmFormat ? FirstDiscoveredPCMFormat : // can be null! (destinationFormatInfo != null ? destinationFormatInfo.Data : new AudioLibPCMFormat()); AudioLibPCMFormat originalPcmFormat; if (sourceFileType == AudioFileType.Mp3) { result = formatConverter2.UnCompressMp3File(SourceFilePath, destinationDirectory, pcmFormat, out originalPcmFormat); } else { DebugFix.Assert(sourceFileType == AudioFileType.Mp4_AAC); result = formatConverter2.UnCompressMp4_AACFile(SourceFilePath, destinationDirectory, pcmFormat, out originalPcmFormat); } if (originalPcmFormat != null) { if (FirstDiscoveredPCMFormat == null) { //FirstDiscoveredPCMFormat = new PCMFormatInfo(originalPcmFormat); FirstDiscoveredPCMFormat = new AudioLibPCMFormat(); FirstDiscoveredPCMFormat.CopyFrom(originalPcmFormat); } } } finally { RemoveSubCancellable(formatConverter2); } return(result); } default: throw new Exception("Source file format not supported"); } }
private void StartConversion () { m_btn_Add.Enabled = false; m_btn_Browse.Enabled = false; m_btn_Start.Enabled = false; m_btnReset.Enabled = false; m_btnDelete.Enabled = false; m_cb_channel.Enabled = false; m_cb_sampleRate.Enabled = false; IWavFormatConverter audioConverter = new WavFormatConverter ( true ); int samplingRate = int.Parse ( m_cb_sampleRate.SelectedItem.ToString () ); int channels = m_cb_channel.SelectedIndex + 1; int bitDepth = 16; string outputDirectory = m_txt_Browse.Text; string convertedFilePath = null; bool flag = false; if (!Directory.Exists ( outputDirectory )) return; int listPositionIndex = 0; while (m_lb_addFiles.Items.Count > listPositionIndex && listPositionIndex < 50) { string filePath = (string)m_lb_addFiles.Items[listPositionIndex]; //MessageBox.Show ( filePath ); try { if (Path.GetExtension ( filePath ) == ".wav") { convertedFilePath = audioConverter.ConvertSampleRate ( filePath, outputDirectory, channels, samplingRate, bitDepth ); } else if (Path.GetExtension ( filePath ) == ".mp3") { convertedFilePath = audioConverter.UnCompressMp3File ( filePath, outputDirectory, channels, samplingRate, bitDepth ); } // rename converted file to appropriate name string newFilePath = Path.Combine ( outputDirectory, Path.GetFileNameWithoutExtension ( filePath ) ) + ".wav"; //MessageBox.Show ( newFilePath ); if (File.Exists ( newFilePath )) { if (MessageBox.Show ( "File: " + Path.GetFileName ( newFilePath ) + " already exists. Do you want to overwrite it?", "Warning", MessageBoxButtons.YesNo ) == DialogResult.Yes) { File.Delete ( newFilePath ); File.Move ( convertedFilePath, newFilePath ); } } else { File.Move ( convertedFilePath, newFilePath ); } m_lb_addFiles.Items.RemoveAt ( 0 ); } catch (System.Exception ex) { flag = true; MessageBox.Show ( ex.ToString () ); listPositionIndex++; } } if (flag == false) { MessageBox.Show("Files have been converted"); } else MessageBox.Show("Some files have not been converted properly"); m_btn_Add.Enabled = true; m_btn_Start.Enabled = false; m_btnDelete.Enabled = false; m_btn_Browse.Enabled = true; m_btnReset.Enabled = true; m_cb_channel.Enabled = true; m_cb_sampleRate.Enabled = true; }
private void EncodeTransientFileResample(TreeNode node) { string sourceFilePath = base.GetCurrentAudioFileUri().LocalPath; //string destinationFilePath = Path.Combine(base.DestinationDirectory.LocalPath, Path.GetFileNameWithoutExtension(sourceFilePath) + "_" + base.EncodePublishedAudioFilesSampleRate + DataProviderFactory.AUDIO_WAV_EXTENSION); //reportProgress(m_ProgressPercentage, String.Format(UrakawaSDK_daisy_Lang.ConvertingAudio,sourceFilePath)); ExternalAudioMedia extMedia = m_ExternalAudioMediaList[0]; PCMFormatInfo audioFormat = extMedia.Presentation.MediaDataManager.DefaultPCMFormat; //AudioLibPCMFormat pcmFormat = audioFormat.Data; AudioLibPCMFormat pcmFormat = new AudioLibPCMFormat(); pcmFormat.CopyFrom(audioFormat.Data); pcmFormat.SampleRate = (ushort)base.EncodePublishedAudioFilesSampleRate; pcmFormat.NumberOfChannels = (ushort)(base.EncodePublishedAudioFilesStereo ? 2 : 1); AudioLib.WavFormatConverter formatConverter = new WavFormatConverter(true, DisableAcmCodecs); AddSubCancellable(formatConverter); string destinationFilePath = null; try { AudioLibPCMFormat originalPcmFormat; destinationFilePath = formatConverter.ConvertSampleRate(sourceFilePath, base.DestinationDirectory.LocalPath, pcmFormat, out originalPcmFormat); if (originalPcmFormat != null) { DebugFix.Assert(audioFormat.Data.Equals(originalPcmFormat)); } } finally { RemoveSubCancellable(formatConverter); } //string sourceName = Path.GetFileNameWithoutExtension(sourceFilePath); //string destName = Path.GetFileNameWithoutExtension(destinationFilePath); //foreach (ExternalAudioMedia ext in m_ExternalAudioMediaList) //{ //if (ext != null) //{ //ext.Src = ext.Src.Replace(sourceName, destName); //} //} File.Delete(sourceFilePath); File.Move(destinationFilePath, sourceFilePath); try { File.SetAttributes(sourceFilePath, FileAttributes.Normal); } catch { } m_ExternalAudioMediaList.Clear(); }
public static string ConvertedFile(string filePath, Presentation pres) { AudioLib.WavFormatConverter audioConverter = new WavFormatConverter(true, true); int samplingRate = (int)pres.MediaDataManager.DefaultPCMFormat.Data.SampleRate; int channels = pres.MediaDataManager.DefaultPCMFormat.Data.NumberOfChannels; int bitDepth = pres.MediaDataManager.DefaultPCMFormat.Data.BitDepth; string directoryPath = pres.DataProviderManager.DataFileDirectoryFullPath; string convertedFile = null; try { if (Path.GetExtension(filePath).ToLower() == ".wav") { Stream wavStream = null; wavStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); uint dataLength; AudioLibPCMFormat newFilePCMInfo = AudioLibPCMFormat.RiffHeaderParse(wavStream, out dataLength); if (wavStream != null) { wavStream.Close(); } if (newFilePCMInfo.SampleRate == samplingRate && newFilePCMInfo.NumberOfChannels == channels && newFilePCMInfo.BitDepth == bitDepth) { convertedFile = filePath; } else { AudioLibPCMFormat pcmFormat = new AudioLibPCMFormat((ushort)channels, (uint)samplingRate, (ushort)bitDepth); AudioLibPCMFormat originalPCMFormat = null; convertedFile = audioConverter.ConvertSampleRate(filePath, directoryPath, pcmFormat, out originalPCMFormat); } } else if (Path.GetExtension(filePath).ToLower() == ".mp3") { AudioLibPCMFormat pcmFormat = new AudioLibPCMFormat((ushort)channels, (uint)samplingRate, (ushort)bitDepth); AudioLibPCMFormat originalPCMFormat = null; convertedFile = audioConverter.UnCompressMp3File(filePath, directoryPath, pcmFormat, out originalPCMFormat); } else if (Path.GetExtension(filePath).ToLower() == ".mp4" || Path.GetExtension(filePath).ToLower() == ".m4a") { AudioLibPCMFormat pcmFormat = new AudioLibPCMFormat((ushort)channels, (uint)samplingRate, (ushort)bitDepth); AudioLibPCMFormat originalPCMFormat = null; convertedFile = audioConverter.UnCompressMp4_AACFile(filePath, directoryPath, pcmFormat, out originalPCMFormat); } else { MessageBox.Show(string.Format(Localizer.Message("AudioFormatConverter_Error_FileExtentionNodSupported"), filePath), Localizer.Message("Caption_Error")); return(null); } // rename converted file to original file if names are different if (Path.GetFileName(filePath) != Path.GetFileName(convertedFile)) { string newConvertedFilePath = Path.Combine(Path.GetDirectoryName(convertedFile), Path.GetFileNameWithoutExtension(filePath) + ".wav"); for (int i = 0; i < 99999 && File.Exists(newConvertedFilePath); i++) { newConvertedFilePath = Path.Combine(Path.GetDirectoryName(convertedFile), i.ToString() + Path.GetFileNameWithoutExtension(filePath) + ".wav"); if (!File.Exists(newConvertedFilePath)) { MessageBox.Show(string.Format(Localizer.Message("Import_AudioFormat_RenameFile"), Path.GetFileNameWithoutExtension(filePath) + ".wav", Path.GetFileName(newConvertedFilePath)), Localizer.Message("Caption_Information"), MessageBoxButtons.OK); break; } } File.Move(convertedFile, newConvertedFilePath); convertedFile = newConvertedFilePath; } } catch (System.Exception ex) { MessageBox.Show(ex.ToString()); return(null); } return(convertedFile); }