public unsafe static int ov_openFile(VFile f, OggVorbis_File vf) { vf.memset(); ov_callbacks callbacks; callbacks.read_func = Marshal.GetFunctionPointerForDelegate <ov_callbacks.ReadFuncDelegate>(FS_ReadOGG); callbacks.seek_func = Marshal.GetFunctionPointerForDelegate <ov_callbacks.SeekFuncDelegate>(FS_SeekOGG); callbacks.close_func = Marshal.GetFunctionPointerForDelegate <ov_callbacks.CloseFuncDelegate>(FS_CloseOGG); callbacks.tell_func = Marshal.GetFunctionPointerForDelegate <ov_callbacks.TellFuncDelegate>(FS_TellOGG); return(ov_open_callbacks(f, vf, null, -1, callbacks)); }
unsafe int OpenOGG(string strFileName, out WaveformatEx pwfx) { pwfx = default; mhmmio = fileSystem.OpenFileRead(strFileName); if (mhmmio == null) { return(-1); } ISystem.EnterCriticalSection(CRITICAL_SECTION.SECTION_ONE); var ov = new OggVorbis_File(); if (OggVorbis.ov_openFile(mhmmio, ov) < 0) { ISystem.LeaveCriticalSection(CRITICAL_SECTION.SECTION_ONE); fileSystem.CloseFile(mhmmio); mhmmio = null; return(-1); } mfileTime = mhmmio.Timestamp; var vi = ov_info(ov, -1); mpwfx.Format.nSamplesPerSec = (int)vi->rate; mpwfx.Format.nChannels = (short)vi->channels; mpwfx.Format.wBitsPerSample = sizeof(short) * 8; mdwSize = (int)(ov_pcm_total(ov, -1) * vi->channels); // pcm samples * num channels mbIsReadingFromMemory = false; if (SoundSystemLocal.s_realTimeDecoding.Bool) { ov_clear(ov); fileSystem.CloseFile(mhmmio); mhmmio = null; mpwfx.Format.wFormatTag = WAVE_FORMAT_TAG.OGG; mhmmio = fileSystem.OpenFileRead(strFileName); mMemSize = mhmmio.Length; } else { ogg = ov; mpwfx.Format.wFormatTag = WAVE_FORMAT_TAG.PCM; mMemSize = mdwSize * sizeof(short); } pwfx = mpwfx.Format; ISystem.LeaveCriticalSection(CRITICAL_SECTION.SECTION_ONE); isOgg = true; return(0); }
/// <summary> /// Reads the audio data in the oggvorbis stream. /// TODO: Read from stream instead of temp file. /// </summary> /// <returns>Raw Audio Buffer</returns> public override AudioData ReadAudioData() { ALFormat format = 0; int frequency = 0; AudioData data; using (MemoryStream mStream = new MemoryStream()) { vorbis_info info; OggVorbis_File file = new OggVorbis_File(); int bitstream = 0; long bytes; byte[] array = new byte[32768]; string filePath = CreateTempSoundFile(); NativeMethods.ov_fopen(filePath, ref file); info = (vorbis_info)Marshal.PtrToStructure(NativeMethods.ov_info(ref file, -1), typeof(vorbis_info)); if (info.channels == 1) { format = ALFormat.Mono16; } else { format = ALFormat.Stereo16; } frequency = info.rate; do { bytes = NativeMethods.ov_read(ref file, array, array.Length, 0, 2, 1, ref bitstream); mStream.Write(array, 0, (int)bytes); } while (bytes > 0); NativeMethods.ov_clear(ref file); File.Delete(filePath); data = new AudioData(mStream.ToArray(), format, frequency); } return(data); }