doc/examples/encode_audio: fix hardcoded stereo sample stride

The sample generation loop hardcodes a stride of 2 (stereo) with
samples[2*j], but the channel count is dynamically selected by
select_channel_layout() which picks the layout with the highest
channel count. If the encoder supports more than 2 channels,
samples will be written at wrong offsets.

Use c->ch_layout.nb_channels as the stride instead.

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
This commit is contained in:
Jun Zhao
2026-03-09 08:40:53 +08:00
committed by toots
parent 042ff80562
commit 68e18d3a1c

View File

@@ -218,10 +218,10 @@ int main(int argc, char **argv)
samples = (uint16_t*)frame->data[0];
for (j = 0; j < c->frame_size; j++) {
samples[2*j] = (int)(sin(t) * 10000);
samples[c->ch_layout.nb_channels*j] = (int)(sin(t) * 10000);
for (k = 1; k < c->ch_layout.nb_channels; k++)
samples[2*j + k] = samples[2*j];
samples[c->ch_layout.nb_channels*j + k] = samples[c->ch_layout.nb_channels*j];
t += tincr;
}
encode(c, frame, pkt, f);