public static void LoadAudio(AudioStream audioStream)
        {
            // abort if audio data already loaded
            if (audioStream.IsAudioLoaded())
            {
                return;
            }

            AudioDataHelper.LoadAudioData(audioStream);

            var channel = Bass.BASS_StreamCreateFile(audioStream.AudioData.DataPointer,
                                                     0,
                                                     audioStream.AudioData.Data.Length,
                                                     BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_STREAM_PRESCAN);

            audioStream.AddChannel(channel);

            if (audioStream.ChannelId == 0)
            {
                throw new Exception("Cannot load " + audioStream.Filename + ". Error code: " + Bass.BASS_ErrorGetCode());
            }

            // DebugHelper.WriteLine("Creating reverse FX stream " + audioStream.Description + "...");
            audioStream.AddChannel(BassFx.BASS_FX_ReverseCreate(audioStream.ChannelId, 1, BASSFlag.BASS_STREAM_DECODE));

            if (audioStream.ChannelId == 0)
            {
                throw new Exception("Cannot load " + audioStream.Filename + ". Error code: " + Bass.BASS_ErrorGetCode());
            }

            Bass.BASS_ChannelSetAttribute(audioStream.ChannelId, BASSAttribute.BASS_ATTRIB_REVERSE_DIR,
                                          (float)BASSFXReverse.BASS_FX_RVS_FORWARD);


            // DebugHelper.WriteLine("Creating tempo FX stream " + audioStream.Description + "...");

            audioStream.AddChannel(BassFx.BASS_FX_TempoCreate(audioStream.ChannelId,
                                                              BASSFlag.BASS_FX_FREESOURCE | BASSFlag.BASS_STREAM_DECODE));

            if (audioStream.ChannelId == 0)
            {
                throw new Exception("Cannot load " + audioStream.Filename + ". Error code: " + Bass.BASS_ErrorGetCode());
            }

            // DebugHelper.WriteLine("Calculating track length " + audioStream.Description + "...");

            audioStream.Length            = Bass.BASS_ChannelGetLength(audioStream.ChannelId);
            audioStream.DefaultSampleRate = GetSampleRate(audioStream.ChannelId);

            SetReplayGain(audioStream);
            SetPosition(audioStream, 0);
        }
Ejemplo n.º 2
0
        internal AudioTrackBass(Stream data, bool quick = false, bool loop = false)
        {
            procs = new BASS_FILEPROCS(ac_Close, ac_Length, ac_Read, ac_Seek);

            Preview = quick;
            Looping = loop;

            BASSFlag flags = Preview ? 0 : (BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_STREAM_PRESCAN);

            if (data == null)
            {
                throw new AudioNotLoadedException();
            }
            else
            {
                //encapsulate incoming stream with async buffer if it isn't already.
                DataStream = data as AsyncBufferStream;
                if (DataStream == null)
                {
                    DataStream = new AsyncBufferStream(data, quick ? 8 : -1);
                }

                audioStreamPrefilter = Bass.BASS_StreamCreateFileUser(BASSStreamSystem.STREAMFILE_NOBUFFER, flags, procs, IntPtr.Zero);
            }

            if (Preview)
            {
                audioStream = audioStreamForwards = audioStreamPrefilter;
            }
            else
            {
                audioStream          = audioStreamForwards = BassFx.BASS_FX_TempoCreate(audioStreamPrefilter, loop ? BASSFlag.BASS_MUSIC_LOOP : BASSFlag.BASS_DEFAULT);
                audioStreamBackwards = BassFx.BASS_FX_ReverseCreate(audioStreamPrefilter, 5f, BASSFlag.BASS_DEFAULT);

                Bass.BASS_ChannelSetAttribute(audioStream, BASSAttribute.BASS_ATTRIB_TEMPO_OPTION_USE_QUICKALGO, Bass.TRUE);
                Bass.BASS_ChannelSetAttribute(audioStream, BASSAttribute.BASS_ATTRIB_TEMPO_OPTION_OVERLAP_MS, 4);
                Bass.BASS_ChannelSetAttribute(audioStream, BASSAttribute.BASS_ATTRIB_TEMPO_OPTION_SEQUENCE_MS, 30);
            }

            Length = (Bass.BASS_ChannelBytes2Seconds(audioStream, Bass.BASS_ChannelGetLength(audioStream)) * 1000);
            Bass.BASS_ChannelGetAttribute(audioStream, BASSAttribute.BASS_ATTRIB_FREQ, ref initialAudioFrequency);
            currentAudioFrequency = initialAudioFrequency;

            AudioEngine.RegisterTrack(this);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Loads the sample audio data.
        /// </summary>
        /// <param name="sample">The sample to load.</param>
        /// <returns>The loaded sample</returns>
        private Sample LoadSampleAudioData(Sample sample)
        {
            // abort if audio data already loaded
            if (sample.Channel != int.MinValue)
            {
                return(sample);
            }

            Debug.Print("Loading sample Audio Data " + sample.Description);

            sample.AudioData       = File.ReadAllBytes(sample.Filename);
            sample.AudioDataHandle = GCHandle.Alloc(sample.AudioData, GCHandleType.Pinned);
            sample.Channel         = Bass.BASS_StreamCreateFile(sample.AudioDataPointer, 0, sample.AudioData.Length, BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_STREAM_PRESCAN);

            if (sample.Channel == 0)
            {
                var errorCode = Bass.BASS_ErrorGetCode();
                throw new Exception("Cannot load sample " + sample.Filename + ". Error code: " + errorCode.ToString());
            }

            sample.Channel = BassFx.BASS_FX_ReverseCreate(sample.Channel, 1, BASSFlag.BASS_STREAM_DECODE);
            if (sample.Channel == 0)
            {
                throw new Exception("Cannot load sample " + sample.Filename);
            }
            Bass.BASS_ChannelSetAttribute(sample.Channel, BASSAttribute.BASS_ATTRIB_REVERSE_DIR, (float)BASSFXReverse.BASS_FX_RVS_FORWARD);

            sample.Channel = BassFx.BASS_FX_TempoCreate(sample.Channel, BASSFlag.BASS_FX_FREESOURCE | BASSFlag.BASS_STREAM_DECODE);
            if (sample.Channel == 0)
            {
                throw new Exception("Cannot load sample " + sample.Filename);
            }

            sample.Length = Bass.BASS_ChannelGetLength(sample.Channel);

            sample.DefaultSampleRate = BassHelper.GetSampleRate(sample.Channel);

            return(sample);
        }