All pastes #383292 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-03-06 07:00 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#include <sndfile.h>
#include "ringbuffer.h"
	      
int main (int argc, char *argv[])
{
	int i;
	int x;
	int err;
	unsigned int rate = 48000;
	short buf[128];
	snd_pcm_t *capture_handle = NULL;
	snd_pcm_hw_params_t *hw_params;
	FILE *outputFile = NULL;
	char *device = NULL;
	
	if (argc > 1) {
		device = argv[1];
	}else{
		device = "default";
	}

	if ((err = snd_pcm_open (&capture_handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
		fprintf (stderr, "cannot open audio device %s (%s)\n", device, snd_strerror (err));
		exit (1);
	}
		   
	if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
		fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err));
		exit (1);
	}
				 
	if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
		fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror (err));
		exit (1);
	}
	
	if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
		fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err));
		exit (1);
	}
	
	if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
		fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err));
		exit (1);
	}
	
	if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, 0)) < 0) {
		fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err));
		exit (1);
	}
	
	if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) {
		fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err));
		exit (1);
	}
	
	if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
		fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err));
		exit (1);
	}
	
	snd_pcm_hw_params_free (hw_params);
	
	if ((err = snd_pcm_prepare (capture_handle)) < 0) {
		fprintf (stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror (err));
		exit (1);
	}

/*	SNDFILE *sf_open ("./output.raw", SFM_WRITE, SF_INFO *sfinfo); */

	if ((outputFile = fopen("./output.raw", "w")) == NULL) {
		printf("Cannot open file.\n");
		exit(1);
	}

	for (i = 0; i < 750; ++i) {
		if ((err = snd_pcm_readi (capture_handle, buf, 128)) != 128) {
			fprintf (stderr, "read from audio interface failed (%s)\n", snd_strerror (err));
			exit (1);
		}
		fwrite(buf, 2, 128, outputFile); 
		printf("%d\n", sizeof(buf)); printf("%d\n", sizeof(buf) * 128); 
/*		for (x = 0; x < 128; x++) {
			fprintf(outputFile, "%hd", buf[x]);
		}*/
	}
	fclose(outputFile); 

	snd_pcm_close (capture_handle);
	return EXIT_SUCCESS;
}