~ubuntu-branches/ubuntu/lucid/mpg123/lucid

« back to all changes in this revision

Viewing changes to src/audio_alib.c

Tags: upstream-0.60
ImportĀ upstreamĀ versionĀ 0.60

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
        audio_alib.c: audio output for HP-UX using alib
 
3
 
 
4
        copyright ?-2006 by the mpg123 project - free software under the terms of the LGPL 2.1
 
5
        see COPYING and AUTHORS files in distribution or http://mpg123.de
 
6
        initially written by Erwan Ducroquet
 
7
        based on source code from HP (Audio SDK)
 
8
*/
 
9
 
 
10
/*
 
11
 *
 
12
 * for mpg123 :
 
13
 * hpux:
 
14
 *  $(MAKE) \
 
15
 *  CC=cc \
 
16
 *  LDFLAGS=-L/opt/audio/lib \
 
17
 *  AUDIO_LIB=-lAlib \
 
18
 *  OBJECTS=decode.o dct64.o \
 
19
 *  CFLAGS=-Ae +O3 -DREAL_IS_FLOAT -D_HPUX_SOURCE -DHPUX -I/opt/audio/include \
 
20
 *  mpg123
 
21
 */
 
22
 
 
23
/*
 
24
 * Rem:
 
25
 * I don't use the "set_rate" and "set_channels" functions
 
26
 * these are set directly in the "audio_open" function
 
27
 * 
 
28
 */
 
29
 
 
30
/*
 
31
 * For the user :
 
32
 * If you launch mpg123 on a XTerm with sound capabilities, it's OK
 
33
 * Else, you have to set the environment variable "AUDIO" to the name of
 
34
 * an HP Xterm with sound card.
 
35
 */
 
36
 
 
37
/**************************************************************************/
 
38
 
 
39
#include <sys/types.h>
 
40
#include <stdio.h>
 
41
#include <unistd.h>
 
42
#include <fcntl.h>
 
43
 
 
44
#include <sys/socket.h>
 
45
#include <netdb.h>
 
46
#include <netinet/in.h>
 
47
#include <netinet/tcp.h>
 
48
#include <errno.h>
 
49
 
 
50
#include "config.h"
 
51
#include "mpg123.h"
 
52
 
 
53
#include "Alib.h"               /* /opt/audio/include */
 
54
#include "CUlib.h"              /* /opt/audio/include */
 
55
 
 
56
/*
 
57
 * 
 
58
 * used :
 
59
 * 
 
60
 * int audio_open(struct audio_info_struct *ai);
 
61
 * int audio_play_samples(struct audio_info_struct *ai,unsigned char *buf,int len);
 
62
 * int audio_close(struct audio_info_struct *ai);
 
63
 * 
 
64
 * unused :
 
65
 * 
 
66
 * int audio_set_rate(struct audio_info_struct *ai);
 
67
 * int audio_set_channels(struct audio_info_struct *ai);
 
68
 * 
 
69
 */
 
70
 
 
71
/**************************************************************************/
 
72
 
 
73
/*
 
74
 * Some things used only for HP Audio
 
75
 */
 
76
 
 
77
static Audio *audioServer = (Audio *) NULL;
 
78
static struct protoent *tcpProtocolEntry;
 
79
static ATransID xid;
 
80
 
 
81
static void printAudioError(Audio *audio,char *message,int errorCode) {
 
82
    char    errorbuff[132];
 
83
    AGetErrorText(audio, errorCode, errorbuff, 131);
 
84
    fprintf ( stderr, "%s: %s\n", message, errorbuff);
 
85
}
 
86
static long myHandler(Audio *audio,AErrorEvent *err_event) {
 
87
  printAudioError( audio, "Audio error", err_event->error_code ); 
 
88
  exit(1);
 
89
}
 
90
 
 
91
/**************************************************************************/
 
92
 
 
93
/*
 
94
 * Set the fn element of ai
 
95
 * Use ai->rate and ai->channels
 
96
 * Doesn't set any volume
 
97
 */
 
