5f68a2d5ce808c81a020586e8c8302c7573d1d5f
[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 <glibmm.h>
23 #include <assert.h>
24
25 #include "pbd/error.h"
26 #include "pbd/failed_constructor.h"
27
28 #include "timecode/time.h"
29 #include "ardour/ltc_file_reader.h"
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace ARDOUR;
35 using namespace PBD;
36 using std::string;
37
38 #define BUFFER_SIZE 1024 // audio chunk size
39
40 LTCFileReader::LTCFileReader (std::string path, double expected_fps, LTC_TV_STANDARD tv_standard)
41         : _path (path)
42         , _expected_fps (expected_fps)
43         , _ltc_tv_standard (tv_standard)
44         , _sndfile (0)
45         , _interleaved_audio_buffer (0)
46         , _frames_decoded (0)
47         , _samples_read (0)
48 {
49         memset (&_info, 0, sizeof (_info));
50         assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
51
52         if (open ()) {
53                 throw failed_constructor ();
54         }
55
56         const int apv = rintf (_info.samplerate / _expected_fps);
57         decoder = ltc_decoder_create (apv, 8); // must be able to hold frmes for BUFFER_SIZE
58
59 #if 0 // TODO allow to auto-detect
60         if (expected_fps == 25.0) {
61                 _ltc_tv_standard = LTC_TV_625_50;
62         }
63         else if (expected_fps == 30.0) {
64                 _ltc_tv_standard = LTC_TV_525_60;
65         }
66         else {
67                 _ltc_tv_standard = LTC_TV_FILM_24;
68         }
69 #endif
70 }
71
72 LTCFileReader::~LTCFileReader ()
73 {
74         close ();
75         ltc_decoder_free (decoder);
76         free (_interleaved_audio_buffer);
77 }
78
79 int
80 LTCFileReader::open ()
81 {
82         if (_sndfile) {
83                 return 0;
84         }
85
86 #ifdef PLATFORM_WINDOWS
87         int fd = g_open (_path.c_str (), O_RDONLY, 0444);
88 #else
89         int fd = ::open (_path.c_str (), O_RDONLY, 0444);
90 #endif
91         if (fd == -1) {
92                 error << string_compose (_("LTCFileReader: cannot open file \"%1\""), _path) << endmsg;
93                 return -1;
94         }
95
96         _sndfile = sf_open_fd (fd, SFM_READ, &_info, true);
97
98         if (_sndfile == 0) {
99                 char errbuf[1024];
100                 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
101                 error << string_compose (_("LTCFileReader: cannot open file \"%1\" (%3)"), _path, errbuf) << endmsg;
102                 return -1;
103         }
104         if (_info.frames == 0 || _info.channels < 1) {
105                 error << string_compose (_("LTCFileReader: \"%1\" is an empty audio file"), _path) << endmsg;
106                 return -1;
107         }
108         _interleaved_audio_buffer = (float*) calloc (_info.channels * BUFFER_SIZE, sizeof (float));
109         return 0;
110 }
111
112 void
113 LTCFileReader::close ()
114 {
115         if (_sndfile) {
116                 sf_close (_sndfile);
117                 _sndfile = 0;
118         }
119 }
120
121 std::vector<LTCFileReader::LTCMap>
122 LTCFileReader::read_ltc (uint32_t channel, uint32_t max_frames)
123 {
124         std::vector<LTCFileReader::LTCMap> rv;
125         ltcsnd_sample_t sound[BUFFER_SIZE];
126         LTCFrameExt frame;
127
128         const uint32_t channels = _info.channels;
129         if (channel >= channels) {
130                 warning << _("LTCFileReader:: invalid audio channel selected") << endmsg;
131                 return rv;
132         }
133
134         while (1) {
135                 int64_t n = sf_readf_float (_sndfile, _interleaved_audio_buffer, BUFFER_SIZE);
136                 if (n <= 0) {
137                         break;
138                 }
139
140                 // convert audio to 8bit unsigned
141                 for (int64_t i = 0; i < n; ++i) {
142                         sound [i]= 128 + _interleaved_audio_buffer[channels * i + channel] * 127;
143                 }
144
145                 ltc_decoder_write (decoder, sound, n, _samples_read);
146
147                 while (ltc_decoder_read (decoder, &frame)) {
148                         SMPTETimecode stime;
149                         ++_frames_decoded;
150
151                         ltc_frame_to_time (&stime, &frame.ltc, /*use_date*/ 0);
152
153                         // convert Timecode to samples @ audio-file rate
154                         Timecode::Time timecode (_expected_fps);
155                         timecode.hours   = stime.hours;
156                         timecode.minutes = stime.mins;
157                         timecode.seconds = stime.secs;
158                         timecode.frames  = stime.frame;
159
160                         int64_t sample = 0;
161                         Timecode::timecode_to_sample (
162                                         timecode, sample, false, false,
163                                         _info.samplerate,
164                                         0, 0, 0);
165
166                         // align LTC frame relative to video-frame
167                         sample -= ltc_frame_alignment (
168                                         _info.samplerate / _expected_fps,
169                                         _ltc_tv_standard);
170
171                         // convert to seconds (session can use session-rate)
172                         double fp_sec = frame.off_start / (double) _info.samplerate;
173                         double tc_sec = sample / (double) _info.samplerate;
174                         rv.push_back (LTCMap (fp_sec, tc_sec));
175
176 #if 0 // DEBUG
177                         printf("LTC %02d:%02d:%02d:%02d @%9lld -> %9lld -> %fsec\n",
178                                         stime.hours,
179                                         stime.mins,
180                                         stime.secs,
181                                         stime.frame,
182                                         frame.off_start,
183                                         sample,
184                                         tc_sec);
185 #endif
186                 }
187
188                 if (n > 0) {
189                         _samples_read += n;
190                 }
191
192                 if (max_frames > 0 && rv.size () >= max_frames) {
193                         break;
194                 }
195
196         }
197
198         return rv;
199 }