private PortAudio.PaStreamCallbackResult _PaStreamCallback( IntPtr input, IntPtr output, uint frameCount, ref PortAudio.PaStreamCallbackTimeInfo timeInfo, PortAudio.PaStreamCallbackFlags statusFlags, IntPtr userData) { byte[] buf = new byte[frameCount * _ByteCount]; if (_Paused) { try { Marshal.Copy(buf, 0, output, (int)frameCount * _ByteCount); } catch (Exception e) { Console.WriteLine(e.ToString()); } return(PortAudio.PaStreamCallbackResult.paContinue); } lock (MutexData) { if (_NoMoreData || _data.BytesNotRead >= buf.Length) { _data.Read(ref buf); byte[] b = new byte[2]; for (int i = 0; i < buf.Length; i += _ByteCount) { b[0] = buf[i]; b[1] = buf[i + 1]; b = BitConverter.GetBytes((Int16)(BitConverter.ToInt16(b, 0) * _Volume)); buf[i] = b[0]; buf[i + 1] = b[1]; if (_ByteCount == 4) { b[0] = buf[i + 2]; b[1] = buf[i + 3]; b = BitConverter.GetBytes((Int16)(BitConverter.ToInt16(b, 0) * _Volume)); buf[i + 2] = b[0]; buf[i + 3] = b[1]; } } } if (_data.BytesNotRead < BUFSIZE - 10000L) { EventDecode.Set(); _waiting = false; } else { _waiting = true; } float latency = buf.Length / _BytesPerSecond + CConfig.AudioLatency / 1000f; float time = _TimeCode - _data.BytesNotRead / _BytesPerSecond - latency; _CurrentTime = _SyncTimer.Update(time); } try { Marshal.Copy(buf, 0, output, (int)frameCount * _ByteCount); } catch (Exception e) { CLog.LogError("Error PortAudio.StreamCallback: " + e.Message); } return(PortAudio.PaStreamCallbackResult.paContinue); }
public void UploadData() { Update(); if (_Paused) { return; } int queued_count = 0; bool doit = true; AL.GetSource(_source, ALGetSourcei.BuffersQueued, out queued_count); int processed_count = buffer_count; if (queued_count != 0) { AL.GetSource(_source, ALGetSourcei.BuffersProcessed, out processed_count); doit = false; Console.WriteLine("Buffers Processed on Stream " + _source.ToString() + " = " + processed_count.ToString()); if (processed_count < 1) { return; } } byte[] buf = new byte[buffer_size]; lock (MutexData) { while (processed_count > 0) { if (_data.BytesNotRead >= buf.Length) { _data.Read(ref buf); byte[] b = new byte[2]; for (int i = 0; i < buf.Length; i += _ByteCount) { b[0] = buf[i]; b[1] = buf[i + 1]; b = BitConverter.GetBytes((Int16)(BitConverter.ToInt16(b, 0) * _Volume * _VolumeMax)); buf[i] = b[0]; buf[i + 1] = b[1]; if (_ByteCount == 4) { b[0] = buf[i + 2]; b[1] = buf[i + 3]; b = BitConverter.GetBytes((Int16)(BitConverter.ToInt16(b, 0) * _Volume * _VolumeMax)); buf[i + 2] = b[0]; buf[i + 3] = b[1]; } } int buffer = 0; if (!doit) { buffer = AL.SourceUnqueueBuffer(_source); } else { buffer = _buffers[queued_count]; queued_count++; } if (buffer != 0) { if (_format.ChannelCount == 2) { AL.BufferData(buffer, ALFormat.Stereo16, buf, buf.Length, _format.SamplesPerSecond); } else { AL.BufferData(buffer, ALFormat.Mono16, buf, buf.Length, _format.SamplesPerSecond); } Console.WriteLine("Write to Buffer: " + buffer.ToString()); AL.SourceQueueBuffer(_source, buffer); } } processed_count--; } AL.GetSource(_source, ALGetSourcei.SourceState, out _state); if ((ALSourceState)_state != ALSourceState.Playing) { AL.SourcePlay(_source); } } _CurrentTime = _TimeCode - _data.BytesNotRead / _BytesPerSecond - 0.1f; _Timer.Reset(); _Timer.Start(); }