98
 
 
99
int audio_open(struct audio_info_struct *ai) {
 
100
  AudioAttributes Attribs;
 
101
  AudioAttrMask   AttribsMask;
 
102
  AGainEntry      gainEntry[4];
 
103
  SSPlayParams    playParams;
 
104
  SStream         audioStream;
 
105
  AErrorHandler   prevHandler;
 
106
  char            server[1];
 
107
  int             i;
 
108
  long            status;
 
109
 
 
110
  if(audioServer)
 
111
    {fprintf(stderr,"openAudio: audio already open\n");exit(1);}
 
112
 
 
113
  prevHandler = ASetErrorHandler(myHandler);
 
114
 
 
115
  server[0] = '\0';
 
116
  audioServer = AOpenAudio( server, NULL );
 
117
  if(audioServer==NULL)
 
118
    {fprintf(stderr,"Error: could not open audio\n");exit(1);}
 
119
 
 
120
  ai->fn = socket( AF_INET, SOCK_STREAM, 0 );
 
121
  if(ai->fn<0)
 
122
    {fprintf(stderr,"Socket creation failed");exit(1);}
 
123
 
 
124
  Attribs.type = ATSampled;
 
125
  Attribs.attr.sampled_attr.sampling_rate = ai->rate;
 
126
  Attribs.attr.sampled_attr.channels      = ai->channels;
 
127
  Attribs.attr.sampled_attr.data_format   = ADFLin16;
 
128
  AttribsMask = ASSamplingRateMask | ASChannelsMask  | ASDataFormatMask;
 
129
 
 
130
  gainEntry[0].gain = AUnityGain;
 
131
  gainEntry[0].u.o.out_ch  = AOCTMono;
 
132
  gainEntry[0].u.o.out_dst = AODTDefaultOutput;
 
133
 
 
134
  playParams.gain_matrix.type = AGMTOutput;  /* gain matrix */
 
135
  playParams.gain_matrix.num_entries = 1;
 
136
  playParams.gain_matrix.gain_entries = gainEntry;
 
137
  playParams.play_volume = AUnityGain;       /* play volume */
 
138
  playParams.priority = APriorityNormal;     /* normal priority */
 
139
  playParams.event_mask = 0;                 /* don't solicit any events */
 
140
 
 
141
  xid=APlaySStream(audioServer,AttribsMask,&Attribs,
 
142
                   &playParams,&audioStream,NULL);
 
143
 
 
144
  status=connect(ai->fn,
 
145
                 (struct sockaddr *) &audioStream.tcp_sockaddr,
 
146
                 sizeof(struct sockaddr_in) );
 
147
  if(status<0){fprintf(stderr,"Connect failed");exit(1);}
 
148
 
 
149
  i=-1;
 
150
  tcpProtocolEntry=getprotobyname("tcp");
 
151
  setsockopt(ai->fn,tcpProtocolEntry->p_proto,TCP_NODELAY,&i,sizeof(i));
 
152
 
 
153
  return ai->fn;
 
154
}
 
155
 
 
156
/**************************************************************************/
 
157
 
 
158
int audio_close(struct audio_info_struct *ai)
 
159
{
 
160
  close(ai->fn);
 
161
  ASetCloseDownMode( audioServer, AKeepTransactions, NULL );
 
162
  ACloseAudio( audioServer, NULL );
 
163
  audioServer = (Audio *) NULL;
 
164
  return 0;
 
165
}
 
166
 
 
167
/**************************************************************************/
 
168
 
 
169
/*
 
170
 * very simple
 
171
 * deserv to be inline
 
172
 */
 
173
 
 
174
inline int audio_play_samples(struct audio_info_struct *ai,unsigned char *buf,int len)
 
175
{
 
176
  return write(ai->fn,buf,len*2);
 
177
}
 
178
 
 
179
/**************************************************************************/
 
180
 
 
181
int audio_get_formats(struct audio_info_struct *ai)
 
182
{
 
183
  return AUDIO_FORMAT_SIGNED_16;
 
184
}
 
185
 
 
186
void audio_queueflush(struct audio_info_struct *ai)
 
187
{
 
188
}
 
189
 
 
190
 
 
191
/**************************************************************************
 
192
 * T H A T ' S    A L L   F O L K S
 
193
 **************************************************************************/