tinyplay基础功能扩展

tinyplay作为tinyalsa中的播放命令简洁方便,为了让其拥有更多的功能,我对其源代码进行了修改,增加了以下功能

1. 可通过传参控制播放次数,也可无限循环

2. 播放和录音同时进行(播录分开)

3. 可将播放的数据录音保存到一个文件(播啥录啥)

扩展:将录音数据实时播放,可有回音效果(录啥播啥),原理与3类似,这里没有设计

大体设计思路如下:

1. 在play_sample()函数里面找到读写数据的那一部分,然后再加上循环即可

2. 在play_sample_c()函数里面传入播放文件所需的相关信息参数,然后在该函数内会初始化录音所需的相关参数,然后将播放和录音的两部分的参数都传到capture_sample_c()内,该函数会同时进行播放和录音的工作

3. 在play_sample_s()函数内传入播放文件所需的相关参数,然后在函数内会完成录音所需的初始化工作,然后将播放所需的参数传入到capture_sample_s(),该函数会把需要播放的数据写到录音文件内后再写入到PCM对象,最后再跳出来生成录音文件

使用方法

1. 传入参数-t n,n为循环播放的次数,n=0则是无限循环

2. 传入参数-c test.c,test.c为录音文件

3. 传入参数-s test.c,test.s为录音文件

代码如下:

/* tinyplay.c
**
** Copyright 2011, The Android Open Source Project
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
**     * Redistributions of source code must retain the above copyright
**       notice, this list of conditions and the following disclaimer.
**     * Redistributions in binary form must reproduce the above copyright
**       notice, this list of conditions and the following disclaimer in the
**       documentation and/or other materials provided with the distribution.
**     * Neither the name of The Android Open Source Project nor the names of
**       its contributors may be used to endorse or promote products derived
**       from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
** DAMAGE.
*/

#include <tinyalsa/asoundlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
#include <endian.h>

#define ID_RIFF 0x46464952
#define ID_WAVE 0x45564157
#define ID_FMT  0x20746d66
#define ID_DATA 0x61746164

#define FORMAT_PCM 1

struct riff_wave_header {
    uint32_t riff_id;
    uint32_t riff_sz;
    uint32_t wave_id;
};

struct chunk_header {
    uint32_t id;
    uint32_t sz;
};

struct chunk_fmt {
    uint16_t audio_format;
    uint16_t num_channels;
    uint32_t sample_rate;
    uint32_t byte_rate;
    uint16_t block_align;
    uint16_t bits_per_sample;
};

static int close = 0;

void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels,
                 unsigned int rate, unsigned int bits, unsigned int period_size,
                 unsigned int period_count, uint32_t data_sz, unsigned int times);

void stream_close(int sig)
{
    /* allow the stream to be closed gracefully */
    signal(sig, SIG_IGN);
    close = 1;
}

/*tinycap*/
struct wav_header {
    uint32_t riff_id;
    uint32_t riff_sz;
    uint32_t riff_fmt;
    uint32_t fmt_id;
    uint32_t fmt_sz;
    uint16_t audio_format;
    uint16_t num_channels;
    uint32_t sample_rate;
    uint32_t byte_rate;
    uint16_t block_align;
    uint16_t bits_per_sample;
    uint32_t data_id;
    uint32_t data_sz;
};

int capturing = 1;

unsigned int capture_sample_c(FILE *file, unsigned int card, unsigned int device,
                              unsigned int channels, unsigned int rate,
                              enum pcm_format format, unsigned int period_size,
                              unsigned int period_count, unsigned int cap_tim,
							  FILE *file_p, unsigned int card_p, unsigned int device_p, 
							  unsigned int channels_p,unsigned int rate_p,
							  unsigned int bits_p,unsigned int period_size_p,
							  unsigned int period_count_p,uint32_t data_sz_p);
 
void play_sample_c(FILE *file_p, unsigned int card_p, unsigned int device_p, 
				   unsigned int channels_p, unsigned int rate_p, unsigned int bits_p,
				   unsigned int period_size_p, unsigned int period_count_p,
				   uint32_t data_sz_p, char *filename);

unsigned int capture_sample_s(FILE *file, FILE *file_p, unsigned int card_p,
							  unsigned int device_p, unsigned int channels_p,
							  unsigned int rate_p, unsigned int bits_p,
							  unsigned int period_size_p, unsigned int period_count_p,
							  uint32_t data_sz_p);

