Beispiel #1
0
        public static AudioClip ToAudioClip(string name)
        {
            var vorbis = new NVorbis.VorbisReader(GameAPI.instance.fileSystem.GetStreamFromPath(Path.Combine("sounds", name + ".ogg")), true);

            var channels     = vorbis.Channels;
            var sampleRate   = vorbis.SampleRate;
            var totalSamples = vorbis.TotalSamples;

            AudioClip clip = AudioClip.Create(name, (int)totalSamples, channels, sampleRate, false);

            var readBuffer  = new float[channels * sampleRate / 5];
            var totalBuffer = new float[channels * totalSamples];

            int rd   = 0;
            int prev = 0;

            while ((rd = vorbis.ReadSamples(readBuffer, 0, readBuffer.Length)) > 0)
            {
                Buffer.BlockCopy(readBuffer, 0, totalBuffer, prev * sizeof(float), rd * sizeof(float));

                prev += rd;
            }

            clip.SetData(totalBuffer, 0);

            vorbis.Dispose();

            return(clip);
        }
Beispiel #2
0
        protected override void Dispose(bool disposing)
        {
            if (disposing && _reader != null)
            {
                _reader.Dispose();
                _reader = null;
            }

            base.Dispose(disposing);
        }
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="VorbisReader"/> and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        /// <inheritdoc/>
        protected override void Dispose(bool disposing)
        {
            if (!IsDisposed)
            {
                if (disposing)
                {
                    reader?.Dispose();
                }

                IsDisposed = true;
            }
        }
Beispiel #4
0
        private static SoundEffectInstance get_music(string cue_name)
        {
            SoundEffect         song = null;
            SoundEffectInstance music = null;
            int intro_start = 0, loop_start = -1, loop_length = -1;

            NVorbis.VorbisReader vorbis = null;
            try
            {
                try
                {
                    Stream cue_stream = TitleContainer.OpenStream(@"Content\Audio\BGM\" + cue_name + ".ogg");


#if __ANDROID__
                    MemoryStream stream = new MemoryStream();
                    cue_stream.CopyTo(stream);
                    vorbis = new NVorbis.VorbisReader(stream, cue_name, true);
#else
                    vorbis = new NVorbis.VorbisReader(cue_stream, cue_name, true);
#endif
                    get_loop_data(vorbis, out intro_start, out loop_start, out loop_length);


                    // If the loop points are set past the end of the song, don't play
                    if (vorbis.TotalSamples < loop_start || vorbis.TotalSamples < loop_start + loop_length)
                    {
#if DEBUG
                        throw new IndexOutOfRangeException("Loop points are set past the end of the song");
#endif
#if __ANDROID__
                        cue_stream.Dispose();
                        vorbis.Dispose();
#else
                        vorbis.Dispose();
#endif
                        throw new FileNotFoundException();
                    }
#if __ANDROID__
                    cue_stream.Dispose();
#endif
                }
                catch (FileNotFoundException ex)
                {
                    throw;
                }
#if __ANDROID__
                catch (Java.IO.FileNotFoundException e)
                {
                    throw;
                }
#endif
            }
            // If loaded as an ogg failed, try loading as a SoundEffect
            catch (FileNotFoundException ex)
            {
                intro_start = 0;
                loop_start  = -1;
                loop_length = -1;
                song        = Global.Content.Load <SoundEffect>(@"Audio/" + cue_name);
            }

            // If the file is an ogg file and was found and initialized successfully
            if (vorbis != null)
            {
                music = get_vorbis_music(vorbis, cue_name,
                                         intro_start, loop_start, loop_length);
            }
            else
            {
                music = get_effect_music(song, cue_name, intro_start, loop_start, loop_length);
            }

            if (music != null)
            {
                music.IsLooped = true;
            }
#if !__ANDROID__
            if (song != null)
            {
                song.Dispose();
            }
#endif
            return(music);
        }