Example #1
0
        private void updateTotalDuration(Command cmd, bool done)
        {
            if (cmd is ManagedAudioMediaInsertDataCommand)
            {
                var command = (ManagedAudioMediaInsertDataCommand)cmd;

                long dur = command.ManagedAudioMediaSource.Duration.AsLocalUnits;
                if (done)
                {
                    TotalDocumentAudioDurationInLocalUnits += dur;
                    TotalSessionAudioDurationInLocalUnits  += dur;
                }
                else
                {
                    TotalDocumentAudioDurationInLocalUnits -= dur;
                    TotalSessionAudioDurationInLocalUnits  -= dur;
                }
            }
            else if (cmd is TreeNodeSetManagedAudioMediaCommand)
            {
                var command = (TreeNodeSetManagedAudioMediaCommand)cmd;

                long dur = command.ManagedAudioMedia.Duration.AsLocalUnits;
                if (done)
                {
                    TotalDocumentAudioDurationInLocalUnits += dur;
                    TotalSessionAudioDurationInLocalUnits  += dur;
                }
                else
                {
                    TotalDocumentAudioDurationInLocalUnits -= dur;
                    TotalSessionAudioDurationInLocalUnits  -= dur;
                }
            }
            else if (cmd is TreeNodeAudioStreamDeleteCommand)
            {
                var command = (TreeNodeAudioStreamDeleteCommand)cmd;

                Time timeBegin = command.SelectionData.m_LocalStreamLeftMark == -1
                    ? Time.Zero
                    : new Time(command.OriginalManagedAudioMedia.AudioMediaData.PCMFormat.Data.ConvertBytesToTime(command.SelectionData.m_LocalStreamLeftMark));

                Time timeEnd = command.SelectionData.m_LocalStreamRightMark == -1
                    ? command.OriginalManagedAudioMedia.AudioMediaData.AudioDuration
                    : new Time(command.OriginalManagedAudioMedia.AudioMediaData.PCMFormat.Data.ConvertBytesToTime(command.SelectionData.m_LocalStreamRightMark));

                Time diff = timeEnd.GetDifference(timeBegin);

                ManagedAudioMedia manMedia = command.TreeNode.GetManagedAudioMedia();
                if (manMedia == null)
                {
                    DebugFix.Assert(done);

                    DebugFix.Assert(diff.IsEqualTo(command.OriginalManagedAudioMedia.Duration));

                    DebugFix.Assert(
                        //command.OriginalManagedAudioMedia.AudioMediaData.PCMFormat.Data.
                        AudioLibPCMFormat.TimesAreEqualWithMillisecondsTolerance(
                            diff.AsLocalUnits,
                            command.OriginalManagedAudioMedia.Duration.AsLocalUnits));
                }

                long dur = diff.AsLocalUnits;
                if (done)
                {
                    TotalDocumentAudioDurationInLocalUnits -= dur;
                    TotalSessionAudioDurationInLocalUnits  -= dur;
                }
                else
                {
                    TotalDocumentAudioDurationInLocalUnits += dur;
                    TotalSessionAudioDurationInLocalUnits  += dur;
                }
            }
            else if (cmd is TreeNodeRemoveCommand)
            {
                var command = (TreeNodeRemoveCommand)cmd;

                Time duration = command.TreeNode.GetDurationOfManagedAudioMediaFlattened();
                long dur      = duration == null ? 0 : duration.AsLocalUnits;
                if (!done)
                {
                    TotalDocumentAudioDurationInLocalUnits += dur;
                    TotalSessionAudioDurationInLocalUnits  += dur;
                }
                else
                {
                    TotalDocumentAudioDurationInLocalUnits -= dur;
                    TotalSessionAudioDurationInLocalUnits  -= dur;
                }
            }
            else if (cmd is TreeNodeInsertCommand)
            {
                var command = (TreeNodeInsertCommand)cmd;

                Time duration = command.TreeNode.GetDurationOfManagedAudioMediaFlattened();
                long dur      = duration == null ? 0 : duration.AsLocalUnits;
                if (done)
                {
                    TotalDocumentAudioDurationInLocalUnits += dur;
                    TotalSessionAudioDurationInLocalUnits  += dur;
                }
                else
                {
                    TotalDocumentAudioDurationInLocalUnits -= dur;
                    TotalSessionAudioDurationInLocalUnits  -= dur;
                }
            }
        }
        /// <summary>
        /// Splits the audio media data at a given split point in time,
        /// <c>this</c> retaining the audio before the split point,
        /// creating a new <see cref="WavAudioMediaData"/> containing the audio after the split point
        /// </summary>
        /// <param name="splitPoint">The given split point</param>
        /// <returns>A audio media data containing the audio after the split point</returns>
        /// <exception cref="exception.MethodParameterIsNullException">
        /// Thrown when the given split point is <c>null</c>
        /// </exception>
        /// <exception cref="exception.MethodParameterIsOutOfBoundsException">
        /// Thrown when the given split point is negative or is beyond the duration of <c>this</c>
        /// </exception>
        public override AudioMediaData Split(Time splitPoint)
        {
            if (splitPoint == null)
            {
                throw new exception.MethodParameterIsNullException(
                          "The split point can not be null");
            }
            if (splitPoint.IsNegative)
            {
                throw new exception.MethodParameterIsOutOfBoundsException(
                          "The split point can not be negative");
            }
            if (splitPoint.IsGreaterThan(AudioDuration))
            {
                throw new exception.MethodParameterIsOutOfBoundsException(
                          String.Format(
                              "Split point {0} is beyond the WavAudioMediaData end - audio length is {1}",
                              splitPoint, AudioDuration));
            }
            WavAudioMediaData oWAMD =
                Presentation.MediaDataFactory.Create <WavAudioMediaData>();

            if (oWAMD == null)
            {
                throw new exception.FactoryCannotCreateTypeException(String.Format(
                                                                         "Thrown if the MediaDataFactory can not create a WacAudioMediaData matching Xuk QName {1}:{0}",
                                                                         GetXukName(), GetXukNamespace()));
            }
            oWAMD.PCMFormat = PCMFormat.Copy();

            Time originalDuration = AudioDuration;
            Time newClipDuration  = AudioDuration.GetDifference(splitPoint);

            Time           curClip_Begin = Time.Zero;
            List <WavClip> clips         = new List <WavClip>(mWavClips);

            mWavClips.Clear();
            oWAMD.mWavClips.Clear();
            for (int i = 0; i < clips.Count; i++)
            {
                WavClip curClip = clips[i];
                Time    savedCurClipDuration = curClip.Duration;

                Time curClip_End = new Time(curClip_Begin.AsTimeSpanTicks + curClip.Duration.AsTimeSpanTicks, true);
                if (splitPoint.IsLessThanOrEqualTo(curClip_Begin))
                {
                    oWAMD.mWavClips.Add(curClip);
                }
                else if (splitPoint.IsLessThan(curClip_End))
                {
                    WavClip secondPartClip = new WavClip(
                        curClip.DataProvider,
                        curClip.ClipBegin,
                        curClip.IsClipEndTiedToEOM ? null : curClip.ClipEnd);

                    curClip.ClipEnd = new Time(curClip.ClipBegin.AsTimeSpanTicks + (splitPoint.AsTimeSpanTicks - curClip_Begin.AsTimeSpanTicks), true);

                    secondPartClip.ClipBegin = curClip.ClipEnd.Copy();
                    mWavClips.Add(curClip);
                    oWAMD.mWavClips.Add(secondPartClip);
                }
                else
                {
                    mWavClips.Add(curClip);
                }
                curClip_Begin.Add(savedCurClipDuration);
            }
            NotifyAudioDataRemoved(this, splitPoint, newClipDuration);
            oWAMD.NotifyAudioDataInserted(oWAMD, Time.Zero, newClipDuration);

#if DEBUG
            DebugFix.Assert(AudioLibPCMFormat.TimesAreEqualWithMillisecondsTolerance(
                                newClipDuration.AsLocalUnits,
                                oWAMD.AudioDuration.AsLocalUnits));

            DebugFix.Assert(newClipDuration.IsEqualTo(oWAMD.AudioDuration));



            DebugFix.Assert(AudioLibPCMFormat.TimesAreEqualWithMillisecondsTolerance(
                                this.AudioDuration.AsLocalUnits + oWAMD.AudioDuration.AsLocalUnits,
                                originalDuration.AsLocalUnits));

            Time copy = this.AudioDuration.Copy();
            copy.Add(oWAMD.AudioDuration);
            DebugFix.Assert(originalDuration.IsEqualTo(copy));
#endif //DEBUG

            return(oWAMD);
        }