void play_sample_s(FILE *file_p, unsigned int card_p, unsigned int device_p, 
				   unsigned int channels_p, unsigned int rate_p, unsigned int bits_p,
				   unsigned int period_size_p,unsigned int period_count_p,
				   uint32_t data_sz_p, char *filename);

void sigint_handler(int sig __attribute__((unused)))
{
    capturing = 0;
    signal(sig, SIG_IGN);
    close = 1;
}

int main(int argc, char **argv)
{
    FILE *file;
    struct riff_wave_header riff_wave_header;
    struct chunk_header chunk_header;
    struct chunk_fmt chunk_fmt;
    unsigned int device = 0;
    unsigned int card = 0;
    unsigned int period_size = 1024;
    unsigned int period_count = 4;
    char *filename;
    char *filenamea;
    char *filenameb;
    int more_chunks = 1;
    int times = 1;
    int mode = 1;

    if (argc < 2) {
        fprintf(stderr, "Usage: %s file.wav [-D card] [-d device] [-p period_size]"
                " [-n n_periods] \n", argv[0]);
        return 1;
    }

    filename = argv[1];
    file = fopen(filename, "rb");
    if (!file) {
        fprintf(stderr, "Unable to open file '%s'\n", filename);
        return 1;
    }

    fread(&riff_wave_header, sizeof(riff_wave_header), 1, file);
    if ((riff_wave_header.riff_id != ID_RIFF) ||
        (riff_wave_header.wave_id != ID_WAVE)) {
        fprintf(stderr, "Error: '%s' is not a riff/wave file\n", filename);
        fclose(file);
        return 1;
    }

    do {
        fread(&chunk_header, sizeof(chunk_header), 1, file);

        switch (chunk_header.id) {
        case ID_FMT:
            fread(&chunk_fmt, sizeof(chunk_fmt), 1, file);
            /* If the format header is larger, skip the rest */
            if (chunk_header.sz > sizeof(chunk_fmt))
                fseek(file, chunk_header.sz - sizeof(chunk_fmt), SEEK_CUR);
            break;
        case ID_DATA:
            /* Stop looking for chunks */
            more_chunks = 0;
            chunk_header.sz = le32toh(chunk_header.sz);
            break; 
        default:
            /* Unknown chunk, skip bytes */
            fseek(file, chunk_header.sz, SEEK_CUR);
        }
    } while (more_chunks);

    /* parse command line arguments */
    argv += 2;
    while (*argv) {
        if (strcmp(*argv, "-d") == 0) {
            argv++;
            if (*argv)
                device = atoi(*argv);
        }
        if (strcmp(*argv, "-p") == 0) {
            argv++;
            if (*argv)
                period_size = atoi(*argv);
        }
        if (strcmp(*argv, "-n") == 0) {
            argv++;
            if (*argv)
                period_count = atoi(*argv);
        }
        if (strcmp(*argv, "-D") == 0) {
            argv++;
            if (*argv)
                card = atoi(*argv);
        }
		if (strcmp(*argv, "-t") == 0) {
			argv++;
			if (*argv)
				times = atoi(*argv);
		}
		if (strcmp(*argv, "-c") == 0) {
			argv++;
			if (*argv)
				filenamea = *argv;
			mode = 2;
		}
		if (strcmp(*argv, "-s") == 0) {
			argv++;
			if (*argv)
				filenameb = *argv;
			mode = 3;
		}
		if (*argv)
			argv++;
	}

    switch (mode) {
	    case 1:
            play_sample(file, card, device, chunk_fmt.num_channels, 
						chunk_fmt.sample_rate, chunk_fmt.bits_per_sample, 
						period_size, period_count, chunk_header.sz, times);
	    break;
	    case 2:
	        play_sample_c(file, card, device, chunk_fmt.num_channels, 
						  chunk_fmt.sample_rate, chunk_fmt.bits_per_sample, 
						  period_size, period_count, chunk_header.sz, filenamea);
	    break;
	    case 3:
	        play_sample_s(file, card, device, chunk_fmt.num_channels, 
						  chunk_fmt.sample_rate, chunk_fmt.bits_per_sample, 
						  period_size, period_count, chunk_header.sz, filenameb);
	    break;
    }

    fclose(file);

    return 0;
}

