Esempio n. 1
0
            private Stream SetPlayStream_FromFile_OPEN(
                FileStream fileStream,
                string filePathOptionalInfo)
            {
                Stream stream = fileStream;

                if (stream != null)
                {
                    stream.Position = 0;
                    stream.Seek(0, SeekOrigin.Begin);

                    uint dataLength;
                    AudioLibPCMFormat format = AudioLibPCMFormat.RiffHeaderParse(stream, out dataLength);

                    PcmFormat = new PCMFormatInfo(format);

                    dataLength = (uint)(stream.Length - stream.Position);

                    stream = new SubStream(stream, stream.Position, dataLength, filePathOptionalInfo);

                    DebugFix.Assert(dataLength == stream.Length);

                    DataLength            = stream.Length;
                    EndOffsetOfPlayStream = DataLength;
                }

                return(stream);
            }
        /// <summary>
        /// Replaces with audio from a RIFF Wave file of a given duration at a given replace point
        /// </summary>
        /// <param name="riffWaveStream">The RIFF Wave file</param>
        /// <param name="replacePoint">The given replace point</param>
        /// <param name="duration">The duration of the audio to replace</param>
        public void ReplacePcmData_RiffHeader(Stream riffWaveStream, Time replacePoint, Time duration)
        {
            if (OriginalRelativePath != null && DataProvider != null)
            {
                throw new NotImplementedException();
            }

            uint dataLength;
            AudioLibPCMFormat format = AudioLibPCMFormat.RiffHeaderParse(riffWaveStream, out dataLength);

            if (!format.IsCompatibleWith(PCMFormat.Data))
            {
                throw new exception.InvalidDataFormatException(
                          String.Format("RIFF WAV file has incompatible PCM format"));
            }

            Time fileDuration = new Time(format.ConvertBytesToTime(dataLength));

            if (fileDuration.IsLessThan(duration))
            {
                throw new exception.MethodParameterIsOutOfBoundsException(String.Format(
                                                                              "Can not insert {0} of audio from RIFF Wave file since the file's duration is only {1}",
                                                                              duration, fileDuration));
            }
            ReplacePcmData(riffWaveStream, replacePoint, duration);
        }
        private AudioLibPCMFormat GetInfo(string name, out uint dataLength)
        {
            FileStream fs = new FileStream(GetPath(name), FileMode.Open, FileAccess.Read, FileShare.Read);

            try
            {
                return(AudioLibPCMFormat.RiffHeaderParse(fs, out dataLength));
            }
            finally
            {
                fs.Close();
            }
        }
        /// <summary>
        /// Appends audio data from a RIFF Wave file
        /// </summary>
        /// <param name="riffWaveStream">The RIFF Wave file</param>
        public void AppendPcmData_RiffHeader(Stream riffWaveStream)
        {
            if (OriginalRelativePath != null && DataProvider != null)
            {
                throw new NotImplementedException();
            }

            uint dataLength;
            AudioLibPCMFormat format = AudioLibPCMFormat.RiffHeaderParse(riffWaveStream, out dataLength);

            if (dataLength <= 0)
            {
                dataLength = (uint)(riffWaveStream.Length - riffWaveStream.Position);
            }

            if (!format.IsCompatibleWith(PCMFormat.Data))
            {
                throw new exception.InvalidDataFormatException(
                          String.Format("RIFF WAV file has incompatible PCM format"));
            }

            AppendPcmData(riffWaveStream, new Time(format.ConvertBytesToTime(dataLength)));
        }
