rework alsa device scan, fix lang for pactl
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / audio1394.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_FIREWIRE
23 #include "audio1394.h"
24 #include "playbackconfig.h"
25 #include "device1394input.h"
26 #include "device1394output.h"
27 #include "iec61883input.h"
28 #include "iec61883output.h"
29 #include "preferences.h"
30 #include "recordconfig.h"
31 #include "videoconfig.h"
32 #include "videodevice.h"
33
34 #define SAMPLES_PER_FRAME 2048
35
36 Audio1394::Audio1394(AudioDevice *device)
37  : AudioLowLevel(device)
38 {
39         initialize();
40 }
41
42
43 Audio1394::~Audio1394()
44 {
45         close_all();
46 }
47
48 void Audio1394::initialize()
49 {
50         input_thread = 0;
51         output_thread = 0;
52         input_iec = 0;
53         output_iec = 0;
54 }
55
56 int Audio1394::open_input()
57 {
58         int result = 0;
59         if(!input_thread && !input_iec)
60         {
61 // Lock the channels for the DV format
62                 device->in_channels = 2;
63                 device->in_bits = 16;
64                 bytes_per_sample = device->in_channels * device->in_bits / 8;
65
66
67                 if(device->idriver == AUDIO_DV1394 ||
68                         device->idriver == AUDIO_1394)
69                 {
70                         input_thread = new Device1394Input;
71                         result = input_thread->open(device->in_config->firewire_path,
72                                 device->in_config->firewire_port,
73                                 device->in_config->firewire_channel,
74                                 30,
75                                 device->in_channels,
76                                 device->in_samplerate,
77                                 device->in_bits,
78                                 device->vconfig->w,
79                                 device->vconfig->h);
80                 }
81                 else
82                 {
83                         input_iec = new IEC61883Input;
84                         result = input_iec->open(device->in_config->firewire_port,
85                                 device->in_config->firewire_channel,
86                                 30,
87                                 device->in_channels,
88                                 device->in_samplerate,
89                                 device->in_bits,
90                                 device->vconfig->w,
91                                 device->vconfig->h);
92                 }
93
94
95
96
97                 if(result)
98                 {
99                         delete input_thread;
100                         input_thread = 0;
101                         delete input_iec;
102                         input_iec = 0;
103                 }
104         }
105
106         return result;
107 }
108
109 int Audio1394::open_output()
110 {
111         if(!output_thread && !output_iec)
112         {
113 // Lock the channels for the DV format
114                 device->out_channels = 2;
115                 device->out_bits = 16;
116                 bytes_per_sample = device->out_channels * device->out_bits / 8;
117
118
119                 if(device->odriver == AUDIO_DV1394)
120                 {
121                         output_thread = new Device1394Output(device);
122                         output_thread->open(device->out_config->dv1394_path,
123                                 device->out_config->dv1394_port,
124                                 device->out_config->dv1394_channel,
125                                 30,
126                                 device->out_channels,
127                                 device->out_bits,
128                                 device->out_samplerate,
129                                 device->out_config->dv1394_syt);
130                 }
131                 else
132                 if(device->odriver == AUDIO_1394)
133                 {
134                         output_thread = new Device1394Output(device);
135                         output_thread->open(device->out_config->firewire_path,
136                                 device->out_config->firewire_port,
137                                 device->out_config->firewire_channel,
138                                 30,
139                                 device->out_channels,
140                                 device->out_bits,
141                                 device->out_samplerate,
142                                 device->out_config->firewire_syt);
143                 }
144                 else
145                 {
146                         output_iec = new IEC61883Output(device);
147                         output_iec->open(device->out_config->firewire_port,
148                                 device->out_config->firewire_channel,
149                                 30,
150                                 device->out_channels,
151                                 device->out_bits,
152                                 device->out_samplerate,
153                                 device->out_config->firewire_syt);
154                 }
155         }
156         return 0;
157 }
158
159 int Audio1394::close_all()
160 {
161         if(input_thread)
162         {
163                 delete input_thread;
164         }
165
166         if(output_thread)
167         {
168                 delete output_thread;
169         }
170         delete input_iec;
171         delete output_iec;
172
173         initialize();
174         return 0;
175 }
176
177
178 int Audio1394::read_buffer(char *buffer, int bytes)
179 {
180         if(input_thread)
181         {
182                 input_thread->read_audio(buffer, bytes / bytes_per_sample);
183         }
184         else
185         if(input_iec)
186         {
187                 input_iec->read_audio(buffer, bytes / bytes_per_sample);
188         }
189
190         return 0;
191 }
192
193 int Audio1394::write_buffer(char *buffer, int bytes)
194 {
195         if(output_thread)
196                 output_thread->write_samples(buffer, bytes / bytes_per_sample);
197         else
198         if(output_iec)
199                 output_iec->write_samples(buffer, bytes / bytes_per_sample);
200         return 0;
201 }
202
203 int64_t Audio1394::device_position()
204 {
205         if(output_thread)
206                 return output_thread->get_audio_position();
207         else
208         if(output_iec)
209                 return output_iec->get_audio_position();
210         else
211                 return 0;
212 }
213
214
215 int Audio1394::flush_device()
216 {
217         if(output_thread)
218                 output_thread->flush();
219         else
220         if(output_iec)
221                 output_iec->flush();
222         return 0;
223 }
224
225 int Audio1394::interrupt_playback()
226 {
227         if(output_thread)
228                 output_thread->interrupt();
229         else
230         if(output_iec)
231                 output_iec->interrupt();
232         return 0;
233 }
234
235 #endif