使用 OpenGL 混合音频

发布于 2024-10-08 02:33:55 字数 1133 浏览 8 评论 0原文

我想使用 OpenGL 混合两个(或更多)16 位音频流,我需要一些帮助

基本上我想做的是将音频数据放入纹理中,我将其绘制到帧缓冲区对象,然后读回。这不是问题,但是以给出正确结果的方式绘制数据有点问题。

我基本上有两个问题。

  1. 为了通过绘图混合数据,我需要使用混合(alpha = 0.5),但是结果不应该有任何 alpha 通道。因此,如果我渲染到例如 RGB 格式的帧缓冲区,alpha 混合仍然会按我的预期工作,并且生成的 alpha 不会写入 fbo? (我想避免为每个渲染通道读回 fbo)

纹理|sR|sG|sB|

帧缓冲区(之前)|dR|dG|dB|

帧缓冲区(之后) |dR*0.5+sR*0.5|dG*0.5+sG*0.5|dB*0.5+sB*0.5|

  1. 音频样本是带符号的 16 位整数值。是否可以通过这种方式进行有符号计算?或者我是否需要首先在 cpu 上将这些值转换为无符号,绘制它们,然后在 cpu 上再次对它们进行签名?

编辑:

我有点不清楚。我的硬件仅限于 OpenGL 3.3 硬件。我不想使用 CUDA 或 OpenCL,因为我已经使用 OpenGL 来做其他事情了。

每个音频样本都将在单独的通道中渲染,这意味着它必须与已渲染到帧缓冲区的内容“混合”。问题是如何将像素着色器的输出写入帧缓冲区(据我所知,这种混合无法通过可编程着色器访问,并且必须使用 glBlendFunc)。

EDIT2:

每个音频样本将在不同的通道中渲染,因此着色器中一次只有一个音频样本可用,这意味着它们需要在 FBO 中累积。

foreach(var audio_sample in audio_samples)
     draw(audio_sample);

并且

for(int n = 0; n < audio_samples.size(); ++n)
{
      glActiveTexture(GL_TEXTURE0 + n);
      glBindTexture(audio_sample);
}
draw_everything();

I want to mix two (or more) 16bit audio streams using OpenGL and I need a bit of help

Basically what I want to do is to put the audio data into texture which I draw to a frame buffer object and then read back. This is not a problem, however drawing the data in way that gives correct results is a bit more problematic.

I have basically two questions.

  1. In order to mix the data by drawing i need to use blending (alpha = 0.5), however the result should not have any alpha channel. So if I render to e.g. a frame buffer with the format RGB will alpha blending still work as I expect and the resulting alpha will not be written to the fbo? (I want to avoid having to read back the fbo for each render pass)

texture |sR|sG|sB|

framebuffer(before) |dR|dG|dB|

framebuffer(after)
|dR*0.5+sR*0.5|dG*0.5+sG*0.5|dB*0.5+sB*0.5|

  1. The audio samples are signed 16bit integer values. Is it possible do signed calculations this way? Or will I need to first convert the values to unsigned on the cpu, draw them, and then make them signed again on the cpu?

EDIT:

I was a bit unclear. My hardware is restricted to OpenGL 3.3 hardware. I would prefer to not use CUDA or OpenCL, since I'm alrdy using OpenGL for other stuff.

Each audio sample will be rendered in seperate passes, which means that it has to "mix" with whats already been rendered to the frame buffer. The problem is how the output from the pixel shader is written to the framebuffer (this blending is not accessible through programmable shaders, as far as i know, and one has to use glBlendFunc).

EDIT2:

Each audio sample will be rendered in different passes, so only one audio sample will be available in the shader at a time, which means that they need to be accumulated in the FBO.

foreach(var audio_sample in audio_samples)
     draw(audio_sample);

and not

for(int n = 0; n < audio_samples.size(); ++n)
{
      glActiveTexture(GL_TEXTURE0 + n);
      glBindTexture(audio_sample);
}
draw_everything();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挖鼻大婶 2024-10-15 02:33:55

坦率地说,为什么不直接使用可编程像素着色器呢?

您必须使用 OpenGL 1 固定功能管道吗?

我只是选择一个在签名 16 位灰度线性纹理上运行的可编程着色器。

编辑:

foreach(var audio_sample in audio_samples) 
 blend FBO1 + audio_sample => FBO2
 swap FBO2, FBO1 

它应该同样快,甚至更快(感谢流管道)。

Frankly, why wouldn't you just use programmable pixel shaders for that?

Do you have to use OpenGL 1 Fixed Functionality Pipeline?

I'd just go with a programmable shader operating on signed 16bit grayscale linear textures.

Edit:

foreach(var audio_sample in audio_samples) 
 blend FBO1 + audio_sample => FBO2
 swap FBO2, FBO1 

It ought to be just as fast, if not faster (thanks to streaming pipelines).

随心而道 2024-10-15 02:33:55

我同意 QDot 的观点。不过,您能否告诉我们一些您面临的硬件限制?如果您有合理的现代硬件,我什至可能建议采用 CUDA 或 OpenCL 路线,而不是通过 OpenGL。

I agree with QDot. Could you however inform us a bit about the hardware restrictions you are facing? If you have reasonable modern hardware I might even suggest to go the CUDA or OpenCL route, in stead of going through OpenGL.

殊姿 2024-10-15 02:33:55
  1. 即使目标缓冲区没有 Alpha,您也应该能够进行混合。也就是说,渲染为非二次方尺寸(rgb16 = 6 字节/像素)通常会导致性能损失。

  2. Signed 不是典型的渲染目标格式,但它确实存在于 OpenGL 4.0 规范中(表 3.12,称为 RGB16_SNORM 或 RGB16I,具体取决于您是否需要标准化表示)。

附带说明一下,您还可以使用 glBlendFunc(GL_CONSTANT_ALPHA,GL_ONE_MINUS_CONSTANT_ALPHA) 甚至不必指定每个像素的 Alpha。但这可能不适用于所有 GL 实现。

  1. You should be able to do blending even if the destination buffer does not have alpha. That said, rendering to non-power-of-two sizes (rgb16 = 6bytes/pixel) usually incurs performance penalties.

  2. Signed is not your typical render target format, but it does exist in the OpenGL 4.0 specification (Table 3.12, called RGB16_SNORM or RGB16I, depending on whether you want a normalized representation or not).

As a side note, you also have glBlendFunc(GL_CONSTANT_ALPHA,GL_ONE_MINUS_CONSTANT_ALPHA) to not even have to specify an alpha per-pixel. That may not be available on all GL implementations though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文