Esempio n. 5
0
        private void diagramXmlParseBody_(XmlNode diagramElementNode, string xmlFilePath, TreeNode treeNode, int objectIndex)
        {
            string diagramElementName = diagramElementNode.Name;

            AlternateContent           altContent     = treeNode.Presentation.AlternateContentFactory.CreateAlternateContent();
            AlternateContentAddCommand cmd_AltContent =
                treeNode.Presentation.CommandFactory.CreateAlternateContentAddCommand(treeNode, altContent);

            treeNode.Presentation.UndoRedoManager.Execute(cmd_AltContent);



            Metadata diagramElementName_Metadata = treeNode.Presentation.MetadataFactory.CreateMetadata();

            diagramElementName_Metadata.NameContentAttribute              = new MetadataAttribute();
            diagramElementName_Metadata.NameContentAttribute.Name         = DiagramContentModelHelper.DiagramElementName;
            diagramElementName_Metadata.NameContentAttribute.NamespaceUri = null;
            diagramElementName_Metadata.NameContentAttribute.Value        = diagramElementName;
            AlternateContentMetadataAddCommand cmd_AltContent_diagramElementName_Metadata =
                treeNode.Presentation.CommandFactory.CreateAlternateContentMetadataAddCommand(
                    treeNode,
                    null,
                    altContent,
                    diagramElementName_Metadata,
                    null
                    );

            treeNode.Presentation.UndoRedoManager.Execute(cmd_AltContent_diagramElementName_Metadata);


            if (diagramElementNode.Attributes != null)
            {
                for (int i = 0; i < diagramElementNode.Attributes.Count; i++)
                {
                    XmlAttribute attribute = diagramElementNode.Attributes[i];


                    if (attribute.Name.StartsWith(XmlReaderWriterHelper.NS_PREFIX_XMLNS + ":"))
                    {
                        //
                    }
                    else if (attribute.Name == XmlReaderWriterHelper.NS_PREFIX_XMLNS)
                    {
                        //
                    }
                    else if (attribute.Name == DiagramContentModelHelper.TOBI_Audio)
                    {
                        string fullPath = null;
                        if (FileDataProvider.isHTTPFile(attribute.Value))
                        {
                            fullPath = FileDataProvider.EnsureLocalFilePathDownloadTempDirectory(attribute.Value);
                        }
                        else
                        {
                            fullPath = Path.Combine(Path.GetDirectoryName(xmlFilePath), attribute.Value);
                        }
                        if (fullPath != null && File.Exists(fullPath))
                        {
                            string extension = Path.GetExtension(fullPath);

                            bool isWav = extension.Equals(DataProviderFactory.AUDIO_WAV_EXTENSION, StringComparison.OrdinalIgnoreCase);

                            AudioLibPCMFormat wavFormat = null;
                            if (isWav)
                            {
                                Stream fileStream = File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                                try
                                {
                                    uint dataLength;
                                    wavFormat = AudioLibPCMFormat.RiffHeaderParse(fileStream, out dataLength);
                                }
                                finally
                                {
                                    fileStream.Close();
                                }
                            }
                            string originalFilePath = null;

                            DebugFix.Assert(treeNode.Presentation.MediaDataManager.EnforceSinglePCMFormat);

                            bool wavNeedsConversion = false;
                            if (wavFormat != null)
                            {
                                wavNeedsConversion = !wavFormat.IsCompatibleWith(treeNode.Presentation.MediaDataManager.DefaultPCMFormat.Data);
                            }
                            if (!isWav || wavNeedsConversion)
                            {
                                originalFilePath = fullPath;

                                var audioFormatConvertorSession =
                                    new AudioFormatConvertorSession(
                                        //AudioFormatConvertorSession.TEMP_AUDIO_DIRECTORY,
                                        treeNode.Presentation.DataProviderManager.DataFileDirectoryFullPath,
                                        treeNode.Presentation.MediaDataManager.DefaultPCMFormat,
                                        false,
                                        m_UrakawaSession.IsAcmCodecsDisabled);

                                //filePath = m_AudioFormatConvertorSession.ConvertAudioFileFormat(filePath);

                                bool cancelled = false;

                                var converter = new AudioClipConverter(audioFormatConvertorSession, fullPath);

                                bool error = ShellView.RunModalCancellableProgressTask(true,
                                                                                       "Converting audio...",
                                                                                       converter,
                                                                                       () =>
                                {
                                    m_Logger.Log(@"Audio conversion CANCELLED", Category.Debug, Priority.Medium);
                                    cancelled = true;
                                },
                                                                                       () =>
                                {
                                    m_Logger.Log(@"Audio conversion DONE", Category.Debug, Priority.Medium);
                                    cancelled = false;
                                });

                                if (cancelled)
                                {
                                    //DebugFix.Assert(!result);
                                    break;
                                }

                                fullPath = converter.ConvertedFilePath;
                                if (string.IsNullOrEmpty(fullPath))
                                {
                                    break;
                                }

                                m_Logger.Log(string.Format("Converted audio {0} to {1}", originalFilePath, fullPath),
                                             Category.Debug, Priority.Medium);

                                //Stream fileStream = File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                                //try
                                //{
                                //    uint dataLength;
                                //    wavFormat = AudioLibPCMFormat.RiffHeaderParse(fileStream, out dataLength);
                                //}
                                //finally
                                //{
                                //    fileStream.Close();
                                //}
                            }


                            ManagedAudioMedia manAudioMedia  = treeNode.Presentation.MediaFactory.CreateManagedAudioMedia();
                            AudioMediaData    audioMediaData = treeNode.Presentation.MediaDataFactory.CreateAudioMediaData(DataProviderFactory.AUDIO_WAV_EXTENSION);
                            manAudioMedia.AudioMediaData = audioMediaData;

                            FileDataProvider dataProv = (FileDataProvider)treeNode.Presentation.DataProviderFactory.Create(DataProviderFactory.AUDIO_WAV_MIME_TYPE);
                            dataProv.InitByCopyingExistingFile(fullPath);
                            audioMediaData.AppendPcmData(dataProv);

                            //                            Stream wavStream = null;
                            //                            try
                            //                            {
                            //                                wavStream = File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read);

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

                            //                                if (!treeNode.Presentation.MediaDataManager.DefaultPCMFormat.Data.IsCompatibleWith(pcmInfo))
                            //                                {
                            //#if DEBUG
                            //                                    Debugger.Break();
                            //#endif //DEBUG
                            //                                    wavStream.Close();
                            //                                    wavStream = null;

                            //                                    var audioFormatConvertorSession =
                            //                                        new AudioFormatConvertorSession(
                            //                                        //AudioFormatConvertorSession.TEMP_AUDIO_DIRECTORY,
                            //                                        treeNode.Presentation.DataProviderManager.DataFileDirectoryFullPath,
                            //                                    treeNode.Presentation.MediaDataManager.DefaultPCMFormat, m_UrakawaSession.IsAcmCodecsDisabled);

                            //                                    string newfullWavPath = audioFormatConvertorSession.ConvertAudioFileFormat(fullPath);

                            //                                    FileDataProvider dataProv = (FileDataProvider)treeNode.Presentation.DataProviderFactory.Create(DataProviderFactory.AUDIO_WAV_MIME_TYPE);
                            //                                    dataProv.InitByMovingExistingFile(newfullWavPath);
                            //                                    audioMediaData.AppendPcmData(dataProv);
                            //                                }
                            //                                else // use original wav file by copying it to data directory
                            //                                {
                            //                                    FileDataProvider dataProv = (FileDataProvider)treeNode.Presentation.DataProviderFactory.Create(DataProviderFactory.AUDIO_WAV_MIME_TYPE);
                            //                                    dataProv.InitByCopyingExistingFile(fullPath);
                            //                                    audioMediaData.AppendPcmData(dataProv);
                            //                                }
                            //                            }
                            //                            finally
                            //                            {
                            //                                if (wavStream != null) wavStream.Close();
                            //                            }



                            AlternateContentSetManagedMediaCommand cmd_AltContent_diagramElement_Audio =
                                treeNode.Presentation.CommandFactory.CreateAlternateContentSetManagedMediaCommand(treeNode, altContent, manAudioMedia);
                            treeNode.Presentation.UndoRedoManager.Execute(cmd_AltContent_diagramElement_Audio);

                            //SetDescriptionAudio(altContent, audio, treeNode);
                        }
                    }
                    else
                    {
                        Metadata diagramElementAttribute_Metadata = treeNode.Presentation.MetadataFactory.CreateMetadata();
                        diagramElementAttribute_Metadata.NameContentAttribute              = new MetadataAttribute();
                        diagramElementAttribute_Metadata.NameContentAttribute.Name         = attribute.Name;
                        diagramElementAttribute_Metadata.NameContentAttribute.NamespaceUri = attribute.NamespaceURI;
                        diagramElementAttribute_Metadata.NameContentAttribute.Value        = attribute.Value;
                        AlternateContentMetadataAddCommand cmd_AltContent_diagramElementAttribute_Metadata =
                            treeNode.Presentation.CommandFactory.CreateAlternateContentMetadataAddCommand(
                                treeNode,
                                null,
                                altContent,
                                diagramElementAttribute_Metadata,
                                null
                                );
                        treeNode.Presentation.UndoRedoManager.Execute(
                            cmd_AltContent_diagramElementAttribute_Metadata);
                    }
                }
            }

            int nObjects = -1;

            XmlNode textNode = diagramElementNode;

            if (diagramElementName == DiagramContentModelHelper.D_SimplifiedImage ||
                diagramElementName == DiagramContentModelHelper.D_Tactile)
            {
                string  localTourName = DiagramContentModelHelper.StripNSPrefix(DiagramContentModelHelper.D_Tour);
                XmlNode tour          =
                    XmlDocumentHelper.GetFirstChildElementOrSelfWithName(diagramElementNode, false,
                                                                         localTourName,
                                                                         DiagramContentModelHelper.NS_URL_DIAGRAM);
                textNode = tour;

                IEnumerable <XmlNode> objects = XmlDocumentHelper.GetChildrenElementsOrSelfWithName(diagramElementNode, false,
                                                                                                    DiagramContentModelHelper.
                                                                                                    Object,
                                                                                                    DiagramContentModelHelper.
                                                                                                    NS_URL_ZAI, false);
                nObjects = 0;
                foreach (XmlNode obj in objects)
                {
                    nObjects++;
                }

                int i = -1;
                foreach (XmlNode obj in objects)
                {
                    i++;
                    if (i != objectIndex)
                    {
                        continue;
                    }

                    if (obj.Attributes == null || obj.Attributes.Count <= 0)
                    {
                        break;
                    }

                    for (int j = 0; j < obj.Attributes.Count; j++)
                    {
                        XmlAttribute attribute = obj.Attributes[j];


                        if (attribute.Name.StartsWith(XmlReaderWriterHelper.NS_PREFIX_XMLNS + ":"))
                        {
                            //
                        }
                        else if (attribute.Name == XmlReaderWriterHelper.NS_PREFIX_XMLNS)
                        {
                            //
                        }
                        else if (attribute.Name == DiagramContentModelHelper.Src)
                        {
                            //
                        }
                        else if (attribute.Name == DiagramContentModelHelper.SrcType)
                        {
                            //
                        }
                        else
                        {
                            Metadata diagramElementAttribute_Metadata = treeNode.Presentation.MetadataFactory.CreateMetadata();
                            diagramElementAttribute_Metadata.NameContentAttribute              = new MetadataAttribute();
                            diagramElementAttribute_Metadata.NameContentAttribute.Name         = attribute.Name;
                            diagramElementAttribute_Metadata.NameContentAttribute.NamespaceUri = attribute.NamespaceURI;
                            diagramElementAttribute_Metadata.NameContentAttribute.Value        = attribute.Value;
                            AlternateContentMetadataAddCommand cmd_AltContent_diagramElementAttribute_Metadata =
                                treeNode.Presentation.CommandFactory.CreateAlternateContentMetadataAddCommand(
                                    treeNode,
                                    null,
                                    altContent,
                                    diagramElementAttribute_Metadata,
                                    null
                                    );
                            treeNode.Presentation.UndoRedoManager.Execute(
                                cmd_AltContent_diagramElementAttribute_Metadata);
                        }
                    }

                    XmlAttribute srcAttr = (XmlAttribute)obj.Attributes.GetNamedItem(DiagramContentModelHelper.Src);
                    if (srcAttr != null)
                    {
                        XmlAttribute srcType =
                            (XmlAttribute)obj.Attributes.GetNamedItem(DiagramContentModelHelper.SrcType);

                        ManagedImageMedia img = treeNode.Presentation.MediaFactory.CreateManagedImageMedia();

                        string imgFullPath = null;
                        if (FileDataProvider.isHTTPFile(srcAttr.Value))
                        {
                            imgFullPath = FileDataProvider.EnsureLocalFilePathDownloadTempDirectory(srcAttr.Value);
                        }
                        else
                        {
                            imgFullPath = Path.Combine(Path.GetDirectoryName(xmlFilePath), srcAttr.Value);
                        }
                        if (imgFullPath != null && File.Exists(imgFullPath))
                        {
                            string extension = Path.GetExtension(imgFullPath);

                            ImageMediaData imgData = treeNode.Presentation.MediaDataFactory.CreateImageMediaData(extension);
                            if (imgData != null)
                            {
                                imgData.InitializeImage(imgFullPath, Path.GetFileName(imgFullPath));
                                img.ImageMediaData = imgData;

                                AlternateContentSetManagedMediaCommand cmd_AltContent_Image =
                                    treeNode.Presentation.CommandFactory.CreateAlternateContentSetManagedMediaCommand(
                                        treeNode, altContent, img);
                                treeNode.Presentation.UndoRedoManager.Execute(cmd_AltContent_Image);
                            }
                        }
                    }
                }
            }

            if (textNode != null)
            {
                string strText = textNode.InnerXml;

                if (!string.IsNullOrEmpty(strText))
                {
                    strText = strText.Trim();
                    strText = Regex.Replace(strText, @"\s+", " ");
                    strText = strText.Replace("\r\n", "\n");
                }

                if (!string.IsNullOrEmpty(strText))
                {
                    TextMedia txtMedia = treeNode.Presentation.MediaFactory.CreateTextMedia();
                    txtMedia.Text = strText;
                    AlternateContentSetManagedMediaCommand cmd_AltContent_Text =
                        treeNode.Presentation.CommandFactory.CreateAlternateContentSetManagedMediaCommand(treeNode,
                                                                                                          altContent,
                                                                                                          txtMedia);
                    treeNode.Presentation.UndoRedoManager.Execute(cmd_AltContent_Text);
                }
            }

            if (nObjects > 0 && ++objectIndex <= nObjects - 1)
            {
                diagramXmlParseBody_(diagramElementNode, xmlFilePath, treeNode, objectIndex);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// takes wav file / mp3 file as input and converts it to wav file with audio format info supplied as parameter
        /// </summary>
        /// <param name="SourceFilePath"></param>
        /// <param name="destinationDirectory"></param>
        /// <param name="destinationFormatInfo"></param>
        /// <returns> full file path of converted file  </returns>
        private string ConvertToDefaultFormat(string SourceFilePath, string destinationDirectory, PCMFormatInfo destinationFormatInfo, bool skipACM)
        {
            if (!File.Exists(SourceFilePath))
            {
                throw new FileNotFoundException(SourceFilePath);
            }

            if (!Directory.Exists(destinationDirectory))
            {
                FileDataProvider.CreateDirectory(destinationDirectory);
            }

            AudioFileType sourceFileType = GetAudioFileType(SourceFilePath);

            switch (sourceFileType)
            {
            case AudioFileType.WavUncompressed:
            case AudioFileType.WavCompressed:
            {
                if (FirstDiscoveredPCMFormat == null)
                {
                    Stream wavStream = null;
                    try
                    {
                        wavStream = File.Open(SourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);

                        uint dataLength;
                        AudioLibPCMFormat pcmInfo = null;

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

                        //FirstDiscoveredPCMFormat = new PCMFormatInfo(pcmInfo);
                        FirstDiscoveredPCMFormat = new AudioLibPCMFormat();
                        FirstDiscoveredPCMFormat.CopyFrom(pcmInfo);
                    }
                    finally
                    {
                        if (wavStream != null)
                        {
                            wavStream.Close();
                        }
                    }
                }

                WavFormatConverter formatConverter1 = new WavFormatConverter(true, skipACM);

                AddSubCancellable(formatConverter1);

                // Preserve existing WAV PCM format, the call below to ConvertSampleRate detects the equality of PCM formats and copies the audio file instead of resampling.
                AudioLibPCMFormat pcmFormat = m_autoDetectPcmFormat ? FirstDiscoveredPCMFormat :
                                              (destinationFormatInfo != null ? destinationFormatInfo.Data : new AudioLibPCMFormat());


                string result = null;
                try
                {
                    AudioLibPCMFormat originalPcmFormat;
                    result = formatConverter1.ConvertSampleRate(SourceFilePath, destinationDirectory,
                                                                pcmFormat,
                                                                out originalPcmFormat);
                    if (originalPcmFormat != null && FirstDiscoveredPCMFormat != null)
                    {
                        DebugFix.Assert(FirstDiscoveredPCMFormat.Equals(originalPcmFormat));
                    }
                }
                finally
                {
                    RemoveSubCancellable(formatConverter1);
                }

                return(result);
            }

            case AudioFileType.Mp4_AAC:
            case AudioFileType.Mp3:
            {
                WavFormatConverter formatConverter2 = new WavFormatConverter(true, skipACM);

                AddSubCancellable(formatConverter2);

                string result = null;
                try
                {
                    AudioLibPCMFormat pcmFormat = m_autoDetectPcmFormat ? FirstDiscoveredPCMFormat :         // can be null!
                                                  (destinationFormatInfo != null ? destinationFormatInfo.Data : new AudioLibPCMFormat());

                    AudioLibPCMFormat originalPcmFormat;
                    if (sourceFileType == AudioFileType.Mp3)
                    {
                        result = formatConverter2.UnCompressMp3File(SourceFilePath, destinationDirectory,
                                                                    pcmFormat,
                                                                    out originalPcmFormat);
                    }
                    else
                    {
                        DebugFix.Assert(sourceFileType == AudioFileType.Mp4_AAC);

                        result = formatConverter2.UnCompressMp4_AACFile(SourceFilePath, destinationDirectory,
                                                                        pcmFormat,
                                                                        out originalPcmFormat);
                    }

                    if (originalPcmFormat != null)
                    {
                        if (FirstDiscoveredPCMFormat == null)
                        {
                            //FirstDiscoveredPCMFormat = new PCMFormatInfo(originalPcmFormat);
                            FirstDiscoveredPCMFormat = new AudioLibPCMFormat();
                            FirstDiscoveredPCMFormat.CopyFrom(originalPcmFormat);
                        }
                    }
                }
                finally
                {
                    RemoveSubCancellable(formatConverter2);
                }

                return(result);
            }

            default:
                throw new Exception("Source file format not supported");
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Gets an input <see cref="Stream"/> providing read access to the raw PCM audio data
        /// between given sub-clip begin and end times
        /// </summary>
        /// <param name="subClipBegin">The beginning of the sub-clip</param>
        /// <param name="subClipEnd">The end of the sub-clip</param>
        /// <returns>The raw PCM audio data <see cref="Stream"/></returns>
        /// <remarks>
        /// <para>Sub-clip times must be in the interval <c>[0;this.getAudioDuration()]</c>.</para>
        /// <para>
        /// The sub-clip is
        /// relative to clip begin of the WavClip, that if <c>this.getClipBegin()</c>
        /// returns <c>00:00:10</c>, <c>this.getClipEnd()</c> returns <c>00:00:50</c>,
        /// <c>x</c> and <c>y</c> is <c>00:00:05</c> and <c>00:00:30</c> respectively,
        /// then <c>this.GetAudioData(x, y)</c> will get the audio in the underlying wave audio between
        /// <c>00:00:15</c> and <c>00:00:40</c>
        /// </para>
        /// </remarks>
        public Stream OpenPcmInputStream(Time subClipBegin, Time subClipEnd)
        {
            if (subClipBegin == null)
            {
                throw new exception.MethodParameterIsNullException("subClipBegin must not be null");
            }
            if (subClipEnd == null)
            {
                throw new exception.MethodParameterIsNullException("subClipEnd must not be null");
            }
            if (
                subClipBegin.IsLessThan(Time.Zero) ||
                subClipEnd.IsLessThan(subClipBegin) ||
                subClipEnd.IsGreaterThan(Duration)
                )
            {
                string msg = String.Format(
                    "subClipBegin/subClipEnd [{0};{1}] not within ([0;{2}])",
                    subClipBegin, subClipEnd, Duration);
                throw new exception.MethodParameterIsOutOfBoundsException(msg);
            }

            Stream            raw = DataProvider.OpenInputStream();
            uint              dataLength;
            AudioLibPCMFormat format     = AudioLibPCMFormat.RiffHeaderParse(raw, out dataLength);
            Time              rawEndTime = new Time(format.ConvertBytesToTime(dataLength));

#if DEBUG
            DebugFix.Assert(rawEndTime.IsEqualTo(MediaDuration));
#endif

            //Time rawEndTime = Time.Zero.Add(MediaDuration); // We don't call this to avoid unnecessary I/O (Strem.Open() twice)

            if (
                ClipBegin.IsLessThan(Time.Zero) ||
                ClipBegin.IsGreaterThan(ClipEnd) ||
                ClipEnd.IsGreaterThan(rawEndTime)
                )
            {
                string msg = String.Format(
                    "WavClip [{0};{1}] is empty or not within the underlying wave data stream ([0;{2}])",
                    ClipBegin, ClipEnd, rawEndTime);
                throw new exception.InvalidDataFormatException(msg);
            }

            /*
             * Time clipDuration = Duration;
             * if (subClipBegin.IsEqualTo(Time.Zero) && subClipEnd.IsEqualTo(Time.Zero.Add(clipDuration)))
             * {
             *  // Stream.Position is at the end of the RIFF header, we need to bring it back to the begining
             *  return new SubStream(
             *  raw,
             *  raw.Position, raw.Length - raw.Position);
             * }
             */

            //Time rawClipBegin = new Time(ClipBegin.AsTimeSpan + subClipBegin.AsTimeSpan);
            //Time rawClipEnd = new Time(ClipBegin.AsTimeSpan + subClipEnd.AsTimeSpan);

            long ClipBegin_AsLocalUnits = ClipBegin.AsLocalUnits;

            long posRiffHeader = raw.Position; //44

            long beginPos = posRiffHeader + format.ConvertTimeToBytes(ClipBegin_AsLocalUnits + subClipBegin.AsLocalUnits);
            long endPos   = posRiffHeader + format.ConvertTimeToBytes(ClipBegin_AsLocalUnits + subClipEnd.AsLocalUnits);

            long rawLen = raw.Length;
#if DEBUG
            long rawLenCheck = posRiffHeader + dataLength;
            DebugFix.Assert(rawLen == rawLenCheck);
#endif
            if (endPos > rawLen)
            {
//#if DEBUG
//                Debugger.Break();
//#endif
                endPos = rawLen;
            }

            long len = endPos - beginPos;

            return(new SubStream(
                       raw,
                       beginPos,
                       len,
                       DataProvider is FileDataProvider ? ((FileDataProvider)DataProvider).DataFileFullPath : null));
        }
Esempio n. 8
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 !");
            }
        }
Esempio n. 9
0
        private void checkAndAddDeferredRecordingDataItems()
        {
            if (m_DeferredRecordingDataItems == null)
            {
                return;
            }

            IsAutoPlay = false;

            bool needsRefresh = false;

            bool skipDrawing = Settings.Default.AudioWaveForm_DisableDraw;

            Settings.Default.AudioWaveForm_DisableDraw = true;


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

            //#endif


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

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

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

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

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

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

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

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

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

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

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

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

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

                needsRefresh = true;

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

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

            m_DeferredRecordingDataItems = null;

            Settings.Default.AudioWaveForm_DisableDraw = skipDrawing;

            if (needsRefresh)
            {
                CommandRefresh.Execute();
            }
        }
Esempio n. 10
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);
            }
        }
