4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "audioconfig.h"
23 #include "audiodevice.h"
24 #include "audioesound.h"
25 #include "playbackconfig.h"
26 #include "preferences.h"
27 #include "recordconfig.h"
33 AudioESound::AudioESound(AudioDevice *device) : AudioLowLevel(device)
36 esd_in_fd = esd_out_fd = 0;
39 AudioESound::~AudioESound()
44 int AudioESound::get_bit_flag(int bits)
48 case 8: return ESD_BITS8;
49 case 16: return ESD_BITS16;
54 // No more than 2 channels in ESD
55 int AudioESound::get_channels_flag(int channels)
59 case 1: return ESD_MONO;
60 case 2: return ESD_STEREO;
65 char* AudioESound::translate_device_string(char *server, int port)
69 if(port > 0 && strlen(server))
70 sprintf(device_string, "%s:%d", server, port);
74 int AudioESound::open_input()
76 esd_format_t format = ESD_STREAM | ESD_RECORD;
78 device->in_channels = 2;
81 format |= get_channels_flag(device->in_channels);
82 format |= get_bit_flag(device->in_bits);
84 if((esd_in = esd_open_sound(translate_device_string(device->in_config->esound_in_server, device->in_config->esound_in_port))) <= 0)
86 fprintf(stderr, "AudioESound::open_input: open failed\n");
89 esd_in_fd = esd_record_stream_fallback(format, device->in_samplerate,
90 translate_device_string(device->out_config->esound_out_server, device->out_config->esound_out_port),
95 int AudioESound::open_output()
97 esd_format_t format = ESD_STREAM | ESD_PLAY;
99 device->out_channels = 2;
100 device->out_bits = 16;
102 format |= get_channels_flag(device->out_channels);
103 format |= get_bit_flag(device->out_bits);
105 if((esd_out = esd_open_sound(translate_device_string(
106 device->out_config->esound_out_server,
107 device->out_config->esound_out_port))) <= 0)
109 fprintf(stderr, "AudioESound::open_output %s:%d: open failed\n",
110 device->out_config->esound_out_server,
111 device->out_config->esound_out_port);
115 esd_out_fd = esd_play_stream_fallback(format,
116 device->out_samplerate,
117 translate_device_string(device->out_config->esound_out_server,
118 device->out_config->esound_out_port),
121 device->device_buffer = esd_get_latency(esd_out);
122 device->device_buffer *= device->out_bits / 8 * device->out_channels;
126 int AudioESound::close_all()
142 // No position on ESD
143 int64_t AudioESound::device_position()
148 int AudioESound::read_buffer(char *buffer, int size)
150 return esd_in_fd>0 ? read(esd_in_fd, buffer, size) : 1;
153 int AudioESound::write_buffer(char *buffer, int size)
155 return esd_out_fd>0 && write(esd_out_fd, buffer, size) > 0 ? 0 : -1;
158 // No flushing in ESD
159 int AudioESound::flush_device()
164 // No interrupting ESD
165 int AudioESound::interrupt_playback()
170 #endif // HAVE_ESOUND