public AudioClipObject LoadSongAudioAsset(string songPath, BeatmapLevelDataObject levelData)
        {
            string audioClipFile = songPath.CombineFwdSlash(levelData.SongFilename);
            //string outputFileName = levelData.LevelID + ".ogg";
            int    channels;
            int    frequency;
            Single length;

            byte[] oggBytes = _config.SongFileProvider.Read(audioClipFile);
            unsafe
            {
                GCHandle pinnedArray = GCHandle.Alloc(oggBytes, GCHandleType.Pinned);
                try
                {
                    IntPtr pointer = pinnedArray.AddrOfPinnedObject();
                    int    error;
                    StbSharp.StbVorbis.stb_vorbis_alloc alloc;
                    StbSharp.StbVorbis.stb_vorbis       v = StbSharp.StbVorbis.stb_vorbis_open_memory((byte *)pointer.ToPointer(), oggBytes.Length, &error, &alloc);
                    channels  = v.channels;
                    frequency = (int)v.sample_rate;
                    length    = StbSharp.StbVorbis.stb_vorbis_stream_length_in_seconds(v);
                    StbSharp.StbVorbis.stb_vorbis_close(v);
                }
                catch (Exception ex)
                {
                    Log.LogErr($"Exception parsing ogg file {audioClipFile}", ex);
                    return(null);
                }
                finally
                {
                    pinnedArray.Free();
                }
            }
            var audioClip = new AudioClipObject(_assetsFile)
            {
                Name              = levelData.LevelID,
                LoadType          = 1,
                IsTrackerFormat   = false,
                Ambisonic         = false,
                SubsoundIndex     = 0,
                PreloadAudioData  = false,
                LoadInBackground  = true,
                Legacy3D          = true,
                CompressionFormat = 1,
                BitsPerSample     = 16,
                Channels          = channels,
                Frequency         = frequency,
                Length            = (Single)length,
                Resource          = new StreamedResource(audioClipFile, 0, Convert.ToUInt64(_config.SongFileProvider.GetFileSize(audioClipFile)))
            };

            return(audioClip);
        }
Beispiel #2
0
        public AudioClipObject LoadAudioClipObject(string songID, string songPath, IFileProvider songFileProvider)
        {
            //string outputFileName = levelData.LevelID + ".ogg";
            int    channels;
            int    frequency;
            Single length;

            byte[] oggBytes = songFileProvider.Read(songPath);
            unsafe
            {
                GCHandle pinnedArray = GCHandle.Alloc(oggBytes, GCHandleType.Pinned);
                try
                {
                    IntPtr pointer = pinnedArray.AddrOfPinnedObject();
                    int    error;
                    StbSharp.StbVorbis.stb_vorbis_alloc alloc;
                    StbSharp.StbVorbis.stb_vorbis       v = StbSharp.StbVorbis.stb_vorbis_open_memory((byte *)pointer.ToPointer(), oggBytes.Length, &error, &alloc);
                    channels  = v.channels;
                    frequency = (int)v.sample_rate;
                    length    = StbSharp.StbVorbis.stb_vorbis_stream_length_in_seconds(v);
                    StbSharp.StbVorbis.stb_vorbis_close(v);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    pinnedArray.Free();
                }
            }
            var audioClip = new AudioClipObject(MainDatabase.ParentFile)
            {
                Name              = songID + "_AudioClip",
                LoadType          = 1,
                IsTrackerFormat   = false,
                Ambisonic         = false,
                SubsoundIndex     = 0,
                PreloadAudioData  = false,
                LoadInBackground  = true,
                Legacy3D          = true,
                CompressionFormat = 1,
                BitsPerSample     = 16,
                Channels          = channels,
                Frequency         = frequency,
                Length            = (Single)length,
                Resource          = new StreamedResource(songID + ".ogg", 0, Convert.ToUInt64(songFileProvider.GetFileSize(songPath)))
            };

            return(audioClip);
        }