Exemplo n.º 1
0
        // Access a channel which we know exist and is the only channel by this name.
        //sdk2
        //internal Channel GetSingleChannelByName(string name)
        //{
        //    List<Channel> channels = getChannelsManager().getListOfChannels(name);
        //    if (channels.Count == 0) throw new Exception(String.Format("No channel named \"{0}\"", name));
        //    if (channels.Count > 1) throw new Exception(String.Format("Expected 1 channel for {0}, got {1}.",
        //        name, channels.Count));
        //    return channels[0];
        //}

        // Create a media object from a sound file.
        private ManagedAudioMedia ImportAudioFromFile(string path)
        {
            string dataProviderDirectory = DataProviderManager.DataFileDirectoryFullPath;

            //EnforceSinglePCMFormat is always true
            //if (!MediaDataManager.EnforceSinglePCMFormat)
            //    {
            //    Stream input = File.OpenRead ( path );
            //    PCMDataInfo info = PCMDataInfo.parseRiffWaveHeader ( input );
            //    input.Close ();
            //    DataManager.setDefaultBitDepth ( info.getBitDepth () );
            //    DataManager.setDefaultNumberOfChannels ( info.getNumberOfChannels () );
            //    DataManager.setDefaultSampleRate ( info.getSampleRate () );
            //    DataManager.setEnforceSinglePCMFormat ( true );
            //    }

            AudioMediaData data = MediaDataFactory.CreateAudioMediaData();

            if (Path.GetFullPath(path).StartsWith(Path.GetFullPath(dataProviderDirectory)))
            {
                FileDataProvider dataProv = (FileDataProvider)DataProviderFactory.Create(urakawa.data.DataProviderFactory.AUDIO_WAV_MIME_TYPE);
                dataProv.InitByMovingExistingFile(path);
                data.AppendPcmData(dataProv);
            }
            else
            {
                data.AppendPcmData_RiffHeader(path);
            }

            ManagedAudioMedia media = MediaFactory.CreateManagedAudioMedia();

            media.AudioMediaData = data;
            return(media);
        }
Exemplo n.º 2
0
        public event FinishingPageHandler FinishingPage;        // finishing a page

        private void OnAudioRecordingFinished(object sender, AudioRecorder.AudioRecordingFinishEventArgs e)
        {
            mRecorder.AudioRecordingFinished -= OnAudioRecordingFinished;
            bool deleteAfterInsert = true;

            if (deleteAfterInsert)
            {
                FileDataProvider dataProv = (FileDataProvider)mSessionMedia.Presentation.DataProviderFactory.Create(DataProviderFactory.AUDIO_WAV_MIME_TYPE);
                dataProv.InitByMovingExistingFile(e.RecordedFilePath);
                mSessionMedia.AudioMediaData.AppendPcmData(dataProv);
            }
            else
            {
                // TODO: progress ! (time consuming file copy)
                mSessionMedia.AudioMediaData.AppendPcmData_RiffHeader(e.RecordedFilePath);
            }

            if (deleteAfterInsert && File.Exists(e.RecordedFilePath))     //check exist just in case file adopted by DataProviderManager
            {
                File.Delete(e.RecordedFilePath);
            }
            m_PhDetectorEstimatedBytesRecorded = 0;
        }
