Skip to content

Commit

Permalink
Set appropriate period size of CNFA
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuda-Chen committed Jan 9, 2025
1 parent 34c76a9 commit 712d08a
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions virtio-snd.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#define PRIV(x) ((virtio_snd_config_t *) x->priv)

#define VSND_CNFA_FRAME_SZ 2

enum {
VSND_QUEUE_CTRL = 0,
VSND_QUEUE_EVT = 1,
Expand Down Expand Up @@ -514,15 +516,28 @@ static void virtio_snd_read_pcm_prepare(const virtio_snd_pcm_hdr_t *query,
// pthread_mutex_unlock(&virtio_snd_mutex);
props->pp.hdr.hdr.code = VIRTIO_SND_R_PCM_PREPARE;
uint32_t period_bytes = props->pp.period_bytes;
uint32_t channels = props->pp.channels;
uint32_t rate = pcm_rate_tbl[props->pp.rate];

// Calculate bps rate
uint32_t bps_rate = channels * VSND_CNFA_FRAME_SZ * rate;
// Calculate period bytes for ~100ms interrupt period
uint32_t cnfa_period_bytes = bps_rate / 10;
// Calculate the period size (in frames) for CNFA
uint32_t cnfa_period_frames = cnfa_period_bytes / VSND_CNFA_FRAME_SZ;
fprintf(stderr,
"bps_rate %" PRIu32 " cnfa_period_bytes %" PRIu32
" cnfa_period_frames %" PRIu32 "\n",
bps_rate, cnfa_period_bytes, cnfa_period_frames);

/* The buffer size in frames when calling CNFAInit()
* is actually period size (i.e., period size then divide
* the length of frame). */
/* CNFA only accept frame with signed 16-bit data in little-endian. */
props->audio_host = CNFAInit(
NULL, "semu-virtio-snd", virtio_snd_cb, pcm_rate_tbl[props->pp.rate], 0,
props->pp.channels, 0, period_bytes / sizeof(short), NULL, NULL, &v);
uint32_t sz = period_bytes * 3;
props->audio_host =
CNFAInit(NULL, "semu-virtio-snd", virtio_snd_cb, rate, 0, channels, 0,
cnfa_period_frames, NULL, NULL, &v);
uint32_t sz = props->pp.buffer_bytes;
props->ring.buffer = (void *) malloc(sizeof(void) * sz);
props->ring.prod.head = 0;
props->ring.prod.tail = 0;
Expand Down Expand Up @@ -698,12 +713,12 @@ static void virtio_snd_cb(struct CNFADriver *dev,
totalframesp += framesp;

uint32_t id = v_ptr->stream_id;
// fprintf(stderr, "start to play with out_buf_sz %" PRIu32 "\n",
// out_buf_sz);
uint32_t out_buf_bytes = out_buf_sz * VSND_CNFA_FRAME_SZ;
fprintf(stderr, "start to play with out_buf_sz %" PRIu32 "\n", out_buf_sz);

/* TODO: add single consumer */
#if 1
__virtio_snd_frame_dequeue(out, out_buf_sz, id);
__virtio_snd_frame_dequeue(out, out_buf_bytes, id);
#endif
#if 0
for (int i = 0; i < framesp; i++) {
Expand Down

0 comments on commit 712d08a

Please sign in to comment.