Пример #1
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 !");
            }
        }
Пример #2
0
        private void parseHeadLinks(string rootFilePath, Project project, XmlDocument contentDoc)
        {
            XmlNode headXmlNode = XmlDocumentHelper.GetFirstChildElementOrSelfWithName(contentDoc.DocumentElement, true, "head", null);

            if (headXmlNode == null)
            {
                return;
            }

            Presentation presentation = project.Presentations.Get(0);

            List <string> externalFileRelativePaths = new List <string>();

            foreach (ExternalFiles.ExternalFileData extData in presentation.ExternalFilesDataManager.ManagedObjects.ContentsAs_Enumerable)
            {
                if (!string.IsNullOrEmpty(extData.OriginalRelativePath))
                {
                    string fullPath = Path.Combine(Path.GetDirectoryName(rootFilePath), extData.OriginalRelativePath);
                    string relPath  = FileDataProvider.NormaliseFullFilePath(fullPath).Replace('/', '\\');

                    if (!externalFileRelativePaths.Contains(relPath))
                    {
                        externalFileRelativePaths.Add(relPath);
                    }
                }
            }

            List <XmlNode> externalFilesLinks = new List <XmlNode>();

            externalFilesLinks.AddRange(XmlDocumentHelper.GetChildrenElementsOrSelfWithName(headXmlNode, true, "link", headXmlNode.NamespaceURI, false));
            externalFilesLinks.AddRange(XmlDocumentHelper.GetChildrenElementsOrSelfWithName(headXmlNode, true, "script", headXmlNode.NamespaceURI, false));
            externalFilesLinks.AddRange(XmlDocumentHelper.GetChildrenElementsOrSelfWithName(headXmlNode, true, "style", headXmlNode.NamespaceURI, false));
            externalFilesLinks.AddRange(XmlDocumentHelper.GetChildrenElementsOrSelfWithName(headXmlNode, true, "title", headXmlNode.NamespaceURI, false));

            foreach (XmlNode linkNode in externalFilesLinks)
            {
                TreeNode treeNode = presentation.TreeNodeFactory.Create();
                presentation.HeadNode.AppendChild(treeNode);
                XmlProperty xmlProp = presentation.PropertyFactory.CreateXmlProperty();
                treeNode.AddProperty(xmlProp);
                xmlProp.SetQName(linkNode.LocalName,
                                 headXmlNode.NamespaceURI == linkNode.NamespaceURI ? "" : linkNode.NamespaceURI);
                //Console.WriteLine("XmlProperty: " + xmlProp.LocalName);

                foreach (System.Xml.XmlAttribute xAttr in linkNode.Attributes)
                {
                    if (
                        //xAttr.LocalName.Equals(XmlReaderWriterHelper.NS_PREFIX_XMLNS, StringComparison.OrdinalIgnoreCase)
                        //|| xAttr.LocalName.Equals("xsi", StringComparison.OrdinalIgnoreCase)
                        xAttr.NamespaceURI.Equals(XmlReaderWriterHelper.NS_URL_XMLNS, StringComparison.OrdinalIgnoreCase) ||
                        xAttr.LocalName.Equals("space", StringComparison.OrdinalIgnoreCase) &&
                        xAttr.NamespaceURI.Equals(XmlReaderWriterHelper.NS_URL_XML, StringComparison.OrdinalIgnoreCase)
                        )
                    {
                        continue;
                    }

                    xmlProp.SetAttribute(xAttr.Name,
                                         linkNode.NamespaceURI == xAttr.NamespaceURI ? "" : xAttr.NamespaceURI,
                                         xAttr.Value);

                    if ((xAttr.Name.Equals("href", StringComparison.OrdinalIgnoreCase) ||
                         xAttr.Name.Equals("src", StringComparison.OrdinalIgnoreCase)) &&
                        !string.IsNullOrEmpty(xAttr.Value) &&
                        !FileDataProvider.isHTTPFile(xAttr.Value))
                    {
                        string urlDecoded = FileDataProvider.UriDecode(xAttr.Value);

                        string fullPath     = Path.Combine(Path.GetDirectoryName(rootFilePath), urlDecoded);
                        string pathFromAttr = FileDataProvider.NormaliseFullFilePath(fullPath).Replace('/', '\\');

                        if (!externalFileRelativePaths.Contains(pathFromAttr))
                        {
                            if (File.Exists(pathFromAttr))
                            {
                                ExternalFiles.ExternalFileData efd = presentation.ExternalFilesDataFactory.Create <ExternalFiles.GenericExternalFileData>();
                                try
                                {
                                    efd.InitializeWithData(pathFromAttr, urlDecoded, true, null);

                                    externalFileRelativePaths.Add(pathFromAttr);

                                    addOPF_GlobalAssetPath(pathFromAttr);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                    Console.WriteLine(ex.StackTrace);

#if DEBUG
                                    Debugger.Break();
#endif
                                }
                            }
#if DEBUG
                            else
                            {
                                Debugger.Break();
                            }
#endif
                        }
                    }
                }

                string innerText = linkNode.InnerText; // includes CDATA sections! (merges "//" javascript comment markers too)

                if (!string.IsNullOrEmpty(innerText))
                {
                    urakawa.media.TextMedia textMedia = presentation.MediaFactory.CreateTextMedia();
                    textMedia.Text = innerText;
                    ChannelsProperty cProp = presentation.PropertyFactory.CreateChannelsProperty();
                    cProp.SetMedia(presentation.ChannelsManager.GetOrCreateTextChannel(), textMedia);
                    treeNode.AddProperty(cProp);
                    //Console.WriteLine("Link inner text: " + textMedia.Text);
                }
            }
        }