int check_param(struct pcm_params *params, unsigned int param, unsigned int value,
                char *param_name, char *param_unit)
{
    unsigned int min;
    unsigned int max;
    int is_within_bounds = 1;

    min = pcm_params_get_min(params, param);
    if (value < min) {
        fprintf(stderr, "%s is %u%s, device only supports >= %u%s\n", param_name, value,
                param_unit, min, param_unit);
        is_within_bounds = 0;
    }

    max = pcm_params_get_max(params, param);
    if (value > max) {
        fprintf(stderr, "%s is %u%s, device only supports <= %u%s\n", param_name, value,
                param_unit, max, param_unit);
        is_within_bounds = 0;
    }

    return is_within_bounds;
}

int sample_is_playable(unsigned int card, unsigned int device, unsigned int channels,
                       unsigned int rate, unsigned int bits, unsigned int period_size,
                       unsigned int period_count)
{
    struct pcm_params *params;
    int can_play;

    params = pcm_params_get(card, device, PCM_OUT);
    if (params == NULL) {
        fprintf(stderr, "Unable to open PCM device %u.\n", device);
        return 0;
    }

    can_play = check_param(params, PCM_PARAM_RATE, rate, "Sample rate", "Hz");
    can_play &= check_param(params, PCM_PARAM_CHANNELS, channels, "Sample", " channels");
    can_play &= check_param(params, PCM_PARAM_SAMPLE_BITS, bits, "Bitrate", " bits");
    can_play &= check_param(params, PCM_PARAM_PERIOD_SIZE, period_size, "Period size", " frames");
    can_play &= check_param(params, PCM_PARAM_PERIODS, period_count, "Period count", " periods");

    pcm_params_free(params);

    return can_play;
}

void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels,
                 unsigned int rate, unsigned int bits, unsigned int period_size,
                 unsigned int period_count, uint32_t data_sz, unsigned int times)
{
    struct pcm_config config;
    struct pcm *pcm;
    char *buffer;
    unsigned int size, read_sz;
    int num_read;
    uint32_t data_sz_a = data_sz;

    memset(&config, 0, sizeof(config));
    config.channels = channels;
    config.rate = rate;
    config.period_size = period_size;
    config.period_count = period_count;
    if (bits == 32)
        config.format = PCM_FORMAT_S32_LE;
    else if (bits == 24)
        config.format = PCM_FORMAT_S24_3LE;
    else if (bits == 16) 
        config.format = PCM_FORMAT_S16_LE;
    config.start_threshold = 0;
    config.stop_threshold = 0;
    config.silence_threshold = 0;

    if (!sample_is_playable(card, device, channels, rate, bits, period_size, period_count)) {
        return;
    }

    pcm = pcm_open(card, device, PCM_OUT, &config);
    if (!pcm || !pcm_is_ready(pcm)) {
        fprintf(stderr, "Unable to open PCM device %u (%s)\n",
                device, pcm_get_error(pcm));
        return;
    }

    size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm));
    buffer = malloc(size);
    if (!buffer) {
        fprintf(stderr, "Unable to allocate %d bytes\n", size);
        free(buffer);
        pcm_close(pcm);
        return;
    }

    printf("Playing sample: %u ch, %u hz, %u bit %u bytes\n", channels, rate, bits, data_sz);

    /* catch ctrl-c to shutdown cleanly */
    signal(SIGINT, stream_close);

    if (times == 0) {
		while(!close) {
			do {
                read_sz = size < data_sz ? size : data_sz;
                num_read = fread(buffer, 1, read_sz, file);
                if (num_read > 0) {
                    if (pcm_write(pcm, buffer, num_read)) {
                        fprintf(stderr, "Error playing sample\n");
                        break;
                    }
                    data_sz -= num_read;
                }
            } while (!close && num_read > 0 && data_sz > 0);
	    data_sz = data_sz_a;
	    fseek(file, 0, SEEK_SET);
        } 
    } else {
        while(times > 0 && !close) { 
			do {
				read_sz = size < data_sz ? size : data_sz;
				num_read = fread(buffer, 1, read_sz, file);
				if (num_read > 0) {
					if (pcm_write(pcm, buffer, num_read)) {
						fprintf(stderr, "Error playing sample\n");
						break;
					}
					data_sz -= num_read;
				}
			} while (!close && num_read > 0 && data_sz > 0);
	    data_sz = data_sz_a;
	    fseek(file, 0, SEEK_SET);
	    times--;
        }
    }
    free(buffer);
    pcm_close(pcm);
}

