Пример #1
0
        internal FileBasedAudioSource(string filePath, bool decodeWhole)
        {
            RwOpsHandle = SDL2.SDL_RWFromFile(filePath, "rb");

            if (RwOpsHandle == IntPtr.Zero)
            {
                _log.Error($"Failed to initialize RWops from file: {SDL2.SDL_GetError()}");
                return;
            }

            FileSourceHandle = SDL2_nmix.NMIX_NewFileSource(
                RwOpsHandle,
                Path.GetExtension(filePath).TrimStart('.'),
                decodeWhole
                );

            if (FileSourceHandle == IntPtr.Zero)
            {
                _log.Error($"Failed to initialize audio source from file: {SDL2.SDL_GetError()}");
                return;
            }

            unsafe
            {
                Handle = FileSource->source;
            }

            HookSourceCallback();
        }
Пример #2
0
        internal FileBasedAudioSource(Stream stream, bool decodeWhole)
        {
            using (var ms = new MemoryStream())
            {
                stream.CopyTo(ms);
                var arr = ms.ToArray();

                unsafe
                {
                    fixed(byte *b = &arr[0])
                    {
                        RwOpsHandle = SDL2.SDL_RWFromConstMem(
                            new IntPtr(b),
                            arr.Length
                            );

                        if (RwOpsHandle == IntPtr.Zero)
                        {
                            _log.Error($"Failed to initialize RWops from stream: {SDL2.SDL_GetError()}");
                            return;
                        }

                        foreach (var decoder in AudioManager.Instance.Decoders)
                        {
                            foreach (var format in decoder.SupportedFormats)
                            {
                                SDL2.SDL_RWseek(RwOpsHandle, 0, SDL2.RW_SEEK_SET);
                                FileSourceHandle = SDL2_nmix.NMIX_NewFileSource(RwOpsHandle, format, decodeWhole);

                                if (FileSourceHandle != IntPtr.Zero)
                                {
                                    break;
                                }
                            }
                        }

                        if (FileSourceHandle == IntPtr.Zero)
                        {
                            _log.Error($"Failed to initialize audio source from stream: {SDL2.SDL_GetError()}");
                            return;
                        }

                        Handle = FileSource->source;
                    }
                }
            }
            HookSourceCallback();
        }