File: pulseaudio.c

package info (click to toggle)
qrq 0.3.0-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 464 kB
  • sloc: ansic: 1,849; makefile: 173; perl: 123; sh: 2
file content (80 lines) | stat: -rw-r--r-- 1,980 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* 
Copyright (C) 2011  Fabian Kurz, DJ1YFK
 
$Id: pulseaudio.c 388 2011-12-18 21:28:50Z dj1yfk $

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA  02110-1301, USA.

PulseAudio specific functions and includes.

*/

#include <ncurses.h>
#include <stdlib.h>
#include <fcntl.h>
#include <pulse/simple.h>
#include <pulse/error.h>

extern long samplerate;
extern void  *dsp_fd;

short int buf[441000];		/* 10 secs buffer */
int bufpos = 0;

void *open_dsp () {
	static int opened = 0;

	/* with PA we only open the device once and then leave it
	   opened */
	if (opened) {
		return dsp_fd;
	}

	/* The Sample format to use */
	static const pa_sample_spec ss = {
		.format = PA_SAMPLE_S16LE,
		.rate = 44100,
		.channels = 1
	};
	pa_simple *s = NULL;
	int error;

	if (!(s = pa_simple_new(NULL, "qrq", PA_STREAM_PLAYBACK, NULL, 
				"playback", &ss, NULL, NULL, &error))) {
	        fprintf(stderr, "pa_simple_new() failed: %s\n", 
				pa_strerror(error));
	}

	opened = 1;
	return s;
}

/* actually just puts samples into the buffer that is played at the end 
(close_audio) */
void write_audio (void *bla, int *in, int size) {
	int i = 0;
	for (i=0; i < size/sizeof(int); i++) {
		buf[bufpos] = (short int) in[i];
		bufpos++;
	}	
}

void close_audio (void *s) {
	int e;
	pa_simple_write(s, buf, bufpos*sizeof(short int), &e);
	pa_simple_drain(s, &e);
	bufpos = 0;
}