Exemplo n.º 1
0
        private void EncodeTransientFileToMp4OrAMR(TreeNode node)
        {
            ExternalAudioMedia extMedia = m_ExternalAudioMediaList[0];

            AudioLib.WavFormatConverter formatConverter = new WavFormatConverter(true, DisableAcmCodecs);
            string sourceFilePath = base.GetCurrentAudioFileUri().LocalPath;

            string extension = EncodingFileFormat == AudioFileFormats.MP4 ? DataProviderFactory.AUDIO_MP4_EXTENSION :
                               EncodingFileFormat == AudioFileFormats.AMR ? DataProviderFactory.AUDIO_AMR_EXTENSION :
                               DataProviderFactory.AUDIO_3GPP_EXTENSION;
            string destinationFilePath = Path.Combine(base.DestinationDirectory.LocalPath,
                                                      Path.GetFileNameWithoutExtension(sourceFilePath) + extension);

            //reportProgress(m_ProgressPercentage, String.Format(UrakawaSDK_daisy_Lang.CreateMP3File, Path.GetFileName(destinationFilePath), GetSizeInfo(m_RootNode)));

            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);

            AddSubCancellable(formatConverter);

            bool result = false;

            try
            {
                result = formatConverter.CompressWavToMP4And3GP(sourceFilePath, destinationFilePath, pcmFormat, BitRate_Encoding);
            }
            finally
            {
                RemoveSubCancellable(formatConverter);
            }

            if (RequestCancellation)
            {
                m_ExternalAudioMediaList.Clear();
                return;
            }

            if (result)
            {
                m_EncodingFileCompressionRatio = (new FileInfo(sourceFilePath).Length) / (new FileInfo(destinationFilePath).Length);

                foreach (ExternalAudioMedia ext in m_ExternalAudioMediaList)
                {
                    if (ext != null)
                    {
                        ext.Src = ext.Src.Replace(DataProviderFactory.AUDIO_WAV_EXTENSION, extension);
                    }
                }

                File.Delete(sourceFilePath);
            }
            else
            {
                // append error messages
                base.ErrorMessages = base.ErrorMessages + String.Format(UrakawaSDK_daisy_Lang.ErrorInEncoding, Path.GetFileName(sourceFilePath));
            }

            m_ExternalAudioMediaList.Clear();
        }
Exemplo n.º 2
0
        /// <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");
            }
        }
Exemplo n.º 3
0
        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();
        }