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(); }
public override void Play() { EnsureHandleValid(); if (Status == PlaybackStatus.Playing) { if (SDL2_nmix.NMIX_Pause(Handle) < 0) { _log.Error($"Failed to play the audio source [pause]: {SDL2.SDL_GetError()}"); return; } if (SDL2_nmix.NMIX_Rewind(FileSourceHandle) < 0) { _log.Error($"Failed to play the audio source [rewind]: {SDL2.SDL_GetError()}"); return; } Status = PlaybackStatus.Stopped; } if (SDL2_nmix.NMIX_Play(Handle) < 0) { _log.Error($"Failed to play the audio source [play]: {SDL2.SDL_GetError()}"); return; } Status = PlaybackStatus.Playing; }
public void Seek(int milliseconds) { EnsureFileSourceHandleValid(); if (SDL2_nmix.NMIX_Seek(FileSourceHandle, milliseconds) < 0) { _log.Error($"Failed to seek to {milliseconds}ms: {SDL2.SDL_GetError()}"); } }
public void Rewind() { EnsureFileSourceHandleValid(); if (SDL2_nmix.NMIX_Rewind(FileSourceHandle) < 0) { _log.Error($"Failed to rewind the requested audio source: {SDL2.SDL_GetError()}"); } }
protected override void FreeNativeResources() { if (FileSourceHandle != IntPtr.Zero) { SDL2_nmix.NMIX_FreeFileSource(Handle); FileSourceHandle = IntPtr.Zero; } if (RwOpsHandle != IntPtr.Zero) { SDL2.SDL_FreeRW(RwOpsHandle); RwOpsHandle = IntPtr.Zero; } }
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(); }
public Waveform(AudioFormat format, AudioStreamDelegate sampleGenerator, ChannelMode channelMode = ChannelMode.Stereo, int frequency = 44100) { _internalCallback = AudioCallback; SampleGenerator = sampleGenerator; ChannelMode = channelMode; Frequency = frequency; Handle = SDL2_nmix.NMIX_NewSource( format.SdlFormat, (byte)ChannelMode, Frequency, _internalCallback, IntPtr.Zero ); }