/// <summary>
        /// Fires the <see cref="AudioDataRemoved"/> event
        /// </summary>
        /// <param name="source">The source, that is the <see cref="AudioMediaData"/> from which audio data was removed</param>
        /// <param name="fromPoint">The point at which audio data was removed</param>
        /// <param name="duration">The duration of the removed audio data</param>
        protected void NotifyAudioDataRemoved(AudioMediaData source, Time fromPoint, Time duration)
        {
            EventHandler <events.media.data.audio.AudioDataRemovedEventArgs> d = AudioDataRemoved;

            if (d != null)
            {
                d(this, new urakawa.events.media.data.audio.AudioDataRemovedEventArgs(source, fromPoint, duration));
            }
        }
        /// <summary>
        /// Fires the <see cref="MediaDataChanged"/> event
        /// </summary>
        /// <param name="source">
        /// The source, that is the <see cref="ManagedAudioMedia"/> whoose <see cref="audio.AudioMediaData"/> has changed
        /// </param>
        /// <param name="newData">The new <see cref="audio.AudioMediaData"/></param>
        /// <param name="prevData">The <see cref="audio.AudioMediaData"/> prior to the change</param>
        protected void NotifyMediaDataChanged(ManagedAudioMedia source, AudioMediaData newData, AudioMediaData prevData)
        {
            EventHandler <MediaDataChangedEventArgs> d = MediaDataChanged;

            if (d != null)
            {
                d(this, new MediaDataChangedEventArgs(source, newData, prevData));
            }
        }
        /// <summary>
        /// Fires the <see cref="PCMFormatChanged"/> event
        /// </summary>
        /// <param name="source">The source, that is the <see cref="AudioMediaData"/> whoose <see cref="PCMFormatInfo"/> has changed</param>
        /// <param name="newFormat">The new value</param>
        /// <param name="prevFormat">The value prior to the change</param>
        protected void NotifyPCMFormatChanged(AudioMediaData source, PCMFormatInfo newFormat, PCMFormatInfo prevFormat)
        {
            EventHandler <events.media.data.audio.PCMFormatChangedEventArgs> d = PCMFormatChanged;

            if (d != null)
            {
                d(this, new urakawa.events.media.data.audio.PCMFormatChangedEventArgs(source, newFormat, prevFormat));
            }
        }
        /// <summary>
        /// Gets a copy of the <see cref="Media"/>
        /// </summary>
        /// <returns>The copy</returns>
        protected override Media CopyProtected()
        {
            ManagedAudioMedia cp = (ManagedAudioMedia)base.CopyProtected();

            if (HasActualAudioMediaData)
            {
                cp.AudioMediaData = AudioMediaData.Copy();
            }
            return(cp);
        }
 protected override MediaData ExportProtected(Presentation destPres)
 {
     if (OriginalRelativePath != null && DataProvider != null)
     {
         AudioMediaData expImgData = (AudioMediaData)destPres.MediaDataFactory.Create(GetType());
         expImgData.InitializeAudio(m_DataProvider.Export(destPres), OriginalRelativePath);
         return(expImgData);
     }
     return(null);
 }
        /// <summary>
        /// Exports the external audio media to a destination <see cref="Presentation"/>
        /// </summary>
        /// <param name="destPres">The destination presentation</param>
        /// <returns>The exported external audio media</returns>
        protected override Media ExportProtected(Presentation destPres)
        {
            ManagedAudioMedia exported = (ManagedAudioMedia)base.ExportProtected(destPres);

            if (HasActualAudioMediaData)
            {
                exported.AudioMediaData = AudioMediaData.Export(destPres) as AudioMediaData;
            }

            return(exported);
        }
        /// <summary>
        /// Splits <c>this</c> at a given <see cref="Time"/>
        /// </summary>
        /// <param name="splitPoint">The <see cref="Time"/> at which to split -
        /// must be between clip begin and clip end <see cref="Time"/>s</param>
        /// <returns>
        /// A newly created <see cref="AbstractAudioMedia"/> containing the audio after <paramref localName="splitPoint"/>,
        /// <c>this</c> retains the audio before <paramref localName="splitPoint"/>.
        /// </returns>
        /// <exception cref="exception.MethodParameterIsNullException">
        /// Thrown when <paramref name="splitPoint"/> is <c>null</c>
        /// </exception>
        /// <exception cref="exception.MethodParameterIsOutOfBoundsException">
        /// Thrown when <paramref name="splitPoint"/> is not between clip begin and clip end
        /// </exception>
        protected override AbstractAudioMedia SplitProtected(Time splitPoint)
        {
            ManagedAudioMedia secondPartMAM = Presentation.MediaFactory.Create <ManagedAudioMedia>();

            if (HasActualAudioMediaData)
            {
                AudioMediaData secondPartData = AudioMediaData.Split(splitPoint);
                secondPartMAM.AudioMediaData = secondPartData;
            }

            return(secondPartMAM);
        }
        ///// <summary>
        ///// Part of technical solution to make copy method return correct type.
        ///// In implementing classes this method should return a copy of the class instances
        ///// </summary>
        ///// <returns>The copy</returns>
        //protected abstract AudioMediaData AudioMediaDataCopy();


        protected override MediaData CopyProtected()
        {
            if (OriginalRelativePath != null && DataProvider != null)
            {
                AudioMediaData copyVideoMediaData = (AudioMediaData)Presentation.MediaDataFactory.Create(GetType());
                // We do not Copy the FileDataProvider,
                // it is shared amongst AudioMediaData instances without concurrent access problems
                // because the file stream is read-only by design.
                copyVideoMediaData.InitializeAudio(m_DataProvider, OriginalRelativePath);
                return(copyVideoMediaData);
            }
            return(null);
        }
        /// <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="AudioMediaData"/> 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 virtual AudioMediaData Split(Time splitPoint)
        {
            if (OriginalRelativePath != null && DataProvider != null)
            {
                throw new NotImplementedException();
            }

            //throw new NotImplementedException("AudioMediaData.Split() should never be called ! (WavAudioMediaData instead)");

            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(
                          "The split point can not be beyond the end of the AudioMediaData");
            }
            MediaData md = Presentation.MediaDataFactory.Create(GetType());

            if (!(md is AudioMediaData))
            {
                throw new exception.FactoryCannotCreateTypeException(String.Format(
                                                                         "The MediaDataFactory can not create a AudioMediaData matching Xuk QName {1}:{0}",
                                                                         GetXukName(), GetXukNamespace()));
            }
            AudioMediaData secondPartAMD         = (AudioMediaData)md;
            Time           spDur                 = AudioDuration.GetDifference(splitPoint);
            Stream         secondPartAudioStream = OpenPcmInputStream(splitPoint);

            try
            {
                secondPartAMD.AppendPcmData(secondPartAudioStream, spDur);
            }
            finally
            {
                secondPartAudioStream.Close();
            }
            RemovePcmData(splitPoint);
            return(secondPartAMD);
        }
        public void ValueEquals_MediaData()
        {
            AudioMediaData data1 = mPresentation.MediaDataFactory.Create <codec.WavAudioMediaData>();
            AudioMediaData data2 = mPresentation.MediaDataFactory.Create <codec.WavAudioMediaData>();

            mManagedAudioMedia1.AudioMediaData = data1;
            mManagedAudioMedia2.AudioMediaData = data1;
            Assert.IsTrue(mManagedAudioMedia1.ValueEquals(mManagedAudioMedia2),
                          "two medias with the same data should be equal");
            mManagedAudioMedia2.AudioMediaData = data2;
            Assert.IsTrue(data1.ValueEquals(data2), "[Pre-Condition] media datas should be equal");
            Assert.IsTrue(mManagedAudioMedia1.ValueEquals(mManagedAudioMedia2),
                          "two medias with equal data should be equal");
            data2.Name = "foo";
            Assert.IsFalse(data1.ValueEquals(data2), "[Pre-Condition] media datas shouldn't be equal");
            Assert.IsFalse(data1.ValueEquals(data2) && !mManagedAudioMedia1.ValueEquals(mManagedAudioMedia2),
                           "two medias with different data shouldn't be equal");
        }
        /// <summary>
        /// Merges <c>this</c> with a given other <see cref="AudioMediaData"/>,
        /// appending the audio data of the other <see cref="AudioMediaData"/> to <c>this</c>,
        /// leaving the other <see cref="AudioMediaData"/> without audio data
        /// </summary>
        /// <param name="other">The given other AudioMediaData</param>
        /// <exception cref="exception.MethodParameterIsNullException">
        /// Thrown when <paramref name="other"/> is <c>null</c>
        /// </exception>
        /// <exception cref="exception.InvalidDataFormatException">
        /// Thrown when the PCM format of <c>this</c> is not compatible with that of <paramref name="other"/>
        /// </exception>
        public virtual void MergeWith(AudioMediaData other)
        {
            if (OriginalRelativePath != null && DataProvider != null)
            {
                throw new NotImplementedException();
            }

            //throw new NotImplementedException("AudioMediaData.MergeWith() should never be called ! (WavAudioMediaData instead)");

            if (other == null)
            {
                throw new exception.MethodParameterIsNullException("Can not merge with a null AudioMediaData");
            }
            if (other == this)
            {
                throw new exception.OperationNotValidException("Can not merge a AudioMediaData with itself");
            }
            if (!PCMFormat.Data.IsCompatibleWith(other.PCMFormat.Data))
            {
                throw new exception.InvalidDataFormatException(
                          "Can not merge this with a AudioMediaData with incompatible audio data");
            }

#if DEBUG
            Debugger.Break();
#endif // DEBUG

            Stream otherData = other.OpenPcmInputStream();
            try
            {
                AppendPcmData(otherData, other.AudioDuration);
            }
            finally
            {
                otherData.Close();
            }
            other.RemovePcmData(Time.Zero);
        }
 private void Reset()
 {
     mAudioMediaData = null;
 }
        public override bool ValueEquals(WithPresentation other)
        {
            if (!base.ValueEquals(other))
            {
                return(false);
            }

            AudioMediaData otherz = other as AudioMediaData;

            if (otherz == null)
            {
                return(false);
            }

            if (OriginalRelativePath != null && DataProvider != null)
            {
                if (OriginalRelativePath != otherz.OriginalRelativePath)
                {
                    //System.Diagnostics.Debug.Fail("! ValueEquals !");
                    return(false);
                }

                if (!DataProvider.ValueEquals(otherz.DataProvider))
                {
                    //System.Diagnostics.Debug.Fail("! ValueEquals !");
                    return(false);
                }

                return(true);
            }

            if (!PCMFormat.ValueEquals(otherz.PCMFormat))
            {
                //System.Diagnostics.Debug.Fail("! ValueEquals !");
                return(false);
            }

            if (PCMFormat.Data.ConvertTimeToBytes(AudioDuration.AsLocalUnits)
                != otherz.PCMFormat.Data.ConvertTimeToBytes(otherz.AudioDuration.AsLocalUnits))
            {
                //System.Diagnostics.Debug.Fail("! ValueEquals !");
                return(false);
            }

            if (HasActualPcmData != otherz.HasActualPcmData)
            {
                return(false);
            }

            if (HasActualPcmData)
            {
                Stream thisData = OpenPcmInputStream();
                try
                {
                    Stream otherdata = otherz.OpenPcmInputStream();
                    try
                    {
                        if (!AudioLibPCMFormat.CompareStreamData(thisData, otherdata, (int)thisData.Length))
                        {
                            //System.Diagnostics.Debug.Fail("! ValueEquals !");
                            return(false);
                        }
                    }
                    finally
                    {
                        otherdata.Close();
                    }
                }
                finally
                {
                    thisData.Close();
                }
            }

            return(true);
        }