Пример #3
0
        private void parseDTBookXmlDocAndPopulateDataModel(XmlNode xmlNode, core.TreeNode parentTreeNode)
        {
            XmlNodeType xmlType = xmlNode.NodeType;

            switch (xmlType)
            {
            case XmlNodeType.Attribute:
            {
                System.Diagnostics.Debug.Fail("Calling this method with an XmlAttribute should never happen !!");
                break;
            }

            case XmlNodeType.Document:
            {
                parseDTBookXmlDocAndPopulateDataModel(((XmlDocument)xmlNode).DocumentElement, parentTreeNode);
                break;
            }

            case XmlNodeType.Element:
            {
                Presentation presentation = m_Project.GetPresentation(0);

                core.TreeNode treeNode = presentation.TreeNodeFactory.Create();

                if (parentTreeNode == null)
                {
                    presentation.RootNode = treeNode;
                    parentTreeNode        = presentation.RootNode;
                }
                else
                {
                    parentTreeNode.AppendChild(treeNode);
                }

                XmlProperty xmlProp = presentation.PropertyFactory.CreateXmlProperty();
                treeNode.AddProperty(xmlProp);
                xmlProp.LocalName = xmlNode.Name;
                if (xmlNode.ParentNode != null && xmlNode.ParentNode.NodeType == XmlNodeType.Document)
                {
                    presentation.PropertyFactory.DefaultXmlNamespaceUri = xmlNode.NamespaceURI;
                }

                if (xmlNode.NamespaceURI != presentation.PropertyFactory.DefaultXmlNamespaceUri)
                {
                    xmlProp.NamespaceUri = xmlNode.NamespaceURI;
                }

                XmlAttributeCollection attributeCol = xmlNode.Attributes;

                if (attributeCol != null)
                {
                    for (int i = 0; i < attributeCol.Count; i++)
                    {
                        XmlNode attr = attributeCol.Item(i);
                        if (attr.Name != "smilref")
                        {
                            xmlProp.SetAttribute(attr.Name, "", attr.Value);
                        }
                    }


                    if (xmlNode.Name == "meta")
                    {
                        XmlNode attrName    = attributeCol.GetNamedItem("name");
                        XmlNode attrContent = attributeCol.GetNamedItem("content");
                        if (attrName != null && attrContent != null && !String.IsNullOrEmpty(attrName.Value) &&
                            !String.IsNullOrEmpty(attrContent.Value))
                        {
                            Metadata md = presentation.MetadataFactory.CreateMetadata();
                            md.Name    = attrName.Value;
                            md.Content = attrContent.Value;
                            presentation.AddMetadata(md);
                        }
                    }
                }


                foreach (XmlNode childXmlNode in xmlNode.ChildNodes)
                {
                    parseDTBookXmlDocAndPopulateDataModel(childXmlNode, treeNode);
                }
                break;
            }

            case XmlNodeType.Text:
            {
                Presentation presentation = m_Project.GetPresentation(0);

                string    text      = xmlNode.Value;
                TextMedia textMedia = presentation.MediaFactory.CreateTextMedia();
                textMedia.Text = text;

                ChannelsProperty cProp = presentation.PropertyFactory.CreateChannelsProperty();
                cProp.SetMedia(m_textChannel, textMedia);

                int counter = 0;
                foreach (XmlNode childXmlNode in xmlNode.ParentNode.ChildNodes)
                {
                    XmlNodeType childXmlType = childXmlNode.NodeType;
                    if (childXmlType == XmlNodeType.Text || childXmlType == XmlNodeType.Element)
                    {
                        counter++;
                    }
                }
                if (counter == 1)
                {
                    parentTreeNode.AddProperty(cProp);
                }
                else
                {
                    core.TreeNode txtWrapperNode = presentation.TreeNodeFactory.Create();
                    txtWrapperNode.AddProperty(cProp);
                    parentTreeNode.AppendChild(txtWrapperNode);
                }

                break;
            }

            default:
            {
                return;
            }
            }
        }
