增加示例代码.Signed-off-by: ithink.chan <chenyang@autoai.com>

This commit is contained in:
ithink.chan 2020-04-14 16:46:50 +08:00
parent 619dc98f44
commit 49f7904b6f
9 changed files with 468 additions and 0 deletions

View File

@ -0,0 +1,3 @@
Copyright (C) 2006 by Rob Landley <rob@landley.net>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

View File

@ -0,0 +1,15 @@
.PHONY:all
.PHONY:clean
target = virtio_audio_auto_utest
objects = virtio_audio_auto_utest.o
all:$(objects)
gcc $(objects) -o $(target) -lasound
clean:
rm -rf $(target)
rm -rf $(objects)
$(objects):%.o: %.c
gcc -c $< -o $@

View File

@ -0,0 +1,3 @@
# auto-audio-utest
声音虚拟化uos端测试程序。

View File

@ -0,0 +1,173 @@
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#define PERIOD_CNT 16
#define PERIOD_FRAMES 1024
#define PERIOD_BYTES 4096
#define BUF_SZ (PERIOD_CNT * PERIOD_FRAMES)
#define TOTAL_BUF (PERIOD_CNT * PERIOD_BYTES)
#define BUF_CNT 64
int cpnxt (char* buf, FILE* fd)
{
size_t read;
if (NULL == buf)
return -1;
read = fread (buf, 1, TOTAL_BUF * BUF_CNT, fd);
return TOTAL_BUF * BUF_CNT == read ? 0 : -1;
}
int main (int argc, char *argv[])
{
int i;
int err;
FILE* fd;
char buf[TOTAL_BUF * BUF_CNT];
snd_pcm_t *playback_handle;
snd_pcm_hw_params_t *hw_params;
snd_pcm_sw_params_t *sw_params;
unsigned int rate = 48000;
unsigned int periods = PERIOD_CNT;
snd_pcm_uframes_t period_size = PERIOD_FRAMES;
snd_pcm_uframes_t buffer_size = BUF_SZ;
printf ("V1\n");
if (argc<2) {
printf ("Use plughw:0,0\n");
if ((err = snd_pcm_open (&playback_handle, "plughw:0,0", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf ("cannot open audio device %s (%s)\n",
argv[1],
snd_strerror (err));
exit (1);
}
} else {
if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf ("cannot open audio device %s (%s)\n",
argv[1],
snd_strerror (err));
exit (1);
}
}
fd = fopen(argv[2], "r");
if (NULL != fd) {
fread (buf, 1, TOTAL_BUF * BUF_CNT, fd);
}
else {
for (i = 0; i < TOTAL_BUF * BUF_CNT / sizeof(i); i++)
memcpy(&buf[i * sizeof(i)], &i, sizeof(i));
}
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
printf ("cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
printf ("cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
printf ("cannot set access type (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
printf ("cannot set sample format (%s)\n",
snd_strerror (err));
// exit (1);
}
if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, &rate, 0)) < 0) {
printf ("cannot set sample rate (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_periods_near(playback_handle, hw_params, &periods, NULL)) < 0) {
printf ("cannot set periods (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_period_size_near(playback_handle, hw_params, &period_size, NULL)) < 0) {
printf ("cannot set period size (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_buffer_size_near(playback_handle, hw_params, &buffer_size)) < 0) {
printf ("cannot set buffer size (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, 2)) < 0) {
printf ("cannot set channel count (%s)\n",
snd_strerror (err));
// exit (1);
}
if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) {
printf ("cannot set hardware parameters (%s)\n",
snd_strerror (err));
exit (1);
}
snd_pcm_sw_params_alloca (&sw_params);
if ((err = snd_pcm_sw_params_current(playback_handle, sw_params)) < 0) {
printf ("cannot get current software parameters (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_sw_params_set_start_threshold (playback_handle, sw_params, 0)) < 0) {
printf ("cannot set threshold (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_sw_params (playback_handle, sw_params)) < 0) {
printf ("cannot set software parameters (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_prepare (playback_handle)) < 0) {
printf ("cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
do {
for (i = 0; i < BUF_CNT / 2; ++i) {
printf ("PCM Write 0x%x\n", buf[2 * TOTAL_BUF * i]);
if ((err = snd_pcm_writei (playback_handle,
&buf[2 * TOTAL_BUF * i],
2 * PERIOD_CNT * PERIOD_FRAMES)) != 2 * PERIOD_CNT * PERIOD_FRAMES) {
printf ("write to audio interface failed (%s)\n",
snd_strerror (err));
// exit (1);
}
// sleep(10);
}
} while ( 0 == cpnxt (buf, fd));
// sleep(1);
snd_pcm_drain (playback_handle);
snd_pcm_hw_params_free (hw_params);
snd_pcm_sw_params_free (sw_params);
snd_pcm_close (playback_handle);
if (NULL != fd) {
fclose (fd);
}
exit (0);
}

View File

@ -0,0 +1,8 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CFLAGS += -pie -fPIE
LOCAL_LDFLAGS += -pie -fPIE
LOCAL_MODULE := MeasureTime
LOCAL_SRC_FILES := MeasureTime.c
LOCAL_SYSTEM_SHARED_LIBRARIES := libasound libc
include $(BUILD_EXECUTABLE)

View File

@ -0,0 +1,3 @@
Copyright (C) 2006 by Rob Landley <rob@landley.net>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

View File

@ -0,0 +1,15 @@
.PHONY:all
.PHONY:clean
target = MeasureTime
objects = MeasureTime.o
all:$(objects)
gcc $(objects) -o $(target) -lasound
clean:
rm -rf $(target)
rm -rf $(objects)
$(objects):%.o: %.c
gcc -c $< -o $@

View File

@ -0,0 +1,245 @@
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <signal.h>
#define PERIOD_CNT 4
#define PERIOD_FRAMES 4096
#define PERIOD_BYTES (4*PERIOD_FRAMES)
#define BUF_SZ (PERIOD_CNT * PERIOD_FRAMES)
#define TOTAL_BUF (PERIOD_CNT * PERIOD_BYTES)
void init_pcm (snd_pcm_t *handle, int type)
{
int err;
snd_pcm_hw_params_t *hw_params;
snd_pcm_sw_params_t *sw_params;
unsigned int rate = 48000;
unsigned int periods = PERIOD_CNT;
snd_pcm_uframes_t period_size = PERIOD_FRAMES;
snd_pcm_uframes_t buffer_size = BUF_SZ;
if (0==type) {
periods = 4;
period_size = PERIOD_FRAMES;
buffer_size = BUF_SZ;
} else {
periods = 4;
period_size = PERIOD_FRAMES;
buffer_size = BUF_SZ;
}
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
printf ("cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
printf ("cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
printf ("cannot set access type (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
printf ("cannot set sample format (%s)\n",
snd_strerror (err));
// exit (1);
}
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &rate, 0)) < 0) {
printf ("cannot set sample rate (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_periods_near(handle, hw_params, &periods, NULL)) < 0) {
printf ("cannot set periods (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_period_size_near(handle, hw_params, &period_size, NULL)) < 0) {
printf ("cannot set period size (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_buffer_size_near(handle, hw_params, &buffer_size)) < 0) {
printf ("cannot set buffer size (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, 2)) < 0) {
printf ("cannot set channel count (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
printf ("cannot set hardware parameters (%s)\n",
snd_strerror (err));
exit (1);
}
snd_pcm_sw_params_alloca (&sw_params);
if ((err = snd_pcm_sw_params_current(handle, sw_params)) < 0) {
printf ("cannot get current software parameters (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, 16384)) < 0) {
printf ("cannot set threshold (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
printf ("cannot set software parameters (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_prepare (handle)) < 0) {
printf ("cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
}
snd_pcm_t* init_playback (char output[])
{
int err;
snd_pcm_t *playback_handle;
if ((err = snd_pcm_open (&playback_handle, output, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf ("cannot open audio device %s (%s)\n",
output,
snd_strerror (err));
exit (1);
}
init_pcm(playback_handle, 0);
return playback_handle;
}
#if 0
snd_pcm_t* init_capture(char input[])
{
int err;
snd_pcm_t *capture_handle;
if ((err = snd_pcm_open (&capture_handle, input, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
printf ("cannot open audio device %s (%s)\n",
input,
snd_strerror (err));
exit (1);
}
init_pcm(capture_handle, 1);
return capture_handle;
}
#endif
void meCase0 (char* arg2, char* arg3)
{
snd_pcm_t* playback_handle;
char buf[4*PERIOD_BYTES];
unsigned long utime=0;
struct timeval start,end;
long sec, usec;
sscanf (arg2, "%lu", &utime);
memset (buf, 0, 4*PERIOD_BYTES);
playback_handle = init_playback (arg3);
snd_pcm_writei (playback_handle, buf, 4*PERIOD_FRAMES);
snd_pcm_pause (playback_handle, 1);
gettimeofday (&start, NULL);
sleep (utime);
snd_pcm_close (playback_handle);
gettimeofday (&end, NULL);
sec = end.tv_sec-start.tv_sec;
usec = end.tv_usec-start.tv_usec;
if(0>usec) {
sec--;
usec+=1000000;
}
printf ("Use time is: %ld.%ld.\r\n", sec, usec);
}
volatile unsigned char OverTime = 1;
void meCase1Cb (int signum)
{
(void)signum;
OverTime = 0;
}
void meCase1 (char* arg2, char* arg3)
{
snd_pcm_t* playback_handle;
char buf[4*PERIOD_BYTES];
unsigned long utime=0;
struct timeval start,end;
long sec, usec;
sscanf (arg2, "%lu", &utime);
memset (buf, 0, 4*PERIOD_BYTES);
signal (SIGALRM, meCase1Cb);
playback_handle = init_playback (arg3);
snd_pcm_writei (playback_handle, buf, 4*PERIOD_FRAMES);
snd_pcm_pause (playback_handle, 1);
gettimeofday (&start, NULL);
alarm(utime);
while(OverTime);
snd_pcm_close (playback_handle);
gettimeofday (&end, NULL);
sec = end.tv_sec-start.tv_sec;
usec = end.tv_usec-start.tv_usec;
if(0>usec) {
sec--;
usec+=1000000;
}
printf ("Use time is: %ld.%ld.\r\n", sec, usec);
}
int main (int argc, char *argv[])
{
if (2==argc) {
if (0==strcmp("-v", argv[1])) {
printf ("MeasureTime Version 1.0\r\n");
exit (0);
}
if (0==strcmp("-h", argv[1])) {
printf ("MeasureTime <case> <case arg>\r\nExp: MeasureTime 0 60 plughw:0,0\r\n");
exit (0);
}
}
if (3>argc) {
printf ("MeasureTime <case> <case arg>\r\nExp: MeasureTime 0 60 plughw:0,0\r\n");
exit (0);
}
if (0==strcmp("0", argv[1])) {
if (4!=argc) {
printf ("MeasureTime 0 <time(s)> <pcm>\r\nExp: MeasureTime 0 60 plughw:0,0\r\n");
exit (0);
}
meCase0 (argv[2], argv[3]);
}
if (0==strcmp("1", argv[1])) {
if (4!=argc) {
printf ("MeasureTime 0 <time(s)> <pcm>\r\nExp: MeasureTime 0 60 plughw:0,0\r\n");
exit (0);
}
meCase1 (argv[2], argv[3]);
}
exit (0);
}

View File

@ -0,0 +1,3 @@
# auto-measure-time
时间虚拟化测试验证程序。