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