00001 #include "audioconfig.h"
00002 #include "audiodevice.h"
00003 #include "audioesound.h"
00004 #include "playbackconfig.h"
00005 #include "preferences.h"
00006 #include "recordconfig.h"
00007
00008 #ifdef HAVE_ESOUND
00009 #include <esd.h>
00010 #include <string.h>
00011
00012 AudioESound::AudioESound(AudioDevice *device) : AudioLowLevel(device)
00013 {
00014 esd_in = esd_out = esd_duplex = 0;
00015 esd_in_fd = esd_out_fd = esd_duplex_fd = 0;
00016 }
00017
00018 AudioESound::~AudioESound()
00019 {
00020 }
00021
00022
00023 int AudioESound::get_bit_flag(int bits)
00024 {
00025 switch(bits)
00026 {
00027 case 8:
00028 return ESD_BITS8;
00029 break;
00030
00031 case 16:
00032 return ESD_BITS16;
00033 break;
00034
00035 case 24:
00036 return ESD_BITS16;
00037 break;
00038 }
00039 }
00040
00041
00042 int AudioESound::get_channels_flag(int channels)
00043 {
00044 switch(channels)
00045 {
00046 case 1:
00047 return ESD_MONO;
00048 break;
00049
00050 case 2:
00051 return ESD_STEREO;
00052 break;
00053
00054 default:
00055 return ESD_STEREO;
00056 break;
00057 }
00058 }
00059
00060 char* AudioESound::translate_device_string(char *server, int port)
00061 {
00062
00063 if(port > 0 && strlen(server))
00064 sprintf(device_string, "%s:%d", server, port);
00065 else
00066 sprintf(device_string, "");
00067 return device_string;
00068 }
00069
00070 int AudioESound::open_input()
00071 {
00072 esd_format_t format = ESD_STREAM | ESD_RECORD;
00073
00074 device->in_channels = 2;
00075 device->in_bits = 16;
00076
00077 format |= get_channels_flag(device->in_channels);
00078 format |= get_bit_flag(device->in_bits);
00079
00080 if((esd_in = esd_open_sound(translate_device_string(device->in_config->esound_in_server, device->in_config->esound_in_port))) <= 0)
00081 {
00082 fprintf(stderr, "AudioESound::open_input: open failed\n");
00083 return 1;
00084 }
00085 esd_in_fd = esd_record_stream_fallback(format, device->in_samplerate,
00086 translate_device_string(device->out_config->esound_out_server, device->out_config->esound_out_port),
00087 "Cinelerra");
00088 return 0;
00089 }
00090
00091 int AudioESound::open_output()
00092 {
00093 esd_format_t format = ESD_STREAM | ESD_PLAY;
00094
00095 device->out_channels = 2;
00096 device->out_bits = 16;
00097
00098 format |= get_channels_flag(device->out_channels);
00099 format |= get_bit_flag(device->out_bits);
00100
00101 if((esd_out = esd_open_sound(translate_device_string(device->out_config->esound_out_server, device->out_config->esound_out_port))) <= 0)
00102 {
00103 fprintf(stderr, "AudioESound::open_output: open failed\n");
00104 return 1;
00105 };
00106 esd_out_fd = esd_play_stream_fallback(format, device->out_samplerate,
00107 translate_device_string(device->out_config->esound_out_server, device->out_config->esound_out_port),
00108 "Cinelerra");
00109
00110 device->device_buffer = esd_get_latency(esd_out);
00111 device->device_buffer *= device->out_bits / 8 * device->out_channels;
00112 return 0;
00113 }
00114
00115
00116 int AudioESound::open_duplex()
00117 {
00118 device->duplex_channels = 2;
00119 device->duplex_bits = 16;
00120 open_input();
00121 open_output();
00122 return 0;
00123 }
00124
00125 int AudioESound::close_all()
00126 {
00127 if(device->r || device->d)
00128 {
00129 close(esd_in_fd);
00130 esd_close(esd_in);
00131 }
00132
00133 if(device->w || device->d)
00134 {
00135 close(esd_out_fd);
00136 esd_close(esd_out);
00137 }
00138 }
00139
00140
00141 int64_t AudioESound::device_position()
00142 {
00143 return -1;
00144 }
00145
00146 int AudioESound::read_buffer(char *buffer, int size)
00147 {
00148 if(esd_in_fd > 0)
00149 return read(esd_in_fd, buffer, size);
00150 else
00151 return 1;
00152 }
00153
00154 int AudioESound::write_buffer(char *buffer, int size)
00155 {
00156 if(esd_out_fd > 0)
00157 return write(esd_out_fd, buffer, size);
00158 else
00159 return 0;
00160 }
00161
00162
00163 int AudioESound::flush_device()
00164 {
00165 return 0;
00166 }
00167
00168
00169 int AudioESound::interrupt_playback()
00170 {
00171 return 0;
00172 }
00173
00174 #endif // HAVE_ESOUND