r870: Merge 2.1:
[cinelerra_cv.git] / cinelerra / audioesound.C
blob29388567c74d86cafadb70f68de7a5dd83dbdeb4
1 #include "audioconfig.h"
2 #include "audiodevice.h"
3 #include "audioesound.h"
4 #include "playbackconfig.h"
5 #include "preferences.h"
6 #include "recordconfig.h"
8 #ifdef HAVE_ESOUND
9 #include <esd.h>
10 #include <string.h>
12 AudioESound::AudioESound(AudioDevice *device) : AudioLowLevel(device)
14         esd_in = esd_out = esd_duplex = 0;
15         esd_in_fd = esd_out_fd = esd_duplex_fd = 0;
18 AudioESound::~AudioESound()
23 int AudioESound::get_bit_flag(int bits)
25         switch(bits)
26         {
27                 case 8:
28                         return ESD_BITS8;
29                         break;
30                 
31                 case 16:
32                         return ESD_BITS16;
33                         break;
34                 
35                 case 24:
36                         return ESD_BITS16;
37                         break;
38         }
41 // No more than 2 channels in ESD
42 int AudioESound::get_channels_flag(int channels)
44         switch(channels)
45         {
46                 case 1:
47                         return ESD_MONO;
48                         break;
49                 
50                 case 2:
51                         return ESD_STEREO;
52                         break;
53                 
54                 default:
55                         return ESD_STEREO;
56                         break;
57         }
60 char* AudioESound::translate_device_string(char *server, int port)
62 // ESD server
63         if(port > 0 && strlen(server))
64                 sprintf(device_string, "%s:%d", server, port);
65         else
66                 sprintf(device_string, "");
67         return device_string;
70 int AudioESound::open_input()
72         esd_format_t format = ESD_STREAM | ESD_RECORD;
73         
74         device->in_channels = 2;
75         device->in_bits = 16;
77         format |= get_channels_flag(device->in_channels);
78         format |= get_bit_flag(device->in_bits);
80         if((esd_in = esd_open_sound(translate_device_string(device->in_config->esound_in_server, device->in_config->esound_in_port))) <= 0)
81         {
82                 fprintf(stderr, "AudioESound::open_input: open failed\n");
83                 return 1;
84         }
85         esd_in_fd = esd_record_stream_fallback(format, device->in_samplerate, 
86                                 translate_device_string(device->out_config->esound_out_server, device->out_config->esound_out_port), 
87                                                 "Cinelerra");
88         return 0;
91 int AudioESound::open_output()
93         esd_format_t format = ESD_STREAM | ESD_PLAY;
95         device->out_channels = 2;
96         device->out_bits = 16;
98         format |= get_channels_flag(device->out_channels);
99         format |= get_bit_flag(device->out_bits);
101         if((esd_out = esd_open_sound(translate_device_string(device->out_config->esound_out_server, device->out_config->esound_out_port))) <= 0)
102         {
103                 fprintf(stderr, "AudioESound::open_output: open failed\n");
104                 return 1;
105         };
106         esd_out_fd = esd_play_stream_fallback(format, device->out_samplerate, 
107                                 translate_device_string(device->out_config->esound_out_server, device->out_config->esound_out_port), 
108                                                 "Cinelerra");
110         device->device_buffer = esd_get_latency(esd_out);
111         device->device_buffer *= device->out_bits / 8 * device->out_channels;
112         return 0;
115 // Open both input and output in ESD
116 int AudioESound::open_duplex()
118         device->duplex_channels = 2;
119         device->duplex_bits = 16;
120         open_input();
121         open_output();
122         return 0;
125 int AudioESound::close_all()
127         if(device->r || device->d)
128         { 
129         close(esd_in_fd);
130                 esd_close(esd_in);      
131         }
133         if(device->w || device->d)
134         {
135                 close(esd_out_fd);
136                 esd_close(esd_out);     
137         }
140 // No position on ESD
141 int64_t AudioESound::device_position()
143         return -1;
146 int AudioESound::read_buffer(char *buffer, int size)
148         if(esd_in_fd > 0)
149                 return read(esd_in_fd, buffer, size);
150         else
151                 return 1;
154 int AudioESound::write_buffer(char *buffer, int size)
156         if(esd_out_fd > 0)
157                 return write(esd_out_fd, buffer, size);
158         else
159                 return 0;
162 // No flushing in ESD
163 int AudioESound::flush_device()
165         return 0;
168 // No interrupting ESD
169 int AudioESound::interrupt_playback()
171         return 0;
174 #endif // HAVE_ESOUND