Пример #4
0
        private void verifyTree(TreeNode node, bool ancestorHasAudio, string ancestorExtAudioFile)
        {
            if (TreeNodeMustBeSkipped(node))
            {
                return;
            }

            if (TreeNodeTriggersNewAudioFile(node) && ancestorExtAudioFile == null)
            {
                ancestorExtAudioFile = "";
            }

            Media manSeqMedia = node.GetManagedAudioMediaOrSequenceMedia();

            if (ancestorHasAudio)
            {
                DebugFix.Assert(manSeqMedia == null);
            }

            if (node.HasChannelsProperty)
            {
                ChannelsProperty chProp = node.GetChannelsProperty();
                Media            media  = chProp.GetMedia(DestinationChannel);

                if (ancestorHasAudio)
                {
                    DebugFix.Assert(media == null);
                }

                if (media != null)
                {
                    DebugFix.Assert(media is ExternalAudioMedia);
                    DebugFix.Assert(manSeqMedia != null);

                    if (!ancestorHasAudio)
                    {
                        ExternalAudioMedia extMedia = (ExternalAudioMedia)media;

                        ancestorHasAudio = true;

                        if (ancestorExtAudioFile != null)
                        {
                            if (ancestorExtAudioFile == "")
                            {
                                ancestorExtAudioFile = extMedia.Uri.LocalPath;
                            }
                            else
                            {
                                DebugFix.Assert(ancestorExtAudioFile == extMedia.Uri.LocalPath);
                            }
                        }
                        else
                        {
                            ancestorExtAudioFile = extMedia.Uri.LocalPath;
                        }

                        string ext = Path.GetExtension(ancestorExtAudioFile);
                        if (!DataProviderFactory.AUDIO_WAV_EXTENSION.Equals(ext, StringComparison.OrdinalIgnoreCase))
                        {
                            Debug.Fail("Verification can only be done if external media points to wav file!");
                        }

                        //reportProgress(-1, @"DEBUG: " + ancestorExtAudioFile);

                        Stream extMediaStream = new FileStream(ancestorExtAudioFile, FileMode.Open, FileAccess.Read,
                                                               FileShare.None);

                        Stream manMediaStream = null;

                        ManagedAudioMedia manMedia = node.GetManagedAudioMedia();

#if ENABLE_SEQ_MEDIA
                        SequenceMedia seqMedia = node.GetManagedAudioSequenceMedia();
#endif //ENABLE_SEQ_MEDIA

                        if (manMedia != null)
                        {
#if ENABLE_SEQ_MEDIA
                            DebugFix.Assert(seqMedia == null);
#endif //ENABLE_SEQ_MEDIA

                            DebugFix.Assert(manMedia.HasActualAudioMediaData);

                            manMediaStream = manMedia.AudioMediaData.OpenPcmInputStream();
                        }
                        else
                        {
                            Debug.Fail("SequenceMedia is normally removed at import time...have you tried re-importing the DAISY book ?");

#if ENABLE_SEQ_MEDIA
                            DebugFix.Assert(seqMedia != null);
                            DebugFix.Assert(!seqMedia.AllowMultipleTypes);
                            DebugFix.Assert(seqMedia.ChildMedias.Count > 0);
                            DebugFix.Assert(seqMedia.ChildMedias.Get(0) is ManagedAudioMedia);

                            manMediaStream = seqMedia.OpenPcmInputStreamOfManagedAudioMedia();
#endif //ENABLE_SEQ_MEDIA
                        }

                        try
                        {
                            uint extMediaPcmLength;
                            AudioLibPCMFormat pcmInfo = AudioLibPCMFormat.RiffHeaderParse(extMediaStream,
                                                                                          out extMediaPcmLength);

                            DebugFix.Assert(extMediaPcmLength == extMediaStream.Length - extMediaStream.Position);

                            if (manMedia != null)
                            {
                                DebugFix.Assert(pcmInfo.IsCompatibleWith(manMedia.AudioMediaData.PCMFormat.Data));
                            }

#if ENABLE_SEQ_MEDIA
                            if (seqMedia != null)
                            {
                                DebugFix.Assert(
                                    pcmInfo.IsCompatibleWith(
                                        ((ManagedAudioMedia)seqMedia.ChildMedias.Get(0)).AudioMediaData.PCMFormat.Data));
                            }
#endif //ENABLE_SEQ_MEDIA

                            extMediaStream.Position +=
                                pcmInfo.ConvertTimeToBytes(extMedia.ClipBegin.AsLocalUnits);

                            long manMediaStreamPosBefore = manMediaStream.Position;
                            long extMediaStreamPosBefore = extMediaStream.Position;

                            //DebugFix.Assert(AudioLibPCMFormat.CompareStreamData(manMediaStream, extMediaStream, (int)manMediaStream.Length));

                            //DebugFix.Assert(manMediaStream.Position == manMediaStreamPosBefore + manMediaStream.Length);
                            //DebugFix.Assert(extMediaStream.Position == extMediaStreamPosBefore + manMediaStream.Length);
                        }
                        finally
                        {
                            extMediaStream.Close();
                            manMediaStream.Close();
                        }
                    }
                }
                else
                {
                    DebugFix.Assert(manSeqMedia == null);
                }
            }
            else
            {
                DebugFix.Assert(manSeqMedia == null);
            }

            foreach (TreeNode child in node.Children.ContentsAs_Enumerable)
            {
                verifyTree(child, ancestorHasAudio, ancestorExtAudioFile);
            }
        }
