public void SetPosition03()
        {
            var            audioProperties = new AudioProperties(2, 44100, 32, AudioFormat.IEEE);
            TimeWarpStream s = new TimeWarpStream(
                new NullStream(audioProperties, TimeUtil.TimeSpanToBytes(new TimeSpan(0, 1, 0), audioProperties)));
            TimeSpan length = TimeUtil.BytesToTimeSpan(s.Length, s.Properties);

            s.Mappings.Add(new TimeWarp {
                From = new TimeSpan(length.Ticks / 2),
                To   = new TimeSpan(length.Ticks / 4)
            });
            s.Mappings.Add(new TimeWarp {
                From = length,
                To   = new TimeSpan(length.Ticks / 4 * 2)
            });

            byte[] buffer = new byte[5000];

            Assert.AreEqual(0, s.Position);
            s.Read(buffer, 0, buffer.Length);
            s.Position = 44440;
            while (s.Read(buffer, 0, buffer.Length) > 0)
            {
            }
            Assert.AreEqual(s.Length, s.Position);
        }
        public void SetPosition02()
        {
            var            audioProperties = new AudioProperties(2, 44100, 32, AudioFormat.IEEE);
            TimeWarpStream s = new TimeWarpStream(
                new NullStream(audioProperties, TimeUtil.TimeSpanToBytes(new TimeSpan(0, 1, 0), audioProperties)));
            TimeSpan length = TimeUtil.BytesToTimeSpan(s.Length, s.Properties);

            //s.Mappings.Add(new TimeWarp {
            //    From = StreamUtil.AlignToBlockSize(length / 2, s.SampleBlockSize),
            //    To = StreamUtil.AlignToBlockSize(length / 4, s.SampleBlockSize)
            //});
            s.Mappings.Add(new TimeWarp {
                From = length,
                To   = new TimeSpan(length.Ticks / 4 * 2)
            });

            byte[] buffer = new byte[5000];
            int    bytesRead;
            long   totalBytesRead;

            Assert.AreEqual(0, s.Position);
            totalBytesRead = 0;
            s.Position     = 11104;
            Assert.AreEqual(11104, s.Position);
            //Assert.AreEqual(0, s.BufferedBytes);
            while ((bytesRead = s.Read(buffer, 0, buffer.Length)) > 0)
            {
                totalBytesRead += bytesRead;
            }
            Assert.AreEqual(totalBytesRead, s.Position - 11104);
            Assert.AreEqual(s.Length, s.Position);
        }
        public void SetPosition04()
        {
            var            audioProperties = new AudioProperties(2, 44100, 32, AudioFormat.IEEE);
            TimeWarpStream s = new TimeWarpStream(
                new NullStream(audioProperties, TimeUtil.TimeSpanToBytes(new TimeSpan(0, 1, 0), audioProperties)));
            TimeSpan length = TimeUtil.BytesToTimeSpan(s.Length, s.Properties);

            s.Mappings.Add(new TimeWarp {
                From = new TimeSpan(length.Ticks / 2),
                To   = new TimeSpan(length.Ticks / 4)
            });
            s.Mappings.Add(new TimeWarp {
                From = length,
                To   = new TimeSpan(length.Ticks / 4 * 2)
            });

            byte[] buffer = new byte[5000];

            Assert.AreEqual(0, s.Position);
            bool positionSet = false;
            int  count       = 0;

            while (s.Read(buffer, 0, buffer.Length) > 0)
            {
                if (++count == 5)
                {
                    positionSet = true;
                    long posBefore = s.Position;
                    //long sourcePosBefore = s.SourceStream.Position - s.BufferedBytes;
                    s.Position = posBefore;
                    Assert.AreEqual(posBefore, s.Position);
                    //Assert.AreEqual(sourcePosBefore, s.SourceStream.Position);
                }
            }
            Assert.IsTrue(positionSet); // if the position hasn't been set, the whole test case is pointless
            Assert.AreEqual(s.Length, s.Position);
        }
