Esempio n. 1
0
static ssize_t pcm_write(u_char *data, size_t count)
{
	ssize_t r;
	ssize_t result = 0;
	if (count < chunk_size) {
		snd_pcm_format_set_silence(hwparams.format, data + count * bits_per_frame / 8, (chunk_size - count) * hwparams.channels);
		count = chunk_size;
	}
	while (count > 0) {
	 
		r = writei_func(handle, data, count);
		 
		if (r == -EAGAIN || (r >= 0 && (size_t)r < count)) {
			snd_pcm_wait(handle, 1000);
		} else if (r == -EPIPE) {
			xrun();
		} else if (r == -ESTRPIPE) {
			suspend();
		} else if (r < 0) {
			error("write error: %s", snd_strerror(r));
			exit(EXIT_FAILURE);
		}
		if (r > 0) {
			if (vumeter)
				compute_max_peak(data, r * hwparams.channels);
			result += r;
			count -= r;
			data += r * bits_per_frame / 8;
		}
	}
	return result;
}
Esempio n. 2
0
static void playback(char *name)
{
	printf("name : %s\n",name);
	int fd;
	fd = open(name, O_RDONLY, 0);

	/* read the file header */
	WaveTotalInfo *waveTotalInfo=(WaveTotalInfo *)malloc(WAVE_HEAD_LENGTH);

	int a;
	a=read(fd, waveTotalInfo, WAVE_HEAD_LENGTH);

        printf("1. a= %d  WAVE_HEAD_LENGTH: %d\n",a,WAVE_HEAD_LENGTH);
	

	/* read data*/
	int rawDataSize=waveTotalInfo->waveDataStart.dataSize;
	uint8_t *rawData=(uint8_t *)malloc(rawDataSize);
	a=read(fd, rawData, rawDataSize);
	
	printf("2. a= %d  rawDataSize: %d\n",a,rawDataSize);
	sleep(1);


	int i;
#if 0
	printf("0000020 : ");
	for(i=0;i<rawDataSize;i++)
	{
		if(rawData[i]==0)
		{
			printf("0x00 ");
		}
		else
		{
			printf("%#02x ",rawData[i]);
		}
		if((i%16)==15)
		{
			printf("\n");
			printf("%06x0 : ",i/16+3);
		}
	}
#endif

#define CUT_PART 10
#define DATA_UNIT_NUMBER (16/8)*2 /*(16bit/8bit) * 2 channel*/
	set_params();
	for(i=0;i<CUT_PART;i++)
	{
                printf("%d\n",(rawDataSize/CUT_PART)*i);
		writei_func(handle, &rawData[(rawDataSize/CUT_PART)*i],rawDataSize/(CUT_PART*DATA_UNIT_NUMBER));
	}
}
Esempio n. 3
0
static size_t pcm_write_raw(alsa_param_t * alsa_param, u_char * data, size_t count)
{

    snd_pcm_sframes_t r;
    size_t result = 0;

    /*
        if (count < chunk_size) {
            snd_pcm_format_set_silence(hwparams.format, data + count * bits_per_frame / 8, (chunk_size - count) * hwparams.channels);
            count = chunk_size;
        }
    */
    while (count > 0) {
        r = writei_func(alsa_param->handle, data, count);

        if (r == -EINTR) {
            r = 0;
        }
        if (r == -ESTRPIPE) {
            while ((r = snd_pcm_resume(alsa_param->handle)) == -EAGAIN) {
                sleep(1);
            }
        }

        if (r < 0) {
            //printf("xun in\n");
            if ((r = snd_pcm_prepare(alsa_param->handle)) < 0) {
                return 0;
            }
        }

        if (r > 0) {
            result += r;
            count -= r;
            data += r * alsa_param->bits_per_frame / 8;
        }
    }
    return result;
}