Skip to content

Commit d959a2e

Browse files
committed
Add circulate buffer copy behavior
1 parent c039974 commit d959a2e

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

virtio-snd.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,10 @@ typedef struct {
297297
// PCM frame doubly-ended queue
298298
vsnd_buf_queue_node_t buf;
299299
struct list_head buf_queue_head;
300-
// PCM frame intermediate buffer;
300+
// PCM frame intermediate buffer
301301
void *intermediate;
302+
uint32_t buf_sz;
303+
uint32_t buf_idx;
302304

303305
// playback control
304306
vsnd_stream_sel_t v;
@@ -806,14 +808,15 @@ static void virtio_snd_read_pcm_prepare(const virtio_snd_pcm_hdr_t *query,
806808
uint32_t cnfa_period_bytes = bps_rate / 10;
807809
/* Calculate the period size (in frames) for CNFA . */
808810
uint32_t cnfa_period_frames = cnfa_period_bytes / VSND_CNFA_FRAME_SZ;
809-
fprintf(stderr, "period_bytes %" PRIu32 " period_frames %" PRIu32 "\n",
810-
cnfa_period_bytes, cnfa_period_frames);
811-
/* Get the number of multiplier */
811+
/* Get the number of buffer multiplier */
812812
uint32_t mul = props->pp.buffer_bytes / props->pp.period_bytes;
813813

814814
INIT_LIST_HEAD(&props->buf_queue_head);
815+
props->buf_sz = cnfa_period_bytes * mul;
816+
props->buf_idx = 0;
817+
uint32_t sz = sizeof(*props->intermediate) * props->buf_sz;
815818
props->intermediate =
816-
(void *) malloc(sizeof(*props->intermediate) * cnfa_period_bytes * mul);
819+
(void *) malloc(sz);
817820
PaStreamParameters params = {
818821
.device = Pa_GetDefaultOutputDevice(),
819822
.channelCount = props->pp.channels,
@@ -1027,10 +1030,22 @@ static int virtio_snd_rx_stream_cb(const void *input,
10271030
int channels = props->pp.channels;
10281031
uint32_t out_buf_sz = frame_cnt * channels;
10291032
uint32_t out_buf_bytes = out_buf_sz * VSND_CNFA_FRAME_SZ;
1030-
fprintf(stderr, "out_buf_bytes %" PRIu32 "\n", out_buf_bytes);
10311033
//__virtio_snd_frame_enqueue(output, out_buf_bytes, id);
1032-
memcpy(props->intermediate, input, out_buf_bytes);
1033-
fprintf(stderr, "+++ virtio_snd_rx_stream_cb +++\n");
1034+
1035+
uint32_t idx = props->buf_idx;
1036+
uint32_t sz = props->buf_sz;
1037+
uint32_t base = (idx + out_buf_bytes > sz) ? (sz - idx) : out_buf_bytes;
1038+
uint32_t left = out_buf_bytes - base;
1039+
memcpy(props->intermediate + idx, input, base);
1040+
if(left != 0)
1041+
memcpy(props->intermediate, input + base, left);
1042+
1043+
props->buf_idx = (props->buf_idx + out_buf_bytes) % sz;
1044+
fprintf(stderr, "+++ virtio_snd_rx_stream_cb"
1045+
" idx %" PRIu32
1046+
" base %" PRIu32
1047+
" left %" PRIu32 " +++\n",
1048+
idx, base, left);
10341049

10351050
return paContinue;
10361051
}

0 commit comments

Comments
 (0)