Esempio n. 11
0
        public static string ConvertedFile(string filePath, Presentation pres)
        {
            AudioLib.WavFormatConverter audioConverter = new WavFormatConverter(true, true);
            int    samplingRate  = (int)pres.MediaDataManager.DefaultPCMFormat.Data.SampleRate;
            int    channels      = pres.MediaDataManager.DefaultPCMFormat.Data.NumberOfChannels;
            int    bitDepth      = pres.MediaDataManager.DefaultPCMFormat.Data.BitDepth;
            string directoryPath = pres.DataProviderManager.DataFileDirectoryFullPath;
            string convertedFile = null;

            try
            {
                if (Path.GetExtension(filePath).ToLower() == ".wav")
                {
                    Stream wavStream = null;
                    wavStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    uint dataLength;
                    AudioLibPCMFormat newFilePCMInfo = AudioLibPCMFormat.RiffHeaderParse(wavStream, out dataLength);
                    if (wavStream != null)
                    {
                        wavStream.Close();
                    }
                    if (newFilePCMInfo.SampleRate == samplingRate && newFilePCMInfo.NumberOfChannels == channels && newFilePCMInfo.BitDepth == bitDepth)
                    {
                        convertedFile = filePath;
                    }
                    else
                    {
                        AudioLibPCMFormat pcmFormat         = new AudioLibPCMFormat((ushort)channels, (uint)samplingRate, (ushort)bitDepth);
                        AudioLibPCMFormat originalPCMFormat = null;
                        convertedFile = audioConverter.ConvertSampleRate(filePath, directoryPath, pcmFormat, out originalPCMFormat);
                    }
                }
                else if (Path.GetExtension(filePath).ToLower() == ".mp3")
                {
                    AudioLibPCMFormat pcmFormat         = new AudioLibPCMFormat((ushort)channels, (uint)samplingRate, (ushort)bitDepth);
                    AudioLibPCMFormat originalPCMFormat = null;
                    convertedFile = audioConverter.UnCompressMp3File(filePath, directoryPath, pcmFormat, out originalPCMFormat);
                }
                else if (Path.GetExtension(filePath).ToLower() == ".mp4" || Path.GetExtension(filePath).ToLower() == ".m4a")
                {
                    AudioLibPCMFormat pcmFormat         = new AudioLibPCMFormat((ushort)channels, (uint)samplingRate, (ushort)bitDepth);
                    AudioLibPCMFormat originalPCMFormat = null;
                    convertedFile = audioConverter.UnCompressMp4_AACFile(filePath, directoryPath, pcmFormat, out originalPCMFormat);
                }
                else
                {
                    MessageBox.Show(string.Format(Localizer.Message("AudioFormatConverter_Error_FileExtentionNodSupported"), filePath), Localizer.Message("Caption_Error"));
                    return(null);
                }
                // rename converted file to original file if names are different
                if (Path.GetFileName(filePath) != Path.GetFileName(convertedFile))
                {
                    string newConvertedFilePath = Path.Combine(Path.GetDirectoryName(convertedFile), Path.GetFileNameWithoutExtension(filePath) + ".wav");
                    for (int i = 0; i < 99999 && File.Exists(newConvertedFilePath); i++)
                    {
                        newConvertedFilePath = Path.Combine(Path.GetDirectoryName(convertedFile), i.ToString() + Path.GetFileNameWithoutExtension(filePath) + ".wav");
                        if (!File.Exists(newConvertedFilePath))
                        {
                            MessageBox.Show(string.Format(Localizer.Message("Import_AudioFormat_RenameFile"), Path.GetFileNameWithoutExtension(filePath) + ".wav", Path.GetFileName(newConvertedFilePath)),
                                            Localizer.Message("Caption_Information"),
                                            MessageBoxButtons.OK);
                            break;
                        }
                    }
                    File.Move(convertedFile, newConvertedFilePath);
                    convertedFile = newConvertedFilePath;
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return(null);
            }
            return(convertedFile);
        }
Esempio n. 12
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);
            }
        }
Esempio n. 13
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 !");
            }
        }