Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuda-Chen committed Dec 16, 2024
1 parent 91a1c7f commit c1c3187
Showing 1 changed file with 97 additions and 100 deletions.
197 changes: 97 additions & 100 deletions virtio-snd.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum {
VSND_FEATURES_1,
};

/* supported control messages */
enum {
/* jack control requests types */
VIRTIO_SND_R_JACK_INFO = 1,
Expand Down Expand Up @@ -65,38 +66,30 @@ enum {

/* supported PCM frame rates */
enum {
VIRTIO_SND_PCM_RATE_5512 = 0, /* 5512 Hz */
VIRTIO_SND_PCM_RATE_8000, /* 8000 Hz */
VIRTIO_SND_PCM_RATE_11025, /* 11025 Hz */
VIRTIO_SND_PCM_RATE_16000, /* 16000 Hz */
VIRTIO_SND_PCM_RATE_22050, /* 22050 Hz */
VIRTIO_SND_PCM_RATE_32000, /* 32000 Hz */
VIRTIO_SND_PCM_RATE_44100, /* 44100 Hz */
VIRTIO_SND_PCM_RATE_48000, /* 48000 Hz */
VIRTIO_SND_PCM_RATE_64000, /* 64000 Hz */
VIRTIO_SND_PCM_RATE_88200, /* 88200 Hz */
VIRTIO_SND_PCM_RATE_96000, /* 96000 Hz */
VIRTIO_SND_PCM_RATE_176400, /* 176400 Hz */
VIRTIO_SND_PCM_RATE_192000, /* 192000 Hz */
VIRTIO_SND_PCM_RATE_384000, /* 384000 Hz */
#define _(rate) VIRTIO_SND_PCM_RATE_##rate
_(5512) = 0, /* 5512 Hz */
_(8000), /* 8000 Hz */
_(11025), /* 11025 Hz */
_(16000), /* 16000 Hz */
_(22050), /* 22050 Hz */
_(32000), /* 32000 Hz */
_(44100), /* 44100 Hz */
_(48000), /* 48000 Hz */
_(64000), /* 64000 Hz */
_(88200), /* 88200 Hz */
_(96000), /* 96000 Hz */
_(176400), /* 176400 Hz */
_(192000), /* 192000 Hz */
_(384000), /* 384000 Hz */
#undef _
};

/* supported PCM frames rates mapping */
int pcm_rate_tbl[] = {
[VIRTIO_SND_PCM_RATE_5512] = 5512,
[VIRTIO_SND_PCM_RATE_8000] = 8000,
[VIRTIO_SND_PCM_RATE_11025] = 11025,
[VIRTIO_SND_PCM_RATE_16000] = 16000,
[VIRTIO_SND_PCM_RATE_22050] = 22050,
[VIRTIO_SND_PCM_RATE_32000] = 32000,
[VIRTIO_SND_PCM_RATE_44100] = 44100,
[VIRTIO_SND_PCM_RATE_48000] = 48000,
[VIRTIO_SND_PCM_RATE_64000] = 64000,
[VIRTIO_SND_PCM_RATE_88200] = 88200,
[VIRTIO_SND_PCM_RATE_96000] = 96000,
[VIRTIO_SND_PCM_RATE_176400] = 176400,
[VIRTIO_SND_PCM_RATE_192000] = 192000,
[VIRTIO_SND_PCM_RATE_384000] = 384000,
#define _(rate) [VIRTIO_SND_PCM_RATE_##rate] = rate
_(5512), _(8000), _(11025), _(16000), _(22050), _(32000), _(44100),
_(48000), _(64000), _(88200), _(96000), _(176400), _(192000), _(384000),
#undef _
};

/* supported PCM stream features */
Expand All @@ -106,74 +99,78 @@ enum {

/* supported PCM sample formats */
enum {
/* analog formats (width / physical width) */
VIRTIO_SND_PCM_FMT_IMA_ADPCM = 0, /* 4 / 4 bits */
VIRTIO_SND_PCM_FMT_MU_LAW, /* 8 / 8 bits */
VIRTIO_SND_PCM_FMT_A_LAW, /* 8 / 8 bits */
VIRTIO_SND_PCM_FMT_S8, /* 8 / 8 bits */
VIRTIO_SND_PCM_FMT_U8, /* 8 / 8 bits */
VIRTIO_SND_PCM_FMT_S16, /* 16 / 16 bits */
VIRTIO_SND_PCM_FMT_U16, /* 16 / 16 bits */
VIRTIO_SND_PCM_FMT_S18_3, /* 18 / 24 bits */
VIRTIO_SND_PCM_FMT_U18_3, /* 18 / 24 bits */
VIRTIO_SND_PCM_FMT_S20_3, /* 20 / 24 bits */
VIRTIO_SND_PCM_FMT_U20_3, /* 20 / 24 bits */
VIRTIO_SND_PCM_FMT_S24_3, /* 24 / 24 bits */
VIRTIO_SND_PCM_FMT_U24_3, /* 24 / 24 bits */
VIRTIO_SND_PCM_FMT_S20, /* 20 / 32 bits */
VIRTIO_SND_PCM_FMT_U20, /* 20 / 32 bits */
VIRTIO_SND_PCM_FMT_S24, /* 24 / 32 bits */
VIRTIO_SND_PCM_FMT_U24, /* 24 / 32 bits */
VIRTIO_SND_PCM_FMT_S32, /* 32 / 32 bits */
VIRTIO_SND_PCM_FMT_U32, /* 32 / 32 bits */
VIRTIO_SND_PCM_FMT_FLOAT, /* 32 / 32 bits */
VIRTIO_SND_PCM_FMT_FLOAT64, /* 64 / 64 bits */
/* analog formats (width / physical width) */
#define _(samp_fmt) VIRTIO_SND_PCM_FMT_##samp_fmt
_(IMA_ADPCM) = 0, /* 4 / 4 bits */
_(MU_LAW), /* 8 / 8 bits */
_(A_LAW), /* 8 / 8 bits */
_(S8), /* 8 / 8 bits */
_(U8), /* 8 / 8 bits */
_(S16), /* 16 / 16 bits */
_(U16), /* 16 / 16 bits */
_(S18_3), /* 18 / 24 bits */
_(U18_3), /* 18 / 24 bits */
_(S20_3), /* 20 / 24 bits */
_(U20_3), /* 20 / 24 bits */
_(S24_3), /* 24 / 24 bits */
_(U24_3), /* 24 / 24 bits */
_(S20), /* 20 / 32 bits */
_(U20), /* 20 / 32 bits */
_(S24), /* 24 / 32 bits */
_(U24), /* 24 / 32 bits */
_(S32), /* 32 / 32 bits */
_(U32), /* 32 / 32 bits */
_(FLOAT), /* 32 / 32 bits */
_(FLOAT64), /* 64 / 64 bits */
/* digital formats (width / physical width) */
VIRTIO_SND_PCM_FMT_DSD_U8, /* 8 / 8 bits */
VIRTIO_SND_PCM_FMT_DSD_U16, /* 16 / 16 bits */
VIRTIO_SND_PCM_FMT_DSD_U32, /* 32 / 32 bits */
VIRTIO_SND_PCM_FMT_IEC958_SUBFRAME, /* 32 / 32 bits */
_(DSD_U8), /* 8 / 8 bits */
_(DSD_U16), /* 16 / 16 bits */
_(DSD_U32), /* 32 / 32 bits */
_(IEC958_SUBFRAME), /* 32 / 32 bits */
#undef _
};

/* standard channel position definition */
enum {
VIRTIO_SND_CHMAP_NONE = 0, /* undefined */
VIRTIO_SND_CHMAP_NA, /* silent */
VIRTIO_SND_CHMAP_MONO, /* mono stream */
VIRTIO_SND_CHMAP_FL, /* front left */
VIRTIO_SND_CHMAP_FR, /* front right */
VIRTIO_SND_CHMAP_RL, /* rear left */
VIRTIO_SND_CHMAP_RR, /* rear right */
VIRTIO_SND_CHMAP_FC, /* front center */
VIRTIO_SND_CHMAP_LFE, /* low frequency (LFE) */
VIRTIO_SND_CHMAP_SL, /* side left */
VIRTIO_SND_CHMAP_SR, /* side right */
VIRTIO_SND_CHMAP_RC, /* rear center */
VIRTIO_SND_CHMAP_FLC, /* front left center */
VIRTIO_SND_CHMAP_FRC, /* front right center */
VIRTIO_SND_CHMAP_RLC, /* rear left center */
VIRTIO_SND_CHMAP_RRC, /* rear right center */
VIRTIO_SND_CHMAP_FLW, /* front left wide */
VIRTIO_SND_CHMAP_FRW, /* front right wide */
VIRTIO_SND_CHMAP_FLH, /* front left high */
VIRTIO_SND_CHMAP_FCH, /* front center high */
VIRTIO_SND_CHMAP_FRH, /* front right high */
VIRTIO_SND_CHMAP_TC, /* top center */
VIRTIO_SND_CHMAP_TFL, /* top front left */
VIRTIO_SND_CHMAP_TFR, /* top front right */
VIRTIO_SND_CHMAP_TFC, /* top front center */
VIRTIO_SND_CHMAP_TRL, /* top rear left */
VIRTIO_SND_CHMAP_TRR, /* top rear right */
VIRTIO_SND_CHMAP_TRC, /* top rear center */
VIRTIO_SND_CHMAP_TFLC, /* top front left center */
VIRTIO_SND_CHMAP_TFRC, /* top front right center */
VIRTIO_SND_CHMAP_TSL, /* top side left */
VIRTIO_SND_CHMAP_TSR, /* top side right */
VIRTIO_SND_CHMAP_LLFE, /* left LFE */
VIRTIO_SND_CHMAP_RLFE, /* right LFE */
VIRTIO_SND_CHMAP_BC, /* bottom center */
VIRTIO_SND_CHMAP_BLC, /* bottom left center */
VIRTIO_SND_CHMAP_BRC, /* bottom right center */
#define _(chmap_pos) VIRTIO_SND_CHMAP_##chmap_pos
_(NONE) = 0, /* undefined */
_(NA), /* silent */
_(MONO), /* mono stream */
_(FL), /* front left */
_(FR), /* front right */
_(RL), /* rear left */
_(RR), /* rear right */
_(FC), /* front center */
_(LFE), /* low frequency (LFE) */
_(SL), /* side left */
_(SR), /* side right */
_(RC), /* rear center */
_(FLC), /* front left center */
_(FRC), /* front right center */
_(RLC), /* rear left center */
_(RRC), /* rear right center */
_(FLW), /* front left wide */
_(FRW), /* front right wide */
_(FLH), /* front left high */
_(FCH), /* front center high */
_(FRH), /* front right high */
_(TC), /* top center */
_(TFL), /* top front left */
_(TFR), /* top front right */
_(TFC), /* top front center */
_(TRL), /* top rear left */
_(TRR), /* top rear right */
_(TRC), /* top rear center */
_(TFLC), /* top front left center */
_(TFRC), /* top front right center */
_(TSL), /* top side left */
_(TSR), /* top side right */
_(LLFE), /* left LFE */
_(RLFE), /* right LFE */
_(BC), /* bottom center */
_(BLC), /* bottom left center */
_(BRC), /* bottom right center */
#undef _
};

/* audio data flow direction */
Expand Down Expand Up @@ -265,7 +262,7 @@ typedef struct {
/* Adapted from DPDK ring buffer. */
/* https://github.com/scylladb/dpdk/blob/master/lib/librte_ring/rte_ring.h#L147
*/
#define VSND_RING_SZ_MASK (uint32_t)(0x0fffffff) /* ring size mask */
#define VSND_RING_SZ_MASK 0x0FFFFFFFULL /* ring size mask */
typedef struct {
void *buffer;
struct prod {
Expand Down Expand Up @@ -578,7 +575,7 @@ static void virtio_snd_read_pcm_release(const virtio_snd_pcm_hdr_t *query,
double omega = 0.0;
int totalframesp = 0;
int totalframesr = 0;
static void __virtio_snd_frame_dequeue(void **out,
static void __virtio_snd_frame_dequeue(void *out,
uint32_t n,
uint32_t stream_id)
{
Expand All @@ -605,10 +602,10 @@ static void __virtio_snd_frame_dequeue(void **out,
uint32_t size = props->pp.buffer_bytes;
uint32_t idx = cons_head & mask;
if (idx + n < size) {
memcpy(*out, props->ring.buffer + idx, n);
memcpy(out, props->ring.buffer + idx, n);
} else {
memcpy(*out, props->ring.buffer + idx, size - idx);
memcpy(*out + (size - idx), props->ring.buffer, n - (size - idx));
memcpy(out, props->ring.buffer + idx, size - idx);
memcpy(out + (size - idx), props->ring.buffer, n - (size - idx));
}
asm("" ::: "memory");

Expand All @@ -635,18 +632,18 @@ static void virtio_snd_cb(struct CNFADriver *dev,
totalframesr += framesr;
totalframesp += framesp;

fprintf(stderr, "start to play\n");
int channels = dev->channelsPlay;
uint32_t id = v_ptr->stream_id;
uint32_t out_buf_sz = framesp * channels;
fprintf(stderr, "start to play with out_buf_sz %" PRIu32 "\n", out_buf_sz);

/*if (!(playing)) {
memset(out, 0, sizeof(short) * out_buf_sz);
return;
}*/

/* TODO: add single consumer */
__virtio_snd_frame_dequeue((void **) &out, out_buf_sz, id);
__virtio_snd_frame_dequeue(out, out_buf_sz, id);
#if 0
for (int i = 0; i < framesp; i++) {
// Shift phase, so we run at 440 Hz (A4)
Expand Down Expand Up @@ -772,9 +769,9 @@ static void __virtio_snd_frame_enqueue(void *payload,
* prod_head > cons_tail). So 'free_entries' is always between 0
* and size(ring)-1. */
free_entries = mask + cons_tail - prod_head;
fprintf(stderr,
/*fprintf(stderr,
"mask %" PRIu32 " cons_tail %" PRIu32 " prod_head %" PRIu32 "\n",
mask, cons_tail, prod_head);
mask, cons_tail, prod_head);*/

/* Move prod_head. */
if (n > free_entries)
Expand Down

0 comments on commit c1c3187

Please sign in to comment.