我正在用 C#/MonoGame 编写一个 libretro 前端,我已经设法获得了一个粗糙(但有效)的视频 blitter,但现在我正在努力解决声音问题。
从 API:
/* Renders multiple audio frames in one go.
*
* One frame is defined as a sample of left and right channels, interleaved.
* I.e. int16_t buf[4] = { l, r, l, r }; would be 2 frames.
* Only one of the audio callbacks must ever be used.
*/
typedef size_t (*retro_audio_sample_batch_t)(const int16_t *data,
size_t frames);
因此,样本是有符号的 16 位整数。我正在尝试像这样使用 Stream 中的 SoundEffect:
int size = SoundEffect.GetSampleSizeInBytes(TimeSpan.FromMilliseconds((float)1000/(int)_libretro.GetAVInfo().timing.fps), (int)_libretro.GetAVInfo().timing.sample_rate, AudioChannels.Mono);
data = _libretro.GetSoundBuffer().data;
byte[] buffer = new byte[size];
for (int i = 0; i < size -1 ; i+=2)
{
Int16 chunk = Marshal.ReadInt16(data);
byte b1 = (byte)(chunk);
byte b2 = (byte)(chunk >> 8);
buffer[i+1] = b1;
buffer[i] = b2;
//move ahead 4 bytes skipping the second sound channel for now
data = data + (sizeof(byte)*4);
}
SoundEffect sound_left = new SoundEffect(buffer, (int)_libretro.GetAVInfo().timing.sample_rate, AudioChannels.Mono);
sound_left.Play();
我得到了声音,声音模式清晰可辨,但它是乱码,你看到我的实现有什么问题吗?