Exemplo n.º 3
0
        protected void addAudio(TreeNode treeNode, XmlNode xmlNode, bool isSequence, string fullSmilPath)
        {
            if (RequestCancellation)
            {
                return;
            }

            string dirPath = Path.GetDirectoryName(fullSmilPath);

            XmlAttributeCollection audioAttrs = xmlNode.Attributes;

            if (audioAttrs == null || audioAttrs.Count == 0)
            {
                return;
            }
            XmlNode audioAttrSrc = audioAttrs.GetNamedItem("src");

            if (audioAttrSrc == null || String.IsNullOrEmpty(audioAttrSrc.Value))
            {
                return;
            }

            string src = FileDataProvider.UriDecode(audioAttrSrc.Value);

            XmlNode audioAttrClipBegin = audioAttrs.GetNamedItem("clipBegin");
            XmlNode audioAttrClipEnd   = audioAttrs.GetNamedItem("clipEnd");

            Presentation      presentation = treeNode.Presentation; // m_Project.Presentations.Get(0);
            ManagedAudioMedia media        = null;



            string fullPath = Path.Combine(dirPath, src);

            fullPath = FileDataProvider.NormaliseFullFilePath(fullPath).Replace('/', '\\');
            addOPF_GlobalAssetPath(fullPath);

            string ext = Path.GetExtension(src);


            if (ext.Equals(DataProviderFactory.AUDIO_WAV_EXTENSION, StringComparison.OrdinalIgnoreCase))
            {
                FileDataProvider dataProv = null;

                if (!File.Exists(fullPath))
                {
                    Debug.Fail("File not found: " + fullPath);
                    media = null;
                }
                else
                {
                    //bool deleteSrcAfterCompletion = false;

                    string fullWavPath = fullPath;

                    FileDataProvider obj;
                    m_OriginalAudioFile_FileDataProviderMap.TryGetValue(fullWavPath, out obj);

                    if (obj != null)  //m_OriginalAudioFile_FileDataProviderMap.ContainsKey(fullWavPath))
                    {
                        if (m_AudioConversionSession.FirstDiscoveredPCMFormat == null)
                        {
                            DebugFix.Assert(obj.Presentation != presentation);

                            Object appData = obj.AppData;

                            DebugFix.Assert(appData != null);

                            if (appData != null && appData is WavClip.PcmFormatAndTime)
                            {
                                m_AudioConversionSession.FirstDiscoveredPCMFormat = new AudioLibPCMFormat();
                                m_AudioConversionSession.FirstDiscoveredPCMFormat.CopyFrom(((WavClip.PcmFormatAndTime)appData).mFormat);
                            }
                        }

                        if (obj.Presentation != presentation)
                        {
                            dataProv = (FileDataProvider)presentation.DataProviderFactory.Create(DataProviderFactory.AUDIO_WAV_MIME_TYPE);


                            reportProgress(-1, String.Format(UrakawaSDK_daisy_Lang.CopyingAudio, Path.GetFileName(obj.DataFileFullPath)));


                            dataProv.InitByCopyingExistingFile(obj.DataFileFullPath);

                            //m_AudioConversionSession.RelocateDestinationFilePath(newfullWavPath, dataProv.DataFileFullPath);

                            m_OriginalAudioFile_FileDataProviderMap.Remove(fullWavPath);
                            m_OriginalAudioFile_FileDataProviderMap.Add(fullWavPath, dataProv);

                            Object appData = obj.AppData;

                            DebugFix.Assert(appData != null);

                            if (appData != null && appData is WavClip.PcmFormatAndTime)
                            {
                                dataProv.AppData = new WavClip.PcmFormatAndTime(((WavClip.PcmFormatAndTime)appData).mFormat, ((WavClip.PcmFormatAndTime)appData).mTime);
                            }
                        }
                        else
                        {
                            dataProv = obj; // m_OriginalAudioFile_FileDataProviderMap[fullWavPath];
                        }
                    }
                    else // create FileDataProvider
                    {
                        Stream wavStream = null;
                        try
                        {
                            wavStream = File.Open(fullWavPath, FileMode.Open, FileAccess.Read, FileShare.Read);

                            uint dataLength;
                            AudioLibPCMFormat pcmInfo = null;

                            pcmInfo = AudioLibPCMFormat.RiffHeaderParse(wavStream, out dataLength);

                            if (m_AudioConversionSession.FirstDiscoveredPCMFormat == null)
                            {
                                //m_AudioConversionSession.FirstDiscoveredPCMFormat = new PCMFormatInfo(pcmInfo);
                                m_AudioConversionSession.FirstDiscoveredPCMFormat = new AudioLibPCMFormat();
                                m_AudioConversionSession.FirstDiscoveredPCMFormat.CopyFrom(pcmInfo);
                            }


                            if (RequestCancellation)
                            {
                                return;
                            }

                            //if (m_firstTimePCMFormat)
                            //{
                            //    presentation.MediaDataManager.DefaultPCMFormat = new PCMFormatInfo(pcmInfo);
                            //    m_firstTimePCMFormat = false;
                            //}

                            if (!presentation.MediaDataManager.DefaultPCMFormat.Data.IsCompatibleWith(pcmInfo))
                            {
                                wavStream.Close();
                                wavStream = null;

                                reportProgress(-1, String.Format(UrakawaSDK_daisy_Lang.ConvertingAudio, Path.GetFileName(fullWavPath)));
                                string newfullWavPath = m_AudioConversionSession.ConvertAudioFileFormat(fullWavPath);

                                if (RequestCancellation)
                                {
                                    return;
                                }



                                dataProv = (FileDataProvider)presentation.DataProviderFactory.Create(DataProviderFactory.AUDIO_WAV_MIME_TYPE);
                                //Console.WriteLine("Source audio file to SDK audio file map (before creating SDK audio file): " + Path.GetFileName(fullWavPath) + " = " + dataProv.DataFileRelativePath);
                                dataProv.InitByMovingExistingFile(newfullWavPath);

                                m_AudioConversionSession.RelocateDestinationFilePath(newfullWavPath, dataProv.DataFileFullPath);

                                m_OriginalAudioFile_FileDataProviderMap.Add(fullWavPath, dataProv);

                                if (RequestCancellation)
                                {
                                    return;
                                }
                            }
                            else // use original wav file by copying it to data directory
                            {
                                dataProv = (FileDataProvider)presentation.DataProviderFactory.Create(DataProviderFactory.AUDIO_WAV_MIME_TYPE);
                                //Console.WriteLine("Source audio file to SDK audio file map (before creating SDK audio file): " + Path.GetFileName(fullWavPath) + " = " + dataProv.DataFileRelativePath);
                                reportProgress(-1, String.Format(UrakawaSDK_daisy_Lang.CopyingAudio, Path.GetFileName(fullWavPath)));
                                dataProv.InitByCopyingExistingFile(fullWavPath);
                                m_OriginalAudioFile_FileDataProviderMap.Add(fullWavPath, dataProv);

                                if (RequestCancellation)
                                {
                                    return;
                                }
                            }
                        }
                        finally
                        {
                            if (wavStream != null)
                            {
                                wavStream.Close();
                            }
                        }
                    }
                } // FileDataProvider  key check ends

                if (RequestCancellation)
                {
                    return;
                }

                media = addAudioWav(dataProv, audioAttrClipBegin, audioAttrClipEnd, treeNode);
                //media = addAudioWav ( fullWavPath, deleteSrcAfterCompletion, audioAttrClipBegin, audioAttrClipEnd );
            }
            else if (ext.Equals(DataProviderFactory.AUDIO_MP3_EXTENSION, StringComparison.OrdinalIgnoreCase) ||
                     ext.Equals(DataProviderFactory.AUDIO_MP4_EXTENSION, StringComparison.OrdinalIgnoreCase) ||
                     ext.Equals(DataProviderFactory.AUDIO_MP4_EXTENSION_, StringComparison.OrdinalIgnoreCase))
            {
                if (!File.Exists(fullPath))
                {
                    Debug.Fail("File not found: {0}", fullPath);
                    return;
                }

                if (RequestCancellation)
                {
                    return;
                }

                reportProgress(-1, String.Format(UrakawaSDK_daisy_Lang.DecodingAudio, Path.GetFileName(fullPath)));


                if (RequestCancellation)
                {
                    return;
                }

                string fullMp34PathOriginal = fullPath;

                FileDataProvider obj;
                m_OriginalAudioFile_FileDataProviderMap.TryGetValue(fullMp34PathOriginal, out obj);

                FileDataProvider dataProv = null;
                if (obj != null) //m_OriginalAudioFile_FileDataProviderMap.ContainsKey(fullMp3PathOriginal))
                {
                    if (m_AudioConversionSession.FirstDiscoveredPCMFormat == null)
                    {
                        DebugFix.Assert(obj.Presentation != presentation);

                        Object appData = obj.AppData;

                        DebugFix.Assert(appData != null);

                        if (appData != null && appData is WavClip.PcmFormatAndTime)
                        {
                            m_AudioConversionSession.FirstDiscoveredPCMFormat = new AudioLibPCMFormat();
                            m_AudioConversionSession.FirstDiscoveredPCMFormat.CopyFrom(((WavClip.PcmFormatAndTime)appData).mFormat);
                        }
                    }

                    if (obj.Presentation != presentation)
                    {
                        dataProv = (FileDataProvider)presentation.DataProviderFactory.Create(DataProviderFactory.AUDIO_WAV_MIME_TYPE);

                        reportProgress(-1, String.Format(UrakawaSDK_daisy_Lang.CopyingAudio, Path.GetFileName(obj.DataFileFullPath)));

                        dataProv.InitByCopyingExistingFile(obj.DataFileFullPath);

                        //m_AudioConversionSession.RelocateDestinationFilePath(newfullWavPath, dataProv.DataFileFullPath);

                        m_OriginalAudioFile_FileDataProviderMap.Remove(fullMp34PathOriginal);
                        m_OriginalAudioFile_FileDataProviderMap.Add(fullMp34PathOriginal, dataProv);

                        Object appData = obj.AppData;

                        DebugFix.Assert(appData != null);

                        if (appData != null && appData is WavClip.PcmFormatAndTime)
                        {
                            dataProv.AppData = new WavClip.PcmFormatAndTime(((WavClip.PcmFormatAndTime)appData).mFormat, ((WavClip.PcmFormatAndTime)appData).mTime);
                        }
                    }
                    else
                    {
                        dataProv = obj; // m_OriginalAudioFile_FileDataProviderMap[fullMp3PathOriginal];
                    }
                }
                else
                {
                    string newfullWavPath = m_AudioConversionSession.ConvertAudioFileFormat(fullMp34PathOriginal);

                    dataProv = (FileDataProvider)presentation.DataProviderFactory.Create(DataProviderFactory.AUDIO_WAV_MIME_TYPE);
                    //Console.WriteLine("Source audio file to SDK audio file map (before creating SDK audio file): " + Path.GetFileName(fullMp34PathOriginal) + " = " + dataProv.DataFileRelativePath);
                    dataProv.InitByMovingExistingFile(newfullWavPath);

                    m_AudioConversionSession.RelocateDestinationFilePath(newfullWavPath, dataProv.DataFileFullPath);

                    m_OriginalAudioFile_FileDataProviderMap.Add(fullMp34PathOriginal, dataProv);

                    if (RequestCancellation)
                    {
                        return;
                    }
                }

                if (dataProv != null)
                {
                    //if (m_firstTimePCMFormat)
                    //{
                    //    Stream wavStream = null;
                    //    try
                    //    {
                    //        wavStream = File.Open(newfullWavPath, FileMode.Open, FileAccess.Read, FileShare.Read);

                    //        uint dataLength;
                    //        AudioLibPCMFormat pcmInfo = null;

                    //        pcmInfo = AudioLibPCMFormat.RiffHeaderParse(wavStream, out dataLength);

                    //        presentation.MediaDataManager.DefaultPCMFormat = new PCMFormatInfo(pcmInfo);
                    //    }
                    //    finally
                    //    {
                    //        if (wavStream != null) wavStream.Close();
                    //        m_firstTimePCMFormat = false;
                    //    }
                    //}

                    if (RequestCancellation)
                    {
                        return;
                    }

                    //media = addAudioWav(newfullWavPath, true, audioAttrClipBegin, audioAttrClipEnd);
                    media = addAudioWav(dataProv, audioAttrClipBegin, audioAttrClipEnd, treeNode);

                    if (RequestCancellation)
                    {
                        return;
                    }

                    if (media == null)
                    {
#if DEBUG
                        Debugger.Break();
#endif
                    }
                }
                //}
            }

            if (RequestCancellation)
            {
                return;
            }

            if (media == null)
            {
                if (!TreenodesWithoutManagedAudioMediaData.Contains(treeNode))
                {
                    TreenodesWithoutManagedAudioMediaData.Add(treeNode);
                }

                Debug.Fail("Creating ExternalAudioMedia ??");

                Time timeClipBegin = null;

                ExternalAudioMedia exmedia = presentation.MediaFactory.CreateExternalAudioMedia();
                exmedia.Src = src;
                if (audioAttrClipBegin != null &&
                    !string.IsNullOrEmpty(audioAttrClipBegin.Value))
                {
                    timeClipBegin = new Time();
                    try
                    {
                        timeClipBegin = new Time(audioAttrClipBegin.Value);
                    }
                    catch (Exception ex)
                    {
                        string str = "CLIP BEGIN TIME PARSE FAIL: " + audioAttrClipBegin.Value;
                        Console.WriteLine(str);
                        Debug.Fail(str);
                    }
                    exmedia.ClipBegin = timeClipBegin;
                }
                if (audioAttrClipEnd != null &&
                    !string.IsNullOrEmpty(audioAttrClipEnd.Value))
                {
                    Time timeClipEnd = null;
                    try
                    {
                        timeClipEnd = new Time(audioAttrClipEnd.Value);
                    }
                    catch (Exception ex)
                    {
                        string str = "CLIP END TIME PARSE FAIL: " + audioAttrClipEnd.Value;
                        Console.WriteLine(str);
                        Debug.Fail(str);
                    }

                    if (timeClipEnd != null)
                    {
                        try
                        {
                            exmedia.ClipEnd = timeClipEnd;
                        }
                        catch (Exception ex)
                        {
                            string str = "CLIP TIME ERROR (end < begin): " + timeClipBegin + " (" + (audioAttrClipBegin != null ? audioAttrClipBegin.Value : "N/A") + ") / " + timeClipEnd + " (" + audioAttrClipEnd.Value + ")";
                            Console.WriteLine(str);
                            //Debug.Fail(str);
                        }
                    }
                }
            }

            if (RequestCancellation)
            {
                return;
            }

            if (media != null)
            {
                ChannelsProperty chProp =
                    treeNode.GetChannelsProperty();
                if (chProp == null)
                {
                    chProp =
                        presentation.PropertyFactory.CreateChannelsProperty();
                    treeNode.AddProperty(chProp);
                }
                if (isSequence)
                {
#if ENABLE_SEQ_MEDIA
                    SequenceMedia mediaSeq = chProp.GetMedia(m_audioChannel) as SequenceMedia;
                    if (mediaSeq == null)
                    {
                        mediaSeq = presentation.MediaFactory.CreateSequenceMedia();
                        mediaSeq.AllowMultipleTypes = false;
                        chProp.SetMedia(m_audioChannel, mediaSeq);
                    }
                    mediaSeq.ChildMedias.Insert(mediaSeq.ChildMedias.Count, media);
#else
                    ManagedAudioMedia existingMedia = chProp.GetMedia(presentation.ChannelsManager.GetOrCreateAudioChannel()) as ManagedAudioMedia;
                    if (existingMedia == null)
                    {
                        chProp.SetMedia(presentation.ChannelsManager.GetOrCreateAudioChannel(), media);
                    }
                    else
                    {
                        // WARNING: WavAudioMediaData implementation differs from AudioMediaData:
                        // the latter is naive and performs a stream binary copy, the latter is optimized and re-uses existing WavClips.
                        //  WARNING 2: The audio data from the given parameter gets emptied !
                        existingMedia.AudioMediaData.MergeWith(media.AudioMediaData);

                        //Stream stream = seqManMedia.AudioMediaData.OpenPcmInputStream();
                        //try
                        //{
                        //    mediaData.AppendPcmData(stream, null);
                        //}
                        //finally
                        //{
                        //    stream.Close();
                        //}
                    }
#endif //ENABLE_SEQ_MEDIA
                }
                else
                {
                    //#if DEBUG
                    //                    ((WavAudioMediaData) media.AudioMediaData).checkWavClips();
                    //#endif //DEBUG
                    chProp.SetMedia(presentation.ChannelsManager.GetOrCreateAudioChannel(), media);
                }
            }
            else
            {
                Debug.Fail("Media could not be created !");
            }
        }
