예제 #1
0
        /// <summary>
        /// Saves the media to the given <see cref="Stream"/>.
        /// </summary>
        /// <param name="TargetStream">The <see cref="Stream"/> where the media is written to.</param>
        /// <param param name="Progress">An <see cref="IProgress{T}"/> instance that will be used to report the save progress. Can be null.</param>
        /// <returns>
        /// A <see cref="Task"/> that represents the process of the saving procedure. When the saving
        /// is finished, a bool value will represent whether the saving was successful or not.
        /// </returns>
        public virtual async Task <bool> SaveToAsync(Stream TargetStream, IProgress <LongOperationProgress> Progress)
        {
            #region Error checking
            if (TargetStream == null)
            {
                return(false);
            }

            if (!TargetStream.CanWrite)
            {
                return(false);
            }
            #endregion

            LongOperationProgress Status = new LongOperationProgress {
                IsIndetermine = true,
                Maximum       = 1,
                Position      = 0
            };

            Progress?.Report(Status);

            try {
                await this.AllocatedMemory.CopyToAsync(TargetStream, Progress);
            }
            catch (Exception) {
                return(false);
            }

            return(true);
        }
예제 #2
0
        /// <summary>
        /// Stops the playback and copies the audio stream from CD to a <see cref="Stream"/> in MP3
        /// format asynchronously. If the song was playing before copying, playback will be restored.
        /// </summary>
        /// <param name="TargetStream">The <see cref="Stream"/> where the media is written to.</param>
        /// <param param name="Progress">An <see cref="IProgress{T}"/> instance that will be used to report the save progress. Can be null.</param>
        /// <returns>A <see cref="Task"/> that represents the asynchronous process.</returns>
        public async Task <bool> SaveToAsync(Stream TargetStream, IProgress <LongOperationProgress> Progress)
        {
            bool   RequiresRestore = this.IsPlaying;
            double Position        = this.Position;

            if (this.IsPlaying)
            {
                this.Stop();
            }

            ENCODEPROC proc = new ENCODEPROC(
                (handle, channel, buffer, length, user) => {
                byte[] ManagedBuffer = new byte[length];
                Marshal.Copy(buffer, ManagedBuffer, 0, length);

                TargetStream.Write(ManagedBuffer, 0, length);
            }
                );

            int  DecodingChannel       = Bass.BASS_StreamCreateFile(this.Filename, 0, 0, BASSFlag.BASS_STREAM_DECODE);
            long DecodingChannelLength = Bass.BASS_ChannelGetLength(DecodingChannel);
            int  Handle = BassEnc.BASS_Encode_Start(DecodingChannel, (new Lame()).GetCommandLine(), BASSEncode.BASS_ENCODE_AUTOFREE, proc, IntPtr.Zero);

            LongOperationProgress Status = new LongOperationProgress {
                IsIndetermine = false,
                Maximum       = DecodingChannelLength,
                Position      = 0
            };

            Progress?.Report(Status);

            await Task.Run(() => {
                int BufferLength = 10240;
                byte[] Buffer    = new byte[BufferLength];

                while (Bass.BASS_ChannelGetPosition(DecodingChannel) < DecodingChannelLength)
                {
                    Status.Position += Bass.BASS_ChannelGetData(DecodingChannel, Buffer, BufferLength);
                    Progress?.Report(Status);
                }

                BassEnc.BASS_Encode_Stop(Handle);
            });

            this.ChannelID = Bass.BASS_StreamCreateFile(this.Filename, 0, 0, BASSFlag.BASS_DEFAULT);
            if (RequiresRestore)
            {
                this.Position = Position;
                this.Play();
            }

            return(true);
        }
        private static void OpenFileProgressChanged(object sender, LongOperationProgress e)
        {
            #region Error checking
            if (e == null)
            {
                return;
            }
            #endregion

            string StatusText = String.IsNullOrEmpty(e.StatusText) ? $"Zene megnyitása: {e.Percent}%" : e.StatusText;

            MediaOpenProgressChanged?.Invoke(e.Percent, StatusText);
        }
 private void Progress_ProgressChanged(object sender, LongOperationProgress e)
 {
     UI_ProgressBar.IsIndeterminate = e.IsIndetermine;
     UI_ProgressBar.Maximum         = e.Maximum;
     UI_ProgressBar.Value           = e.Position;
 }