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