1 /*
2  *  Copyright (C) 2009-2014  Christian Heckendorf <heckendorfc@gmail.com>
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "pulseutil.h"
19 #include "sndutil.h"
20 
snd_init(struct playerHandles * ph)21 int snd_init(struct playerHandles *ph){
22 	int error;
23 
24 	ph->ss.format=PA_SAMPLE_S16LE;
25 	ph->ss.channels=2;
26 	ph->ss.rate=44100;
27 
28 	ph->sndfd=pa_simple_new(NULL, "HARP", PA_STREAM_PLAYBACK, ph->device?ph->device:NULL, "music", &ph->ss, NULL, NULL, &error);
29 	if(!ph->sndfd){
30 		fprintf(stderr,"sndfd open failed: %d\n",error);
31 		return 1;
32 	}
33 	return 0;
34 }
35 
snd_param_init(struct playerHandles * ph,int * enc,int * channels,unsigned int * rate)36 int snd_param_init(struct playerHandles *ph, int *enc, int *channels, unsigned int *rate){
37 	int error;
38     struct pa_sample_spec ss;
39 
40 	if(ph->ss.channels==*channels && ph->ss.rate==*rate) // Already configured properly
41 		return 0;
42 
43     ph->ss.channels = *channels;
44     ph->ss.rate = *rate;
45 
46 	if(ph->sndfd)
47 		pa_simple_free(ph->sndfd);
48 
49 	/*
50 	if(pa_simple_drain(ph->sndfd,&error)<0){
51 		fprintf(stderr,"drain failed: %d\n",error);
52 		return 1;
53 	}
54 	*/
55 	ph->sndfd=pa_simple_new(NULL, "HARP", PA_STREAM_PLAYBACK, NULL, "music", &ph->ss, NULL, NULL, &error);
56 	if(!ph->sndfd){
57 		fprintf(stderr,"sndfd open failed: %d\n",error);
58 		return 1;
59 	}
60 
61 	return 0;
62 }
63 
changeVolume(struct playerHandles * ph,int mod)64 void changeVolume(struct playerHandles *ph, int mod){
65 }
66 
toggleMute(struct playerHandles * ph,int * mute)67 void toggleMute(struct playerHandles *ph, int *mute){
68 }
69 
snd_clear(struct playerHandles * ph)70 void snd_clear(struct playerHandles *ph){
71 	int error;
72 	if(pa_simple_flush(ph->sndfd,&error)<0)
73 		fprintf(stderr,"Failed to flush audio\n");
74 }
75 
writei_snd(struct playerHandles * ph,const char * out,const unsigned int size)76 int writei_snd(struct playerHandles *ph, const char *out, const unsigned int size){
77 	int ret;
78 	int error;
79 	if(ph->pflag->pause){
80 		snd_clear(ph);
81 		do{
82 			usleep(100000); // 0.1 seconds
83 		}
84 		while(ph->pflag->pause);
85 	}
86 	if(size==0){
87 		return 0;
88 	}
89 	ret=pa_simple_write(ph->sndfd,out,(size_t)size,&error);
90 	if(ret<0)
91 		fprintf(stderr,"Failed to write audio: %d\n",error);
92 	return 0;
93 }
94 
snd_close(struct playerHandles * ph)95 void snd_close(struct playerHandles *ph){
96 	pa_simple_free(ph->sndfd);
97 }
98