Exemplo n.º 4
0
        private void checkAndAddDeferredRecordingDataItems()
        {
            if (m_DeferredRecordingDataItems == null)
            {
                return;
            }

            IsAutoPlay = false;

            bool needsRefresh = false;

            bool skipDrawing = Settings.Default.AudioWaveForm_DisableDraw;

            Settings.Default.AudioWaveForm_DisableDraw = true;


            //#if !DISABLE_SINGLE_RECORD_FILE
            string            previousRecordedFile    = null;
            FileDataProvider  currentFileDataProvider = null;
            AudioLibPCMFormat currentPcmFormat        = null;
            long currentPcmDataLength = -1;
            long previousBytePosEnd   = 0;

            //#endif


            foreach (var deferredRecordingDataItem in m_DeferredRecordingDataItems)
            {
                Tuple <TreeNode, TreeNode> treeNodeSelection = m_UrakawaSession.PerformTreeNodeSelection(deferredRecordingDataItem.TreeNode1, false, deferredRecordingDataItem.TreeNode2);
                if (treeNodeSelection.Item1 != deferredRecordingDataItem.TreeNode1 ||
                    treeNodeSelection.Item2 != deferredRecordingDataItem.TreeNode2)
                {
#if DEBUG
                    Debugger.Break();
#endif
                    continue;
                }

                if (IsWaveFormLoading && View != null)
                {
                    View.CancelWaveFormLoad(true);
                }

                if (deferredRecordingDataItem.PlayBytePosition >= 0)
                {
                    PlayBytePosition = deferredRecordingDataItem.PlayBytePosition;
                }
                else
                {
                    m_LastSetPlayBytePosition = deferredRecordingDataItem.PlayBytePosition;
                }

                if (PlayBytePosition != deferredRecordingDataItem.PlayBytePosition)
                {
#if DEBUG
                    Debugger.Break();
#endif
                    continue;
                }

                if (deferredRecordingDataItem.SelectionBeginBytePosition >= 0 &&
                    deferredRecordingDataItem.SelectionEndBytePosition > 0)
                {
                    State.Selection.SetSelectionBytes(deferredRecordingDataItem.SelectionBeginBytePosition, deferredRecordingDataItem.SelectionEndBytePosition);
                }
                else
                {
                    State.Selection.ClearSelection();
                }

                if (State.Selection.SelectionBeginBytePosition != deferredRecordingDataItem.SelectionBeginBytePosition ||
                    State.Selection.SelectionEndBytePosition != deferredRecordingDataItem.SelectionEndBytePosition)
                {
#if DEBUG
                    Debugger.Break();
#endif
                    continue;
                }

                if (!Settings.Default.Audio_DisableSingleWavFileRecord)
                {
                    TreeNode treeNode = deferredRecordingDataItem.TreeNode1 ?? deferredRecordingDataItem.TreeNode2;

                    if (string.IsNullOrEmpty(previousRecordedFile) ||
                        previousRecordedFile != deferredRecordingDataItem.RecordedFilePath)
                    {
                        PCMFormatInfo pcmInfo = State.Audio.PcmFormatRecordingMonitoring;
                        currentPcmFormat = (pcmInfo != null ? pcmInfo.Copy().Data : null);
                        if (currentPcmFormat == null)
                        {
                            Stream fileStream = File.Open(deferredRecordingDataItem.RecordedFilePath, FileMode.Open, FileAccess.Read,
                                                          FileShare.Read);
                            try
                            {
                                uint dataLength;
                                currentPcmFormat = AudioLibPCMFormat.RiffHeaderParse(fileStream, out dataLength);

                                currentPcmDataLength = dataLength;
                            }
                            finally
                            {
                                fileStream.Close();
                            }
                        }

                        currentFileDataProvider =
                            (FileDataProvider)treeNode.Presentation.DataProviderFactory.Create(DataProviderFactory.AUDIO_WAV_MIME_TYPE);
                        currentFileDataProvider.InitByMovingExistingFile(deferredRecordingDataItem.RecordedFilePath);
                        if (File.Exists(deferredRecordingDataItem.RecordedFilePath))
                        //check exist just in case file adopted by DataProviderManager
                        {
                            File.Delete(deferredRecordingDataItem.RecordedFilePath);
                        }
                    }

                    //Time duration = new Time(currentPcmFormat.ConvertBytesToTime(currentPcmDataLength));

                    if (previousBytePosEnd < 0)
                    {
                        previousBytePosEnd = 0;
                    }
                    long bytePosEnd = deferredRecordingDataItem.RecordAndContinue_StopBytePos;

                    openFile(treeNode, currentFileDataProvider, previousBytePosEnd, bytePosEnd, currentPcmFormat, currentPcmDataLength);
                }
                else
                {
                    openFile(deferredRecordingDataItem.RecordedFilePath, true, true,
                             State.Audio.PcmFormatRecordingMonitoring);
                }

                needsRefresh = true;

                //m_viewModel.CommandRefresh.Execute();
                //if (m_viewModel.View != null)
                //{
                //    m_viewModel.View.CancelWaveFormLoad(true);
                //}

                if (!Settings.Default.Audio_DisableSingleWavFileRecord)
                {
                    previousRecordedFile = deferredRecordingDataItem.RecordedFilePath;
                    previousBytePosEnd   = deferredRecordingDataItem.RecordAndContinue_StopBytePos;
                }
            }

            m_DeferredRecordingDataItems = null;

            Settings.Default.AudioWaveForm_DisableDraw = skipDrawing;

            if (needsRefresh)
            {
                CommandRefresh.Execute();
            }
        }
