Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
0
        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);
        }