Skip to content

Commit

Permalink
virtio-snd: improve test processes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandermbrown committed Feb 28, 2024
1 parent 19e2112 commit 5ec536f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
11 changes: 8 additions & 3 deletions examples/virtio-snd/userlevel/client_vm/cantina.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <alsa/asoundlib.h>

static char *device = "default";

static const unsigned char CantinaBand60_s16le[];
static const unsigned int CantinaBand60_s16le_len = 2646000;

Expand All @@ -10,11 +8,18 @@ static const unsigned int CantinaBand60_s16le_len = 2646000;
#define RATE 22050
#define CHUNK 2048

int main(void)
int main(int argc, char **argv)
{
int err;
snd_pcm_t *handle;
snd_pcm_sframes_t frames;

char *device;
if (argc > 1) {
device = argv[1];
} else {
device = "default";
}

if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
Expand Down
9 changes: 7 additions & 2 deletions examples/virtio-snd/userlevel/client_vm/crab_rave.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <alsa/asoundlib.h>

static char *device = "default";

static const unsigned char crab_rave_pcm_s16le[];
static const unsigned int crab_rave_pcm_s16le_len = 3064320;

Expand All @@ -14,6 +12,13 @@ int main(void)
int err;
snd_pcm_t *handle;
snd_pcm_sframes_t frames;

char *device;
if (argc > 1) {
device = argv[1];
} else {
device = "default";
}

if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
Expand Down
24 changes: 16 additions & 8 deletions examples/virtio-snd/userlevel/client_vm/record.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <alsa/asoundlib.h>
#include <stdio.h>

static char *device = "default"; /* playback device */
unsigned char buffer[16*1024]; /* some random data */
unsigned char buffer[16*1024];

typedef struct wavfile_header_s
{
Expand All @@ -24,7 +23,7 @@ typedef struct wavfile_header_s
} wavfile_header_t;

#define NUM_CHANNELS 1
#define BITS_PER_SAMPLE 8
#define BITS_PER_SAMPLE 16
#define SUBCHUNK1SIZE 16

#define SAMPLE_RATE 48000
Expand Down Expand Up @@ -85,19 +84,26 @@ int write_header(FILE *file_p,
}


int main(void)
int main(int argc, char **argv)
{
int err;
unsigned int i;
snd_pcm_t *handle;
snd_pcm_sframes_t frames;

char *device;
if (argc > 1) {
device = argv[1];
} else {
device = "default";
}

if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
printf("Capture open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
if ((err = snd_pcm_set_params(handle,
SND_PCM_FORMAT_U8,
SND_PCM_FORMAT_S16,
SND_PCM_ACCESS_RW_INTERLEAVED,
1,
SAMPLE_RATE,
Expand All @@ -115,17 +121,19 @@ int main(void)

const int buffer_count = 16;
write_header(file, SAMPLE_RATE, sizeof(buffer) * buffer_count);

const int frame_size = 2;

for (i = 0; i < buffer_count; i++) {
frames = snd_pcm_readi(handle, buffer, sizeof(buffer));
frames = snd_pcm_readi(handle, buffer, sizeof(buffer) / frame_size);
if (frames < 0)
frames = snd_pcm_recover(handle, frames, 0);
if (frames < 0) {
printf("snd_pcm_readi failed: %s\n", snd_strerror(frames));
break;
}
if (frames > 0 && frames < (long)sizeof(buffer))
printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
if (frames > 0 && frames < (long)sizeof(buffer) / frame_size)
printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer) / frame_size, frames);

fwrite(buffer, 1, sizeof(buffer), file);
}
Expand Down

0 comments on commit 5ec536f

Please sign in to comment.