Exemplo n.º 5
0
        private void addAudio(TreeNode treeNode, XmlNode xmlNode, bool isSequence, string fullSmilPath)
        {
            string dirPath = Path.GetDirectoryName(fullSmilPath);

            XmlAttributeCollection audioAttrs = xmlNode.Attributes;

            if (audioAttrs == null || audioAttrs.Count == 0)
            {
                return;
            }
            XmlNode audioAttrSrc = audioAttrs.GetNamedItem("src");

            if (audioAttrSrc == null || String.IsNullOrEmpty(audioAttrSrc.Value))
            {
                return;
            }
            XmlNode audioAttrClipBegin = audioAttrs.GetNamedItem("clipBegin");
            XmlNode audioAttrClipEnd   = audioAttrs.GetNamedItem("clipEnd");

            Presentation presentation = m_Project.Presentations.Get(0);
            Media        media        = null;

            if (audioAttrSrc.Value.EndsWith("wav"))
            {
                string           dirPathBook         = Path.GetDirectoryName(m_Book_FilePath);
                FileDataProvider dataProv            = null;
                string           fullWavPathOriginal = Path.Combine(dirPathBook, audioAttrSrc.Value);
                if (!File.Exists(fullWavPathOriginal))
                {
                    System.Diagnostics.Debug.Fail("File not found: {0}", fullWavPathOriginal);
                    media = null;
                }
                else
                {
                    //bool deleteSrcAfterCompletion = false;

                    string fullWavPath = fullWavPathOriginal;

                    uint dataLength;
                    AudioLibPCMFormat pcmInfo = null;

                    if (m_OriginalAudioFile_FileDataProviderMap.ContainsKey(fullWavPath))
                    {
                        dataProv = m_OriginalAudioFile_FileDataProviderMap[fullWavPath];
                    }
                    else // create FileDataProvider
                    {
                        Stream wavStream = null;
                        try
                        {
                            wavStream = File.Open(fullWavPath, FileMode.Open, FileAccess.Read, FileShare.Read);

                            pcmInfo = AudioLibPCMFormat.RiffHeaderParse(wavStream, out dataLength);


                            //if (m_firstTimePCMFormat)
                            //{
                            //    presentation.MediaDataManager.DefaultPCMFormat = new PCMFormatInfo(pcmInfo);
                            //    m_firstTimePCMFormat = false;
                            //}

                            if (!presentation.MediaDataManager.DefaultPCMFormat.Data.IsCompatibleWith(pcmInfo))
                            {
                                wavStream.Close();
                                wavStream = null;

                                string newfullWavPath = m_AudioConversionSession.ConvertAudioFileFormat(fullWavPath);

                                dataProv = (FileDataProvider)presentation.DataProviderFactory.Create(DataProviderFactory.AUDIO_WAV_MIME_TYPE);
                                Console.WriteLine("Source audio file to SDK audio file map (before creating SDK audio file): " + Path.GetFileName(fullWavPath) + " = " + dataProv.DataFileRelativePath);
                                dataProv.InitByMovingExistingFile(newfullWavPath);
                                m_OriginalAudioFile_FileDataProviderMap.Add(fullWavPath, dataProv);
                            }
                            else // use original wav file by copying it to data directory
                            {
                                dataProv = (FileDataProvider)presentation.DataProviderFactory.Create(DataProviderFactory.AUDIO_WAV_MIME_TYPE);
                                Console.WriteLine("Source audio file to SDK audio file map (before creating SDK audio file): " + Path.GetFileName(fullWavPath) + " = " + dataProv.DataFileRelativePath);
                                dataProv.InitByCopyingExistingFile(fullWavPath);
                                m_OriginalAudioFile_FileDataProviderMap.Add(fullWavPath, dataProv);
                            }
                        }
                        finally
                        {
                            if (wavStream != null)
                            {
                                wavStream.Close();
                            }
                        }
                    }
                } // FileDataProvider  key check ends

                media = addAudioWav(dataProv, audioAttrClipBegin, audioAttrClipEnd);
                //media = addAudioWav ( fullWavPath, deleteSrcAfterCompletion, audioAttrClipBegin, audioAttrClipEnd );
            }
            else if (audioAttrSrc.Value.EndsWith("mp3"))
            {
                string fullMp3PathOriginal = Path.Combine(dirPath, audioAttrSrc.Value);
                if (!File.Exists(fullMp3PathOriginal))
                {
                    System.Diagnostics.Debug.Fail("File not found: {0}", fullMp3PathOriginal);
                    return;
                }

                string newfullWavPath = m_AudioConversionSession.ConvertAudioFileFormat(fullMp3PathOriginal);

                FileDataProvider dataProv = null;
                if (m_OriginalAudioFile_FileDataProviderMap.ContainsKey(fullMp3PathOriginal))
                {
                    dataProv = m_OriginalAudioFile_FileDataProviderMap[fullMp3PathOriginal];
                }
                else
                {
                    dataProv = (FileDataProvider)presentation.DataProviderFactory.Create(DataProviderFactory.AUDIO_WAV_MIME_TYPE);
                    Console.WriteLine("Source audio file to SDK audio file map (before creating SDK audio file): " + Path.GetFileName(fullMp3PathOriginal) + " = " + dataProv.DataFileRelativePath);
                    dataProv.InitByMovingExistingFile(newfullWavPath);
                    m_OriginalAudioFile_FileDataProviderMap.Add(fullMp3PathOriginal, dataProv);
                }

                if (newfullWavPath != null)
                {
                    //if (m_firstTimePCMFormat)
                    //{
                    //    Stream wavStream = null;
                    //    try
                    //    {
                    //        wavStream = File.Open(newfullWavPath, FileMode.Open, FileAccess.Read, FileShare.Read);

                    //        uint dataLength;
                    //        AudioLibPCMFormat pcmInfo = null;

                    //        pcmInfo = AudioLibPCMFormat.RiffHeaderParse(wavStream, out dataLength);

                    //        presentation.MediaDataManager.DefaultPCMFormat = new PCMFormatInfo(pcmInfo);
                    //    }
                    //    finally
                    //    {
                    //        if (wavStream != null) wavStream.Close();
                    //        m_firstTimePCMFormat = false;
                    //    }
                    //}


                    //media = addAudioWav(newfullWavPath, true, audioAttrClipBegin, audioAttrClipEnd);
                    media = addAudioWav(dataProv, audioAttrClipBegin, audioAttrClipEnd);
                }
                //}
            }

            if (media == null)
            {
                media = presentation.MediaFactory.CreateExternalAudioMedia();
                ((ExternalAudioMedia)media).Src = audioAttrSrc.Value;
                if (audioAttrClipBegin != null &&
                    !string.IsNullOrEmpty(audioAttrClipBegin.Value))
                {
                    try
                    {
                        ((ExternalAudioMedia)media).ClipBegin =
                            Time.ParseTimeString(audioAttrClipBegin.Value);
                    }
                    catch (FormatException e)
                    {
                        ((ExternalAudioMedia)media).ClipBegin =
                            new Time(0);
                        string str = "bad time string: " + audioAttrClipBegin.Value;
                        Console.Write(str);
                        Debug.Fail(str);
                    }
                }
                if (audioAttrClipEnd != null &&
                    !string.IsNullOrEmpty(audioAttrClipEnd.Value))
                {
                    try
                    {
                        ((ExternalAudioMedia)media).ClipEnd =
                            Time.ParseTimeString(audioAttrClipEnd.Value);
                    }
                    catch (FormatException e)
                    {
                        ((ExternalAudioMedia)media).ClipEnd =
                            new Time(0);
                        string str = "bad time string: " + audioAttrClipEnd.Value;
                        Console.Write(str);
                        Debug.Fail(str);
                    }
                }
            }

            if (media != null)
            {
                ChannelsProperty chProp =
                    treeNode.GetProperty <ChannelsProperty>();
                if (chProp == null)
                {
                    chProp =
                        presentation.PropertyFactory.CreateChannelsProperty();
                    treeNode.AddProperty(chProp);
                }
                if (isSequence)
                {
                    SequenceMedia mediaSeq = chProp.GetMedia(m_audioChannel) as SequenceMedia;
                    if (mediaSeq == null)
                    {
                        mediaSeq = presentation.MediaFactory.CreateSequenceMedia();
                        mediaSeq.AllowMultipleTypes = false;
                        chProp.SetMedia(m_audioChannel, mediaSeq);
                    }
                    mediaSeq.ChildMedias.Insert(mediaSeq.ChildMedias.Count, media);
                }
                else
                {
                    chProp.SetMedia(m_audioChannel, media);
                }
            }
            else
            {
                System.Diagnostics.Debug.Fail("Media could not be created !");
            }
        }