void play_sample_c(FILE *file_p, unsigned int card_p, unsigned int device_p, 
				   unsigned int channels_p, unsigned int rate_p, unsigned int bits_p,
				   unsigned int period_size_p,unsigned int period_count_p,
				   uint32_t data_sz_p, char *filename)
{
    FILE *file;
    struct wav_header header;
    unsigned int card = 0;
    unsigned int device = 0;
    unsigned int channels = 1;
    unsigned int rate = 48000;
    unsigned int bits = 16;
    unsigned int frames;
    unsigned int period_size = 1024;
    unsigned int period_count = 4;
    unsigned int cap_time = 0;
    enum pcm_format format;
    
    header.riff_id = ID_RIFF;
    header.riff_sz = 0;
    header.riff_fmt = ID_WAVE;
    header.fmt_id = ID_FMT;
    header.fmt_sz = 16;
    header.audio_format = FORMAT_PCM;
    header.num_channels = channels;
    header.sample_rate = rate;

    file = fopen(filename, "wb");
    if (!file) {
        fprintf(stderr, "Unable to create file '%s'\n", filename);
        return ;
    }
    switch (bits) {
    case 32:
        format = PCM_FORMAT_S32_LE;
        break;
    case 24:
        format = PCM_FORMAT_S24_LE;
        break;
    case 16:
        format = PCM_FORMAT_S16_LE;
        break;
    default:
        fprintf(stderr, "%u bits is not supported.\n", bits);
        fclose(file);
        return ;
    }

    header.bits_per_sample = pcm_format_to_bits(format);
    header.byte_rate = (header.bits_per_sample / 8) * channels * rate;
    header.block_align = channels * (header.bits_per_sample / 8);
    header.data_id = ID_DATA;

    /* leave enough room for header */
    fseek(file, sizeof(struct wav_header), SEEK_SET);

    /* install signal handler and begin capturing */
    signal(SIGINT, sigint_handler);
    signal(SIGHUP, sigint_handler);
    signal(SIGTERM, sigint_handler);

    frames = capture_sample_c(file, card, device, header.num_channels,
                              header.sample_rate, format,
                              period_size, period_count, cap_time,
							  file_p, card_p, device_p, channels_p,
							  rate_p, bits_p, period_size_p, period_count_p,
							  data_sz_p);
    printf("Captured %u frames\n", frames);

    /* write header now all information is known */
    header.data_sz = frames * header.block_align;
    header.riff_sz = header.data_sz + sizeof(header) - 8;
    fseek(file, 0, SEEK_SET);
    fwrite(&header, sizeof(struct wav_header), 1, file);

    fclose(file);
    
}