Example #4
0
        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            if (wavePlayer != null)
            {
                wavePlayer.Dispose();
            }
            debugStreamController = new DebugStreamController();

            MixerStream mixer = new MixerStream(2, 44100);

            foreach (AudioTrack audioTrack in trackListBox.Items)
            {
                WaveFileReader reader  = new WaveFileReader(audioTrack.FileInfo.FullName);
                IeeeStream     channel = new IeeeStream(new DebugStream(new NAudioSourceStream(reader), debugStreamController));
                //ResamplingStream res = new ResamplingStream(new DebugStream(channel, debugStreamController), ResamplingQuality.SincBest, 22050);

                TimeWarpStream warp = new TimeWarpStream(new DebugStream(channel, debugStreamController));
                //warp.Mappings.Add(new TimeWarp { From = new TimeSpan(audioTrack.Length.Ticks / 10 * 4), To = new TimeSpan(audioTrack.Length.Ticks / 9) });
                //warp.Mappings.Add(new TimeWarp { From = new TimeSpan(audioTrack.Length.Ticks / 10 * 5), To = new TimeSpan(audioTrack.Length.Ticks / 9 * 2) });
                //warp.Mappings.Add(new TimeWarp { From = new TimeSpan(audioTrack.Length.Ticks / 10 * 10), To = new TimeSpan(audioTrack.Length.Ticks / 9 * 3) });

                // necessary to control each track individually
                VolumeControlStream volumeControl = new VolumeControlStream(new DebugStream(warp, debugStreamController))
                {
                    Mute   = audioTrack.Mute,
                    Volume = audioTrack.Volume
                };

                // when the AudioTrack.Mute property changes, just set it accordingly on the audio stream
                audioTrack.MuteChanged += new EventHandler <ValueEventArgs <bool> >(
                    delegate(object vsender, ValueEventArgs <bool> ve) {
                    volumeControl.Mute = ve.Value;
                });

                // when the AudioTrack.Solo property changes, we have to react in different ways:
                audioTrack.SoloChanged += new EventHandler <ValueEventArgs <bool> >(
                    delegate(object vsender, ValueEventArgs <bool> ve) {
                    AudioTrack senderTrack  = (AudioTrack)vsender;
                    bool isOtherTrackSoloed = false;

                    foreach (AudioTrack vaudioTrack in trackListBox.Items)
                    {
                        if (vaudioTrack != senderTrack && vaudioTrack.Solo)
                        {
                            isOtherTrackSoloed = true;
                            break;
                        }
                    }

                    /* if there's at least one other track that is soloed, we set the mute property of
                     * the current track to the opposite of the solo property:
                     * - if the track is soloed, we unmute it
                     * - if the track is unsoloed, we mute it
                     */
                    if (isOtherTrackSoloed)
                    {
                        senderTrack.Mute = !ve.Value;
                    }

                    /* if this is the only soloed track, we mute all other tracks
                     * if this track just got unsoloed, we unmute all other tracks
                     */
                    else
                    {
                        foreach (AudioTrack vaudioTrack in trackListBox.Items)
                        {
                            if (vaudioTrack != senderTrack && !vaudioTrack.Solo)
                            {
                                vaudioTrack.Mute = ve.Value;
                            }
                        }
                    }
                });

                // when the AudioTrack.Volume property changes, just set it accordingly on the audio stream
                audioTrack.VolumeChanged += new EventHandler <ValueEventArgs <float> >(
                    delegate(object vsender, ValueEventArgs <float> ve) {
                    volumeControl.Volume = ve.Value;
                });

                mixer.Add(new DebugStream(volumeControl));
            }

            VolumeControlStream volumeControlStream = new VolumeControlStream(new DebugStream(mixer, debugStreamController))
            {
                Volume = (float)volumeSlider.Value
            };
            VolumeMeteringStream volumeMeteringStream = new VolumeMeteringStream(new DebugStream(volumeControlStream, debugStreamController), 5000);

            volumeMeteringStream.StreamVolume += new EventHandler <StreamVolumeEventArgs>(meteringStream_StreamVolume);
            VolumeClipStream volumeClipStream = new VolumeClipStream(new DebugStream(volumeMeteringStream, debugStreamController));

            playbackStream = volumeClipStream;

            wavePlayer = new WaveOut();
            wavePlayer.DesiredLatency = 250;
            wavePlayer.Init(new NAudioSinkStream(new DebugStream(playbackStream, debugStreamController)));

            // master volume setting
            volumeSlider.ValueChanged += new RoutedPropertyChangedEventHandler <double>(
                delegate(object vsender, RoutedPropertyChangedEventArgs <double> ve) {
                volumeControlStream.Volume = (float)ve.NewValue;
            });

            lblTotalPlaybackTime.Content = TimeUtil.BytesToTimeSpan(playbackStream.Length, playbackStream.Properties);
            playbackSeeker.Maximum       = TimeUtil.BytesToTimeSpan(playbackStream.Length, playbackStream.Properties).TotalSeconds;

            wavePlayer.Play();
        }