Skip to content

Commit

Permalink
Test callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuda-Chen committed Oct 30, 2024
1 parent 60b8dd3 commit c28a3c5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ OBJS_EXTRA :=
# command line option
OPTS :=

LDFLAGS :=
LDFLAGS := -lm

# virtio-blk
ENABLE_VIRTIOBLK ?= 1
Expand Down
36 changes: 30 additions & 6 deletions virtio-snd.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <string.h>
#include <math.h>

#define CNFA_IMPLEMENTATION
#include "CNFA_sf.h"
Expand Down Expand Up @@ -253,9 +254,7 @@ typedef struct {
bool guest_playing;
uint32_t stream_id;
} vsnd_stream_sel_t;
static vsnd_stream_sel_t v = {
.guest_playing = false,
};
static vsnd_stream_sel_t v;

/* Forward declaration */
static void virtio_snd_cb(struct CNFADriver *dev,
Expand Down Expand Up @@ -449,12 +448,14 @@ static void virtio_snd_read_pcm_release(struct virtq_desc *vq_desc,
fprintf(stderr, "virtio_snd_read_pcm_release\n");
}

double omega = 0.0;
static void virtio_snd_cb(struct CNFADriver *dev,
short *out,
short *in,
int framesp,
int framesr)
{
#if 0
/* TODO: apply lock on guest_playing */
vsnd_stream_sel_t *v_ptr = (vsnd_stream_sel_t *) dev->opaque;
bool playing = v_ptr->guest_playing;
Expand All @@ -468,6 +469,30 @@ static void virtio_snd_cb(struct CNFADriver *dev,
}

memcpy(out, vsnd_props[id].buf, output_buf_sz);
#endif
vsnd_stream_sel_t *v_ptr = (vsnd_stream_sel_t *) dev->opaque;
bool playing = v_ptr->guest_playing;

if(!playing) {
fprintf(stderr, "notplaying\n");
return;
}

int channels = dev->channelsPlay;
for(int i = 0; i < framesp; i++ )
{
// Shift phase, so we run at 440 Hz (A4)
omega += ( 3.14159 * 2 * 440. ) / dev->spsPlay;

// Make the 440 Hz tone at 10% volume and convert to short.
short value = sin( omega ) * 0.1 * 32767;

int c;
for( c = 0; c < channels; c++ )
{
*(out++) = value;
}
}
}

#define VSND_DESC_CNT 3
Expand Down Expand Up @@ -615,7 +640,6 @@ static int virtio_snd_tx_desc_handler(virtio_snd_state_t *vsnd,
return 0;
}


static void virtio_queue_notify_handler(
virtio_snd_state_t *vsnd,
int index,
Expand Down Expand Up @@ -794,10 +818,10 @@ static bool virtio_snd_reg_write(virtio_snd_state_t *vsnd,
virtio_snd_desc_handler);
break;
case VSND_QUEUE_TX:
fprintf(stderr, "TX start\n");
virtio_queue_notify_handler(vsnd, value,
virtio_snd_tx_desc_handler);
for (int i = 0; i < 10000000; i++)
asm volatile("" ::: "memory");
fprintf(stderr, "TX end\n");
break;
case VSND_QUEUE_EVT:
fprintf(stderr, "EVT\n");
Expand Down

0 comments on commit c28a3c5

Please sign in to comment.