unsigned int capture_sample_c(FILE *file, unsigned int card, unsigned int device,
                              unsigned int channels, unsigned int rate,
                              enum pcm_format format, unsigned int period_size,
                              unsigned int period_count, unsigned int cap_tim,
							  FILE *file_p, unsigned int card_p, unsigned int device_p, 
							  unsigned int channels_p,unsigned int rate_p,
							  unsigned int bits_p,unsigned int period_size_p,
							  unsigned int period_count_p,uint32_t data_sz_p)
{
    /*capture*/
    struct pcm_config config;
    struct pcm *pcm;
    char *buffer;
    unsigned int size;
    unsigned int bytes_read = 0;
    unsigned int frames = 0;

    memset(&config, 0, sizeof(config));
    config.channels = channels;
    config.rate = rate;
    config.period_size = period_size;
    config.period_count = period_count;
    config.format = format;
    config.start_threshold = 0;
    config.stop_threshold = 0;
    config.silence_threshold = 0;

    pcm = pcm_open(card, device, PCM_IN, &config);
    if (!pcm || !pcm_is_ready(pcm)) {
        fprintf(stderr, "Unable to open PCM device (%s)\n",
                pcm_get_error(pcm));
        return 0;
    }

    size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm));
    buffer = malloc(size);
    if (!buffer) {
        fprintf(stderr, "Unable to allocate %u bytes\n", size);
        free(buffer);
        pcm_close(pcm);
        return 0;
    }

    printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate,
           pcm_format_to_bits(format));

    /*play*/
    struct pcm_config config_p;
    struct pcm *pcm_p;
    char *buffer_p;
    unsigned int size_p, read_sz_p;
    int num_read_p;

    memset(&config_p, 0, sizeof(config_p));
    config_p.channels = channels_p;
    config_p.rate = rate_p;
    config_p.period_size = period_size_p;
    config_p.period_count = period_count_p;
    if (bits_p == 32)
        config_p.format = PCM_FORMAT_S32_LE;
    else if (bits_p == 24)
        config_p.format = PCM_FORMAT_S24_3LE;
    else if (bits_p == 16) 
        config_p.format = PCM_FORMAT_S16_LE;
    config_p.start_threshold = 0;
    config_p.stop_threshold = 0;
    config_p.silence_threshold = 0;

    if (!sample_is_playable(card_p, device_p, channels_p, rate_p, bits_p,
			    period_size_p, period_count_p)) {
        return -1;
    }

    pcm_p = pcm_open(card_p, device_p, PCM_OUT, &config_p);
    if (!pcm_p || !pcm_is_ready(pcm_p)) {
        fprintf(stderr, "Unable to open PCM device %u (%s)\n",
                device_p, pcm_get_error(pcm_p));
        return -1;
    }

    size_p = pcm_frames_to_bytes(pcm_p, pcm_get_buffer_size(pcm_p));
    buffer_p = malloc(size_p);
    if (!buffer_p) {
        fprintf(stderr, "Unable to allocate %d bytes\n", size_p);
        free(buffer_p);
        pcm_close(pcm_p);
        return -1;
    }

    printf("Playing sample: %u ch, %u hz, %u bit %u bytes\n", 
		    channels_p, rate_p, bits_p, data_sz_p);
    
    while (1) {
		/*play*/
		read_sz_p = size_p < data_sz_p ? size_p : data_sz_p;
		num_read_p = fread(buffer_p, 1, read_sz_p, file_p);
		if (num_read_p > 0) {
            if (pcm_write(pcm_p, buffer_p, num_read_p)) {
				fprintf(stderr, "Error playing sample\n");
                break;
            }
			data_sz_p -= num_read_p;
		}
		read_sz_p = size_p < data_sz_p ? size_p : data_sz_p;
		num_read_p = fread(buffer_p, 1, read_sz_p, file_p);
		if (num_read_p > 0) {
            if (pcm_write(pcm_p, buffer_p, num_read_p)) {
				fprintf(stderr, "Error playing sample\n");
                break;
            }
            data_sz_p -= num_read_p;
		}

		/*capture*/
		pcm_read(pcm, buffer, size); 
		if (fwrite(buffer, 1, size, file) != size) {
            fprintf(stderr,"Error capturing sample\n");
            break;
		}
        bytes_read += size;
		if (close || num_read_p <= 0 || data_sz_p <= 0) {
			break;
		}
    } 

    free(buffer_p);
    pcm_close(pcm_p);

    frames = pcm_bytes_to_frames(pcm, bytes_read);
    free(buffer);
    pcm_close(pcm);
    return frames;
}

void play_sample_s(FILE *file_p, unsigned int card_p, unsigned int device_p, 
				   unsigned int channels_p, unsigned int rate_p, unsigned int bits_p,
				   unsigned int period_size_p,unsigned int period_count_p,
				   uint32_t data_sz_p, char *filename)
{

    FILE *file;
    struct wav_header header;
    unsigned int frames;
    enum pcm_format format;
    
    header.riff_id = ID_RIFF;
    header.riff_sz = 0;
    header.riff_fmt = ID_WAVE;
    header.fmt_id = ID_FMT;
    header.fmt_sz = 16;
    header.audio_format = FORMAT_PCM;
    header.num_channels = channels_p;
    header.sample_rate = rate_p;

    file = fopen(filename, "wb");
    if (!file) {
        fprintf(stderr, "Unable to create file '%s'\n", filename);
        return ;  
    }

    switch (bits_p) {
    case 32:
        format = PCM_FORMAT_S32_LE;
        break;
    case 24:
        format = PCM_FORMAT_S24_LE;
        break;
    case 16:
        format = PCM_FORMAT_S16_LE;
        break;
    default:
        fprintf(stderr, "%u bits is not supported.\n", bits_p);
        fclose(file);
        return ;
    }

    header.bits_per_sample = pcm_format_to_bits(format);
    header.byte_rate = (header.bits_per_sample / 8) * channels_p * rate_p;
    header.block_align = channels_p * (header.bits_per_sample / 8);
    header.data_id = ID_DATA;

    /* leave enough room for header */
    fseek(file, sizeof(struct wav_header), SEEK_SET);

    /* install signal handler and begin capturing */
    signal(SIGINT, sigint_handler);
    signal(SIGHUP, sigint_handler);
    signal(SIGTERM, sigint_handler);

    frames = capture_sample_s(file, file_p, card_p, device_p, channels_p,
							  rate_p, bits_p, period_size_p, period_count_p,
							  data_sz_p);
    printf("Captured %u frames\n", frames);

    /* write header now all information is known */
    header.data_sz = frames * header.block_align;
    header.riff_sz = header.data_sz + sizeof(header) - 8;
    fseek(file, 0, SEEK_SET);
    fwrite(&header, sizeof(struct wav_header), 1, file);

    fclose(file);
    
}

