enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / libs / ardour / ltc_file_reader.cc
1 /*
2     Copyright (C) 2015 Robin Gareus <robin@gareus.org>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <fcntl.h>
20 #include <sys/stat.h>
21
22 #include <glib.h>
23 #include "pbd/gstdio_compat.h"
24
25 #include <assert.h>
26 #include <string.h>
27 #include <glibmm.h>
28
29 #include "pbd/error.h"
30 #include "pbd/failed_constructor.h"
31
32 #include "timecode/time.h"
33 #include "ardour/ltc_file_reader.h"
34
35 #include "pbd/i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
40 using std::string;
41
42 #define BUFFER_SIZE 1024 // audio chunk size
43
44 LTCFileReader::LTCFileReader (std::string path, double expected_fps, LTC_TV_STANDARD tv_standard)
45         : _path (path)
46         , _expected_fps (expected_fps)
47         , _ltc_tv_standard (tv_standard)
48         , _sndfile (0)
49         , _interleaved_audio_buffer (0)
50         , _frames_decoded (0)
51         , _samples_read (0)
52 {
53         memset (&_info, 0, sizeof (_info));
54         assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
55
56         if (open ()) {
57                 throw failed_constructor ();
58         }
59
60         const int apv = rintf (_info.samplerate / _expected_fps);
61         decoder = ltc_decoder_create (apv, 8); // must be able to hold frmes for BUFFER_SIZE
62
63 #if 0 // TODO allow to auto-detect
64         if (expected_fps == 25.0) {
65                 _ltc_tv_standard = LTC_TV_625_50;
66         }
67         else if (expected_fps == 30.0) {
68                 _ltc_tv_standard = LTC_TV_525_60;
69         }
70         else {
71                 _ltc_tv_standard = LTC_TV_FILM_24;
72         }
73 #endif
74 }
75
76 LTCFileReader::~LTCFileReader ()
77 {
78         close ();
79         ltc_decoder_free (decoder);
80         free (_interleaved_audio_buffer);
81 }
82
83 int
84 LTCFileReader::open ()
85 {
86         if (_sndfile) {
87                 return 0;
88         }
89
90 #ifdef PLATFORM_WINDOWS
91         int fd = g_open (_path.c_str (), O_RDONLY, 0444);
92 #else
93         int fd = ::open (_path.c_str (), O_RDONLY, 0444);
94 #endif
95         if (fd == -1) {
96                 error << string_compose (_("LTCFileReader: cannot open file \"%1\""), _path) << endmsg;
97                 return -1;
98         }
99
100         _sndfile = sf_open_fd (fd, SFM_READ, &_info, true);
101
102         if (_sndfile == 0) {
103                 char errbuf[1024];
104                 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
105                 error << string_compose (_("LTCFileReader: cannot open file \"%1\" (%3)"), _path, errbuf) << endmsg;
106                 return -1;
107         }
108         if (_info.frames == 0 || _info.channels < 1) {
109                 error << string_compose (_("LTCFileReader: \"%1\" is an empty audio file"), _path) << endmsg;
110                 return -1;
111         }
112         _interleaved_audio_buffer = (float*) calloc (_info.channels * BUFFER_SIZE, sizeof (float));
113         return 0;
114 }
115
116 void
117 LTCFileReader::close ()
118 {
119         if (_sndfile) {
120                 sf_close (_sndfile);
121                 _sndfile = 0;
122         }
123 }
124
125 std::vector<LTCFileReader::LTCMap>
126 LTCFileReader::read_ltc (uint32_t channel, uint32_t max_frames)
127 {
128         std::vector<LTCFileReader::LTCMap> rv;
129         ltcsnd_sample_t sound[BUFFER_SIZE];
130         LTCFrameExt frame;
131
132         const uint32_t channels = _info.channels;
133         if (channel >= channels) {
134                 warning << _("LTCFileReader:: invalid audio channel selected") << endmsg;
135                 return rv;
136         }
137
138         while (1) {
139                 int64_t n = sf_readf_float (_sndfile, _interleaved_audio_buffer, BUFFER_SIZE);
140                 if (n <= 0) {
141                         break;
142                 }
143
144                 // convert audio to 8bit unsigned
145                 for (int64_t i = 0; i < n; ++i) {
146                         sound [i]= 128 + _interleaved_audio_buffer[channels * i + channel] * 127;
147                 }
148
149                 ltc_decoder_write (decoder, sound, n, _samples_read);
150
151                 while (ltc_decoder_read (decoder, &frame)) {
152                         SMPTETimecode stime;
153                         ++_frames_decoded;
154
155                         ltc_frame_to_time (&stime, &frame.ltc, /*use_date*/ 0);
156
157                         // convert Timecode to samples @ audio-file rate
158                         Timecode::Time timecode (_expected_fps);
159                         timecode.hours   = stime.hours;
160                         timecode.minutes = stime.mins;
161                         timecode.seconds = stime.secs;
162                         timecode.frames  = stime.frame;
163
164                         int64_t sample = 0;
165                         Timecode::timecode_to_sample (
166                                         timecode, sample, false, false,
167                                         _info.samplerate,
168                                         0, 0, 0);
169
170                         // align LTC frame relative to video-frame
171                         sample -= ltc_frame_alignment (
172                                         _info.samplerate / _expected_fps,
173                                         _ltc_tv_standard);
174
175                         // convert to seconds (session can use session-rate)
176                         double fp_sec = frame.off_start / (double) _info.samplerate;
177                         double tc_sec = sample / (double) _info.samplerate;
178                         rv.push_back (LTCMap (fp_sec, tc_sec));
179
180 #if 0 // DEBUG
181                         printf("LTC %02d:%02d:%02d:%02d @%9lld -> %9lld -> %fsec\n",
182                                         stime.hours,
183                                         stime.mins,
184                                         stime.secs,
185                                         stime.frame,
186                                         frame.off_start,
187                                         sample,
188                                         tc_sec);
189 #endif
190                 }
191
192                 if (n > 0) {
193                         _samples_read += n;
194                 }
195
196                 if (max_frames > 0 && rv.size () >= max_frames) {
197                         break;
198                 }
199
200         }
201
202         return rv;
203 }