public bool HasOrInheritsAudio() { ManagedAudioMedia media = GetManagedAudioMedia(); if (media != null && media.IsWavAudioMediaData) { return(true); } #if ENABLE_SEQ_MEDIA SequenceMedia seqManagedAudioMedia = GetManagedAudioSequenceMedia(); if (seqManagedAudioMedia != null) { return(true); } #endif //ENABLE_SEQ_MEDIA TreeNode ancerstor = GetFirstAncestorWithManagedAudio(); if (ancerstor != null) { return(true); } return(false); }
public void isEmptySequenceReallyEmpty() { SequenceMedia obj = factory.CreateSequenceMedia(); Assert.AreEqual(true, obj.IsSequence); Assert.AreEqual(0, obj.ChildMedias.Count); Assert.AreEqual(false, obj.IsContinuous); Assert.AreEqual(false, obj.IsDiscrete); }
private long getByteOffset(TreeNode treeNode, ManagedAudioMedia managedMedia) { //if (!State.IsTreeNodeShownInAudioWaveForm(treeNode)) //{ // return 0; //} long byteOffset = 0; //Tuple<TreeNode, TreeNode> treeNodeSelection = m_UrakawaSession.GetTreeNodeSelection(); if (State.Audio.PlayStreamMarkers != null) { long bytesRight; long bytesLeft; int index; bool match = State.Audio.FindInPlayStreamMarkers(treeNode, out index, out bytesLeft, out bytesRight); if (match) { byteOffset = bytesLeft; } else { return(0); } } if (managedMedia == null) { return(byteOffset); } #if ENABLE_SEQ_MEDIA SequenceMedia seqManAudioMedia = treeNode.GetManagedAudioSequenceMedia(); if (seqManAudioMedia != null) { Debug.Fail("SequenceMedia is normally removed at import time...have you tried re-importing the DAISY book ?"); foreach (Media media in seqManAudioMedia.ChildMedias.ContentsAs_Enumerable) { var manMedia = (ManagedAudioMedia)media; if (media == managedMedia) { break; } byteOffset += manMedia.AudioMediaData.PCMFormat.Data.ConvertTimeToBytes(manMedia.Duration.AsLocalUnits); } } #endif //ENABLE_SEQ_MEDIA return(byteOffset); }
public void canSequenceMediaHoldOnlyOneMediaType() { SequenceMedia obj = factory.CreateSequenceMedia(); AbstractAudioMedia audio_obj = factory.Create <ExternalAudioMedia>(); AbstractTextMedia text_obj = factory.CreateTextMedia(); obj.InsertItem(obj.ChildMedias.Count, audio_obj); obj.InsertItem(obj.ChildMedias.Count, text_obj); Assert.Fail("The previous should have thrown an MediaNotAcceptable exception"); }
public Time GetDurationOfManagedAudioMediaFlattened() { ManagedAudioMedia audioMedia = GetManagedAudioMedia(); if (audioMedia != null) { Time dur_ = audioMedia.Duration; if (dur_.AsLocalUnits <= 0) { return(null); } return(dur_); } #if ENABLE_SEQ_MEDIA SequenceMedia seq = GetManagedAudioSequenceMedia(); if (seq != null) { Time durSeq = seq.GetDurationOfManagedAudioMedia(); if (durSeq != null) { return(durSeq); } } #endif //ENABLE_SEQ_MEDIA Time dur = new Time(); for (int index = 0; index < mChildren.Count; index++) { TreeNode node = mChildren.Get(index); Time childDur = node.GetDurationOfManagedAudioMediaFlattened(); if (childDur != null) { dur.Add(childDur); } } if (dur.AsLocalUnits <= 0) { return(null); } return(dur); }
public SequenceMedia GetManagedAudioSequenceMedia() { SequenceMedia seqAudioMedia = GetAudioSequenceMedia(); bool isSeqValid = seqAudioMedia != null && seqAudioMedia.ChildMedias.Count > 0 && !seqAudioMedia.AllowMultipleTypes; if (isSeqValid) { foreach (Media media in seqAudioMedia.ChildMedias.ContentsAs_Enumerable) { if (!(media is ManagedAudioMedia)) { isSeqValid = false; break; } } } if (isSeqValid) { return(seqAudioMedia); } return(null); }
private void parseSmil(string fullSmilPath) { if (RequestCancellation) { return; } XmlDocument smilXmlDoc = XmlReaderWriterHelper.ParseXmlDocument(fullSmilPath, false, false); if (RequestCancellation) { return; } //we skip SMIL metadata parsing (we get publication metadata only from OPF and DTBOOK/XHTMLs) //parseMetadata(smilXmlDoc); //XmlNodeList allTextNodes = smilXmlDoc.GetElementsByTagName("text"); //if (allTextNodes.Count == 0) //{ // return; //} //reportProgress(-1, "Parsing SMIL: [" + Path.GetFileName(fullSmilPath) + "]"); foreach (XmlNode textNode in XmlDocumentHelper.GetChildrenElementsOrSelfWithName(smilXmlDoc, true, "text", null, false)) { XmlAttributeCollection textNodeAttrs = textNode.Attributes; if (textNodeAttrs == null || textNodeAttrs.Count == 0) { continue; } XmlNode textNodeAttrSrc = textNodeAttrs.GetNamedItem("src"); if (textNodeAttrSrc == null || String.IsNullOrEmpty(textNodeAttrSrc.Value)) { continue; } string src = FileDataProvider.UriDecode(textNodeAttrSrc.Value); int index = src.LastIndexOf('#'); if (index == src.Length - 1) { return; } string srcFragmentId = src.Substring(index + 1); TreeNode textTreeNode = m_Project.Presentations.Get(0).RootNode.GetFirstDescendantOrSelfWithXmlID(srcFragmentId); if (textTreeNode == null) { continue; } ManagedAudioMedia textTreeNodeAudio = textTreeNode.GetManagedAudioMedia(); if (textTreeNodeAudio != null) { //Ignore. continue; } XmlNode parent = textNode.ParentNode; if (parent != null && parent.LocalName == "a") { parent = parent.ParentNode; } if (parent == null) { continue; } if (parent.LocalName != "par") { //System.Diagnostics.Debug.Fail("Text node in SMIL has no parallel time container as parent ! {0}", parent.Name); continue; } XmlNodeList textPeers = parent.ChildNodes; foreach (XmlNode textPeerNode in textPeers) { if (RequestCancellation) { return; } if (textPeerNode.NodeType != XmlNodeType.Element) { continue; } if (textPeerNode.LocalName == "audio") { addAudio(textTreeNode, textPeerNode, false, fullSmilPath); break; } else if (textPeerNode.LocalName == "a") { XmlNodeList aChildren = textPeerNode.ChildNodes; foreach (XmlNode aChild in aChildren) { if (aChild.LocalName == "audio") { addAudio(textTreeNode, aChild, false, fullSmilPath); break; } } } else if (textPeerNode.LocalName == "seq") { #if DEBUG Debugger.Break(); #endif //DEBUG XmlNodeList seqChildren = textPeerNode.ChildNodes; foreach (XmlNode seqChild in seqChildren) { if (seqChild.LocalName == "audio") { addAudio(textTreeNode, seqChild, true, fullSmilPath); } } #if ENABLE_SEQ_MEDIA SequenceMedia seqManAudioMedia = textTreeNode.GetManagedAudioSequenceMedia(); if (seqManAudioMedia == null) { Debug.Fail("This should never happen !"); break; } ManagedAudioMedia managedAudioMedia = textTreeNode.Presentation.MediaFactory.CreateManagedAudioMedia(); AudioMediaData mediaData = textTreeNode.Presentation.MediaDataFactory.CreateAudioMediaData(); managedAudioMedia.AudioMediaData = mediaData; foreach (Media seqChild in seqManAudioMedia.ChildMedias.ContentsAs_Enumerable) { ManagedAudioMedia seqManMedia = (ManagedAudioMedia)seqChild; // 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 ! mediaData.MergeWith(seqManMedia.AudioMediaData); //Stream stream = seqManMedia.AudioMediaData.OpenPcmInputStream(); //try //{ // mediaData.AppendPcmData(stream, null); //} //finally //{ // stream.Close(); //} //seqManMedia.AudioMediaData.Delete(); // doesn't actually removes the FileDataProviders (need to call Presentation.Cleanup()) ////textTreeNode.Presentation.DataProviderManager.RemoveDataProvider(); } ChannelsProperty chProp = textTreeNode.GetChannelsProperty(); chProp.SetMedia(m_audioChannel, null); chProp.SetMedia(m_audioChannel, managedAudioMedia); #endif //ENABLE_SEQ_MEDIA break; } } } }
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 !"); } }
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); } }
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); }
OpenPcmInputStreamOfManagedAudioMedia(bool openSecondaryStream) { StreamWithMarkers val = new StreamWithMarkers(); //#if USE_NORMAL_LIST // StreamWithMarkers val; //#else // StreamWithMarkers val = new StreamWithMarkers(); //#endif //USE_NORMAL_LIST ManagedAudioMedia audioMedia = GetManagedAudioMedia(); if (audioMedia != null && audioMedia.IsWavAudioMediaData) { val.m_Stream = audioMedia.AudioMediaData.OpenPcmInputStream(); if (openSecondaryStream) { val.m_SecondaryStream = audioMedia.AudioMediaData.OpenPcmInputStream(); } val.m_SubStreamMarkers = new #if USE_NORMAL_LIST List #else LightLinkedList #endif //USE_NORMAL_LIST <TreeNodeAndStreamDataLength>( #if USE_NORMAL_LIST 1 #endif //USE_NORMAL_LIST ); TreeNodeAndStreamDataLength tnasdl = new TreeNodeAndStreamDataLength(); tnasdl.m_LocalStreamDataLength = val.m_Stream.Length; tnasdl.m_TreeNode = this; val.m_SubStreamMarkers.Add(tnasdl); return(val); } #if ENABLE_SEQ_MEDIA SequenceMedia seq = GetManagedAudioSequenceMedia(); if (seq != null) { Stream stream = seq.OpenPcmInputStreamOfManagedAudioMedia(); if (stream != null) { val.m_Stream = stream; val.m_SubStreamMarkers = new #if USE_NORMAL_LIST List #else LightLinkedList #endif //USE_NORMAL_LIST <TreeNodeAndStreamDataLength>( #if USE_NORMAL_LIST 1 #endif //USE_NORMAL_LIST ); TreeNodeAndStreamDataLength tnasdl = new TreeNodeAndStreamDataLength(); tnasdl.m_LocalStreamDataLength = val.m_Stream.Length; tnasdl.m_TreeNode = this; val.m_SubStreamMarkers.Add(tnasdl); return(val); } } #endif //ENABLE_SEQ_MEDIA return(null); }
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 !"); } }