unsigned int capture_sample_s(FILE *file, FILE *file_p, unsigned int card_p,
							  unsigned int device_p, unsigned int channels_p,
							  unsigned int rate_p, unsigned int bits_p,
							  unsigned int period_size_p, unsigned int period_count_p,
							  uint32_t data_sz_p)
{
    /*capture*/
    char *buffer;
    unsigned int size;
    unsigned int bytes_read = 0;
    unsigned int frames = 0;

    size = data_sz_p;

    printf("Capturing sample: %u ch, %u hz, %u bit\n", channels_p, rate_p,
           pcm_format_to_bits(PCM_FORMAT_S16_LE));

    /*play*/
    struct pcm_config config_p;
    struct pcm *pcm_p;
    char *buffer_p;
    unsigned int size_p, read_sz_p;
    int num_read_p;

    memset(&config_p, 0, sizeof(config_p));
    config_p.channels = channels_p;
    config_p.rate = rate_p;
    config_p.period_size = period_size_p;
    config_p.period_count = period_count_p;
    if (bits_p == 32)
        config_p.format = PCM_FORMAT_S32_LE;
    else if (bits_p == 24)
        config_p.format = PCM_FORMAT_S24_3LE;
    else if (bits_p == 16)
        config_p.format = PCM_FORMAT_S16_LE;
    config_p.start_threshold = 0;
    config_p.stop_threshold = 0;
    config_p.silence_threshold = 0;

    if (!sample_is_playable(card_p, device_p, channels_p, rate_p, bits_p,
			    period_size_p, period_count_p)) {
        return -1;
    }

    pcm_p = pcm_open(card_p, device_p, PCM_OUT, &config_p);
    if (!pcm_p || !pcm_is_ready(pcm_p)) {
        fprintf(stderr, "Unable to open PCM device %u (%s)\n",
                device_p, pcm_get_error(pcm_p));
        return -1;
    }

    size_p = pcm_frames_to_bytes(pcm_p, pcm_get_buffer_size(pcm_p));
    buffer_p = malloc(size_p);
    if (!buffer_p) {
        fprintf(stderr, "Unable to allocate %d bytes\n", size_p);
        free(buffer_p);
        pcm_close(pcm_p);
        return -1;
    }

    printf("Playing sample: %u ch, %u hz, %u bit %u bytes\n",
		    channels_p, rate_p, bits_p, data_sz_p);

    while (1) {
		read_sz_p = size_p < data_sz_p ? size_p : data_sz_p;
		num_read_p = fread(buffer_p, 1, read_sz_p, file_p);
		fwrite(buffer_p, 1, num_read_p, file);
		if (num_read_p > 0) {
            if (pcm_write(pcm_p, buffer_p, num_read_p)) {
				fprintf(stderr, "Error playing sample\n");
                break;
			}
        data_sz_p -= num_read_p;
	    bytes_read += size;
		}
		if (close || num_read_p <= 0 || data_sz_p <= 0)
			break;
    }

    free(buffer_p);
    pcm_close(pcm_p);

    frames = bytes_read / (channels_p * (pcm_format_to_bits(PCM_FORMAT_S16_LE) >> 3));
    return frames;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨天不打滑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值