Пример #5
0
        private void parseSmil(string fullSmilPath)
        {
            Presentation presentation = m_Project.GetPresentation(0);

            string dirPath = Path.GetDirectoryName(m_Book_FilePath);

            XmlDocument smilXmlDoc = readXmlDocument(fullSmilPath);

            XmlNodeList listOfAudioNodes = smilXmlDoc.GetElementsByTagName("audio");

            if (listOfAudioNodes != null)
            {
                foreach (XmlNode audioNode in listOfAudioNodes)
                {
                    XmlAttributeCollection attributeCol = audioNode.Attributes;

                    if (attributeCol != null)
                    {
                        XmlNode attrAudioSrc = attributeCol.GetNamedItem("src");
                        if (attrAudioSrc != null && !String.IsNullOrEmpty(attrAudioSrc.Value))
                        {
                            XmlNode parent = audioNode.ParentNode;
                            if (parent != null && parent.Name == "a")
                            {
                                parent = parent.ParentNode;
                            }

                            if (parent != null)
                            {
                                XmlNodeList listOfAudioPeers = parent.ChildNodes;
                                foreach (XmlNode peerNode in listOfAudioPeers)
                                {
                                    if (peerNode.NodeType == XmlNodeType.Element && peerNode.Name == "text")
                                    {
                                        XmlAttributeCollection peerAttrs = peerNode.Attributes;

                                        if (peerAttrs != null)
                                        {
                                            XmlNode attrTextSrc = peerAttrs.GetNamedItem("src");
                                            if (attrTextSrc != null && !String.IsNullOrEmpty(attrTextSrc.Value))
                                            {
                                                int index = attrTextSrc.Value.LastIndexOf('#');
                                                if (index < (attrTextSrc.Value.Length - 1))
                                                {
                                                    string        dtbookFragmentId = attrTextSrc.Value.Substring(index + 1);
                                                    core.TreeNode tNode            = getTreeNodeWithXmlElementId(dtbookFragmentId);
                                                    if (tNode != null)
                                                    {
                                                        AbstractAudioMedia existingAudioMedia = tNode.GetAudioMedia();
                                                        if (existingAudioMedia != null)
                                                        {
                                                            //Ignore.
                                                            //System.Diagnostics.Debug.Fail("TreeNode already has media ??");
                                                        }

                                                        XmlNode attrClipBegin = attributeCol.GetNamedItem("clipBegin");
                                                        XmlNode attrClipEnd   = attributeCol.GetNamedItem("clipEnd");

                                                        Media media = null;
                                                        if (attrAudioSrc.Value.EndsWith("wav"))
                                                        {
                                                            string fullWavPath = Path.Combine(dirPath,
                                                                                              attrAudioSrc.Value);

                                                            PCMDataInfo pcmInfo   = null;
                                                            Stream      wavStream = null;
                                                            try
                                                            {
                                                                wavStream = File.Open(fullWavPath, FileMode.Open,
                                                                                      FileAccess.Read, FileShare.Read);
                                                                pcmInfo = PCMDataInfo.ParseRiffWaveHeader(wavStream);
                                                                presentation.MediaDataManager.DefaultPCMFormat = pcmInfo.Copy();
                                                                TimeDelta duration = new TimeDelta(pcmInfo.Duration);

                                                                Time clipB = Time.Zero;
                                                                Time clipE = Time.MaxValue;

                                                                if (attrClipBegin != null &&
                                                                    !string.IsNullOrEmpty(attrClipBegin.Value))
                                                                {
                                                                    clipB = new Time(TimeSpan.Parse(attrClipBegin.Value));
                                                                }
                                                                if (attrClipEnd != null &&
                                                                    !string.IsNullOrEmpty(attrClipEnd.Value))
                                                                {
                                                                    clipE = new Time(TimeSpan.Parse(attrClipEnd.Value));
                                                                }
                                                                if (!clipB.IsEqualTo(Time.Zero) || !clipE.IsEqualTo(Time.MaxValue))
                                                                {
                                                                    duration = clipE.GetTimeDelta(clipB);
                                                                }
                                                                long byteOffset = 0;
                                                                if (!clipB.IsEqualTo(Time.Zero))
                                                                {
                                                                    byteOffset = pcmInfo.GetByteForTime(clipB);
                                                                }
                                                                if (byteOffset > 0)
                                                                {
                                                                    wavStream.Seek(byteOffset, SeekOrigin.Current);
                                                                }

                                                                presentation.MediaDataFactory.DefaultAudioMediaDataType =
                                                                    typeof(WavAudioMediaData);

                                                                WavAudioMediaData mediaData =
                                                                    (WavAudioMediaData)
                                                                    presentation.MediaDataFactory.CreateAudioMediaData();
                                                                mediaData.InsertAudioData(wavStream, Time.Zero, duration);

                                                                media = presentation.MediaFactory.CreateManagedAudioMedia();
                                                                ((ManagedAudioMedia)media).AudioMediaData = mediaData;
                                                            }
                                                            finally
                                                            {
                                                                if (wavStream != null)
                                                                {
                                                                    wavStream.Close();
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            media = presentation.MediaFactory.CreateExternalAudioMedia();
                                                            ((ExternalAudioMedia)media).Src = attrAudioSrc.Value;
                                                            if (attrClipBegin != null &&
                                                                !string.IsNullOrEmpty(attrClipBegin.Value))
                                                            {
                                                                ((ExternalAudioMedia)media).ClipBegin =
                                                                    new Time(TimeSpan.Parse(attrClipBegin.Value));
                                                            }
                                                            if (attrClipEnd != null &&
                                                                !string.IsNullOrEmpty(attrClipEnd.Value))
                                                            {
                                                                ((ExternalAudioMedia)media).ClipEnd =
                                                                    new Time(TimeSpan.Parse(attrClipEnd.Value));
                                                            }
                                                        }

                                                        ChannelsProperty chProp = tNode.GetProperty <ChannelsProperty>();
                                                        if (chProp == null)
                                                        {
                                                            chProp =
                                                                presentation.PropertyFactory.CreateChannelsProperty();
                                                            tNode.AddProperty(chProp);
                                                        }
                                                        chProp.SetMedia(m_audioChannel, media);
                                                        break; // scan peers to audio node
                                                    }
                                                    else
                                                    {
                                                        System.Diagnostics.Debug.Fail("XmlProperty with ID not found ??");
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #6
0
        public override bool PreVisit(TreeNode node)
        {
            if (m_RootNode == null)
            {
                m_RootNode = node;
            }

            if (m_currentAudioLevelNode == null)
            {
                m_currentAudioLevelNode = m_RootNode;
            }

            if (TreeNodeMustBeSkipped(node))
            {
                return(false);
            }

            if (RequestCancellation)
            {
                checkTransientWavFileAndClose(node);
                return(false);
            }


            if (TreeNodeTriggersNewAudioFile(node))
            {
                m_currentAudioLevelNode = node;

                checkTransientWavFileAndClose(node);
                // REMOVED, because doesn't support nested TreeNode matches ! return false; // skips children, see postVisit
            }

            if (node.GetAlternateContentProperty() != null)
            {
                m_AlternateContentPropertiesList.Add(node.GetAlternateContentProperty());
            }

            if (!node.HasChannelsProperty)
            {
                return(true);
            }

            if (!node.Presentation.MediaDataManager.EnforceSinglePCMFormat)
            {
                Debug.Fail("! EnforceSinglePCMFormat ???");
                throw new Exception("! EnforceSinglePCMFormat ???");
            }

#if ENABLE_SEQ_MEDIA
            Media media = node.GetManagedAudioMediaOrSequenceMedia();

            if (media == null)
            {
                return(true);
            }
#endif


            ManagedAudioMedia manAudioMedia = node.GetManagedAudioMedia();
            if (manAudioMedia == null)
            {
                return(true);
            }

            //if (!manAudioMedia.HasActualAudioMediaData)
            //{
            //    return true;
            //}

            if (m_TransientWavFileStream == null)
            {
                mCurrentAudioFileNumber++;
                Uri waveFileUri = GetCurrentAudioFileUri();
                m_TransientWavFileStream = new FileStream(waveFileUri.LocalPath, FileMode.Create, FileAccess.Write, FileShare.None);

                m_TransientWavFileStreamRiffOffset = node.Presentation.MediaDataManager.DefaultPCMFormat.Data.RiffHeaderWrite(m_TransientWavFileStream, 0);
            }

            long bytesBegin = m_TransientWavFileStream.Position - (long)m_TransientWavFileStreamRiffOffset;

#if ENABLE_SEQ_MEDIA
            SequenceMedia seqAudioMedia = node.GetManagedAudioSequenceMedia();
#endif //ENABLE_SEQ_MEDIA

            Stream audioPcmStream = null;
            if (manAudioMedia != null)
            {
                audioPcmStream = manAudioMedia.AudioMediaData.OpenPcmInputStream();
            }
#if ENABLE_SEQ_MEDIA
            else if (seqAudioMedia != null)
            {
                Debug.Fail("SequenceMedia is normally removed at import time...have you tried re-importing the DAISY book ?");

                audioPcmStream = seqAudioMedia.OpenPcmInputStreamOfManagedAudioMedia();
            }
#endif //ENABLE_SEQ_MEDIA
            else
            {
                Debug.Fail("This should never happen !!");
                return(false);
            }

            if (RequestCancellation)
            {
                checkTransientWavFileAndClose(node);
                return(false);
            }

            try
            {
                const uint BUFFER_SIZE = 1024 * 1024 * 3; // 3 MB MAX BUFFER
                uint       streamCount = StreamUtils.Copy(audioPcmStream, 0, m_TransientWavFileStream, BUFFER_SIZE);

                //System.Windows.Forms.MessageBox.Show ( audioPcmStream.Length.ToString () + " : " +  m_TransientWavFileStream.Length.ToString () + " : " + streamCount.ToString () );
            }
            catch
            {
                m_TransientWavFileStream.Close();
                m_TransientWavFileStream           = null;
                m_TransientWavFileStreamRiffOffset = 0;

#if DEBUG
                Debugger.Break();
#endif
            }
            finally
            {
                audioPcmStream.Close();
            }

            if (m_TransientWavFileStream == null)
            {
                Debug.Fail("Stream copy error !!");
                return(false);
            }

            long bytesEnd = m_TransientWavFileStream.Position - (long)m_TransientWavFileStreamRiffOffset;

            string src = node.Presentation.RootUri.MakeRelativeUri(GetCurrentAudioFileUri()).ToString();

            if (manAudioMedia != null
#if ENABLE_SEQ_MEDIA
                || seqAudioMedia != null
#endif //ENABLE_SEQ_MEDIA
                )
            {
                if (m_TotalTimeInLocalUnits == 0)
                {
                    Time dur = node.Root.GetDurationOfManagedAudioMediaFlattened();
                    if (dur != null)
                    {
                        m_TotalTimeInLocalUnits = dur.AsLocalUnits;
                    }
                }

                m_TimeElapsedInLocalUnits += manAudioMedia != null ? manAudioMedia.Duration.AsLocalUnits :
#if ENABLE_SEQ_MEDIA
                                             seqAudioMedia.GetDurationOfManagedAudioMedia().AsLocalUnits
#else
                                             -1
#endif //ENABLE_SEQ_MEDIA
                ;

                int percent = Convert.ToInt32((m_TimeElapsedInLocalUnits * 100) / m_TotalTimeInLocalUnits);

                if (EncodePublishedAudioFiles)
                {
                    reportProgress_Throttle(percent, String.Format(UrakawaSDK_daisy_Lang.CreateMP3File, Path.GetFileName(src).Replace(DataProviderFactory.AUDIO_WAV_EXTENSION, DataProviderFactory.AUDIO_MP3_EXTENSION), GetSizeInfo(node)));
                }
                else
                {
                    reportProgress_Throttle(percent, String.Format(UrakawaSDK_daisy_Lang.CreatingAudioFile, Path.GetFileName(src), GetSizeInfo(node)));
                }
                //Console.WriteLine("progress percent " + m_ProgressPercentage);
            }

            ExternalAudioMedia extAudioMedia = node.Presentation.MediaFactory.Create <ExternalAudioMedia>();
            extAudioMedia.Tag = m_currentAudioLevelNode;

            ushort nChannels = (ushort)(EncodePublishedAudioFilesStereo ? 2 : 1);
            if ((EncodePublishedAudioFiles
                 ||
                 (ushort)EncodePublishedAudioFilesSampleRate != node.Presentation.MediaDataManager.DefaultPCMFormat.Data.SampleRate ||
                 nChannels != node.Presentation.MediaDataManager.DefaultPCMFormat.Data.NumberOfChannels
                 ) &&
                !m_ExternalAudioMediaList.Contains(extAudioMedia))
            {
                m_ExternalAudioMediaList.Add(extAudioMedia);
            }

            extAudioMedia.Language = node.Presentation.Language;
            extAudioMedia.Src      = src;

            long timeBegin =
                node.Presentation.MediaDataManager.DefaultPCMFormat.Data.ConvertBytesToTime(bytesBegin);
            long timeEnd =
                node.Presentation.MediaDataManager.DefaultPCMFormat.Data.ConvertBytesToTime(bytesEnd);
            extAudioMedia.ClipBegin = new Time(timeBegin);
            extAudioMedia.ClipEnd   = new Time(timeEnd);

            ChannelsProperty chProp = node.GetChannelsProperty();
            if (chProp.GetMedia(DestinationChannel) != null)
            {
                chProp.SetMedia(DestinationChannel, null);
                Debug.Fail("This should never happen !!");
            }
            chProp.SetMedia(DestinationChannel, extAudioMedia);

            return(false);
        }
Пример #7
0
        public override bool PreVisit(TreeNode node)
        {
            if (m_RootNode == null)
            {
                m_RootNode = node;
            }

            if (TreeNodeMustBeSkipped(node))
            {
                return(false);
            }
            if (TreeNodeTriggersNewAudioFile(node))
            {
                createNextAudioFile(node);
            }

            if (node.HasProperties(typeof(ChannelsProperty)))
            {
                ChannelsProperty chProp = node.GetChannelsProperty();

                ManagedAudioMedia mam = chProp.GetMedia(SourceChannel) as ManagedAudioMedia;
                if (mam != null)
                {
                    AudioMediaData amd = mam.AudioMediaData;

                    if (mCurrentAudioFileStream == null)
                    {
                        createNextAudioFile(node);
                    }
                    else if (mCurrentAudioFilePCMFormat != null &&
                             !mCurrentAudioFilePCMFormat.Data.IsCompatibleWith(amd.PCMFormat.Data))
                    {
#if DEBUG
                        Debugger.Break();
#endif
                        createNextAudioFile(node);
                    }

                    if (mCurrentAudioFileStream != null && mCurrentAudioFilePCMFormat == null)
                    {
                        writeInitialHeader(amd.PCMFormat);
                    }

                    Time durationFromRiffHeader = amd.AudioDuration;

                    Time clipBegin = new Time(mCurrentAudioFilePCMFormat.Data.ConvertBytesToTime(mCurrentAudioFileStream.Position - mCurrentAudioFileStreamRiffWaveHeaderLength));
                    Time clipEnd   = new Time(clipBegin.AsTimeSpanTicks + durationFromRiffHeader.AsTimeSpanTicks, true);

                    //BinaryReader rd = new BinaryReader(stream);

                    Stream stream = amd.OpenPcmInputStream();
                    try
                    {
                        const uint BUFFER_SIZE = 1024 * 1024 * 3; // 3 MB MAX BUFFER
                        StreamUtils.Copy(stream, 0, mCurrentAudioFileStream, BUFFER_SIZE);
                    }
                    finally
                    {
                        stream.Close();
                    }

                    ExternalAudioMedia eam = node.Presentation.MediaFactory.Create <ExternalAudioMedia>();
                    if (eam == null)
                    {
                        throw new exception.FactoryCannotCreateTypeException(String.Format(
                                                                                 "The media facotry cannot create a ExternalAudioMedia matching QName {1}:{0}",

                                                                                 XukAble.GetXukName(typeof(ExternalAudioMedia), true) ?? typeof(ExternalAudioMedia).Name,
                                                                                 node.Presentation.Project.GetXukNamespace()));
                    }

                    eam.Language  = mam.Language;
                    eam.Src       = node.Presentation.RootUri.MakeRelativeUri(GetCurrentAudioFileUri()).ToString();
                    eam.ClipBegin = clipBegin;
                    eam.ClipEnd   = clipEnd;

                    if (chProp.GetMedia(DestinationChannel) != null)
                    {
#if DEBUG
                        Debugger.Break();
#endif
                        chProp.SetMedia(DestinationChannel, null);
                    }
                    chProp.SetMedia(DestinationChannel, eam);
                }
            }
            return(true);
        }
Пример #8
0
        private static void CheckPublishedFiles(TreeNode node, Channel sourceCh, Channel destCh,
                                                Uri curWavUri_, MemoryStream curAudioData, PCMFormatInfo curPCMFormat)
        {
            Uri curWavUri = (curWavUri_ == null ? null : new Uri(curWavUri_.ToString()));

            if (node.HasProperties(typeof(ChannelsProperty)))
            {
                ChannelsProperty   chProp = node.GetProperty <ChannelsProperty>();
                ManagedAudioMedia  mam    = chProp.GetMedia(sourceCh) as ManagedAudioMedia;
                ExternalAudioMedia eam    = chProp.GetMedia(destCh) as ExternalAudioMedia;

                Assert.AreEqual(mam == null, eam == null,
                                "There may be external audio media if and only if there is managed audio media");

                if (mam != null && eam != null)
                {
                    Assert.IsTrue(mam.Duration.IsEqualTo(eam.Duration),
                                  "Duration of managed and external audio media differs");

                    if (eam.Uri != null)
                    {
                        FileStream wavFS_         = new FileStream(eam.Uri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.None);
                        Stream     manAudioStream = mam.AudioMediaData.OpenPcmInputStream();
                        try
                        {
                            uint dataLength;
                            AudioLibPCMFormat pcmInfo = AudioLibPCMFormat.RiffHeaderParse(wavFS_, out dataLength);

                            Assert.IsTrue(pcmInfo.IsCompatibleWith(mam.AudioMediaData.PCMFormat.Data),
                                          "External audio has incompatible pcm format");

                            wavFS_.Position += pcmInfo.ConvertTimeToBytes(eam.ClipBegin.TimeAsMillisecondFloat);

                            Assert.IsTrue(
                                AudioLibPCMFormat.CompareStreamData(manAudioStream, wavFS_, (int)manAudioStream.Length),
                                "External audio contains wrong data");
                        }
                        finally
                        {
                            wavFS_.Close();
                            manAudioStream.Close();
                        }
                    }

                    if (curWavUri != null)
                    {
                        FileStream wavFS = new FileStream(curWavUri.LocalPath, FileMode.Open, FileAccess.Read,
                                                          FileShare.None);
                        try
                        {
                            uint dataLength;
                            AudioLibPCMFormat pcmInfo = AudioLibPCMFormat.RiffHeaderParse(wavFS, out dataLength);

                            Assert.IsTrue(pcmInfo.IsCompatibleWith(curPCMFormat.Data),
                                          "External audio has incompatible pcm format");

                            curAudioData.Position = 0;

                            Assert.AreEqual(curAudioData.Length, (long)dataLength,
                                            "External audio has unexpected length");
                            Assert.IsTrue(
                                AudioLibPCMFormat.CompareStreamData(curAudioData, wavFS, (int)curAudioData.Length),
                                "External audio contains wrong data");
                        }
                        finally
                        {
                            wavFS.Close();
                        }
                    }

                    if (curWavUri == null)
                    {
                        curWavUri    = new Uri(eam.Uri.ToString());
                        curAudioData = new MemoryStream();
                        curPCMFormat = mam.AudioMediaData.PCMFormat;
                    }
                    else if (curWavUri.ToString() != eam.Uri.ToString())
                    {
                        curWavUri    = new Uri(eam.Uri.ToString());
                        curAudioData = new MemoryStream();
                        curPCMFormat = mam.AudioMediaData.PCMFormat;
                    }

                    Assert.IsTrue(curPCMFormat.ValueEquals(mam.AudioMediaData.PCMFormat),
                                  "Managed audio has incompatible pcm format");

                    Stream manAudio = mam.AudioMediaData.OpenPcmInputStream();
                    try
                    {
                        media.data.StreamUtils.CopyData(manAudio, curAudioData);
                    }
                    finally
                    {
                        manAudio.Close();
                    }
                }
            }
            foreach (TreeNode child in node.Children.ContentsAs_YieldEnumerable)
            {
                CheckPublishedFiles(child, sourceCh, destCh, curWavUri, curAudioData, curPCMFormat);
            }
        }
 /// <summary>
 /// Constructor setting the source <see cref="ChannelsProperty"/> of the event
 /// </summary>
 /// <param name="src">The source <see cref="ChannelsProperty"/> of the event</param>
 public ChannelsPropertyEventArgs(ChannelsProperty src)
     : base(src)
 {
     SourceChannelsProperty = src;
 }
Пример #10
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 !");
            }
        }