aboutsummaryrefslogtreecommitdiff
path: root/src/output/alib.c
blob: 635382d05398f85130b8895ead6711b500e32057 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
	alib: audio output for HP-UX using alib

	copyright ?-2006 by the mpg123 project - free software under the terms of the LGPL 2.1
	see COPYING and AUTHORS files in distribution or http://mpg123.org
	initially written by Erwan Ducroquet
	based on source code from HP (Audio SDK)
*/

/*
 *
 * for mpg123 :
 * hpux:
 *  $(MAKE) \
 *  CC=cc \
 *  LDFLAGS=-L/opt/audio/lib \
 *  AUDIO_LIB=-lAlib \
 *  OBJECTS=decode.o dct64.o \
 *  CFLAGS=-Ae +O3 -DREAL_IS_FLOAT -D_HPUX_SOURCE -DHPUX -I/opt/audio/include \
 *  mpg123
 */


/*
 * For the user :
 * If you launch mpg123 on a XTerm with sound capabilities, it's OK
 * Else, you have to set the environment variable "AUDIO" to the name of
 * an HP Xterm with sound card.
 */

/**************************************************************************/

#include "mpg123app.h"

#include <fcntl.h>

#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <errno.h>


#include <Alib.h>   /* /opt/audio/include */
#include <CUlib.h>  /* /opt/audio/include */

#include "debug.h"

/**************************************************************************/


/* FIXME: These globals should be moved into a structure */
static Audio *audioServer = (Audio *) NULL;
static struct protoent *tcpProtocolEntry;
static ATransID xid;

static void printAudioError(Audio *audio,char *message,int errorCode) {
	char    errorbuff[132];
	AGetErrorText(audio, errorCode, errorbuff, 131);
	error2("%s: %s", message, errorbuff);
}
static long myHandler(Audio *audio,AErrorEvent *err_event) {
	printAudioError( audio, "Audio error", err_event->error_code ); 
	/* we cannot just do random exists, that messes terminal up
	 need proper error propagation in that case for future, setting intflag or such */
	/* exit(1); */
}

/**************************************************************************/

/*
 * Set the fn element of ai
 * Use ao->rate and ao->channels
 * Doesn't set any volume
 */

/* return on error leaves stuff dirty here... */
static int open_alib(audio_output_t *ao)
{
	AudioAttributes Attribs;
	AudioAttrMask   AttribsMask;
	AGainEntry      gainEntry[4];
	SSPlayParams    playParams;
	SStream	  audioStream;
	AErrorHandler   prevHandler;
	char		  server[1];
	int		  i;
	long            status;
	
	if (audioServer) {
		error("openAudio: audio already open");
		return -1;
	}
	
	prevHandler = ASetErrorHandler(myHandler);
	
	server[0] = '\0';
	audioServer = AOpenAudio( server, NULL );
	if (audioServer==NULL) {
		error("Error: could not open audio\n");
		return -1;
	}
	
	ao->fn = socket( AF_INET, SOCK_STREAM, 0 );
	if(ao->fn<0) {
		error("Socket creation failed");
		return -1;
	}
	
	Attribs.type = ATSampled;
	Attribs.attr.sampled_attr.sampling_rate = ao->rate;
	Attribs.attr.sampled_attr.channels	  = ao->channels;
	Attribs.attr.sampled_attr.data_format	  = ADFLin16;
	AttribsMask = ASSamplingRateMask | ASChannelsMask  | ASDataFormatMask;
	
	gainEntry[0].gain = AUnityGain;
	gainEntry[0].u.o.out_ch  = AOCTMono;
	gainEntry[0].u.o.out_dst = AODTDefaultOutput;
	
	playParams.gain_matrix.type = AGMTOutput;  /* gain matrix */
	playParams.gain_matrix.num_entries = 1;
	playParams.gain_matrix.gain_entries = gainEntry;
	playParams.play_volume = AUnityGain;       /* play volume */
	playParams.priority = APriorityNormal;     /* normal priority */
	playParams.event_mask = 0;                 /* don't solicit any events */
	
	xid=APlaySStream(audioServer,AttribsMask,&Attribs,
	&playParams,&audioStream,NULL);
	
	status=connect(ao->fn,
	(struct sockaddr *) &audioStream.tcp_sockaddr,
	sizeof(struct sockaddr_in) );
	if (status<0) {
		error("Connect failed");
		return -1;
	}
	
	i=-1;
	tcpProtocolEntry=getprotobyname("tcp");
	setsockopt(ao->fn,tcpProtocolEntry->p_proto,TCP_NODELAY,&i,sizeof(i));
	
	return ao->fn;
}

/**************************************************************************/

static int close_alib(audio_output_t *ao)
{
	close(ao->fn);
	ASetCloseDownMode( audioServer, AKeepTransactions, NULL );
	ACloseAudio( audioServer, NULL );
	audioServer = (Audio *) NULL;
	return 0;
}

/**************************************************************************/

/*
 * very simple
 * deserv to be inline
 */

static int write_alib(audio_output_t *ao,unsigned char *buf,int len)
{
	return write(ao->fn,buf,len*2);
}

/**************************************************************************/

static int get_formats_alib(audio_output_t *ao)
{
	return MPG123_ENC_SIGNED_16;
}

static void flush_alib(audio_output_t *ao)
{
}


static int init_alib(audio_output_t* ao)
{
	if (ao==NULL) return -1;

	/* Set callbacks */
	ao->open = open_alib;
	ao->flush = flush_alib;
	ao->write = write_alib;
	ao->get_formats = get_formats_alib;
	ao->close = close_alib;

	/* Success */
	return 0;
}



/* 
	Module information data structure
*/
mpg123_module_t mpg123_output_module_info = {
	/* api_version */	MPG123_MODULE_API_VERSION,
	/* name */			"alib",						
	/* description */	"Output audio HP-UX using alib.",
	/* revision */		"$Rev:$",						
	/* handle */		NULL,
	
	/* init_output */	init_alib,						
};