1 #include "AdmScriptMapper.h"
2 #include "AudioOutput.h"
3 #include "ADM_editor/include/ADM_edAudioTrack.h"
4 #include "ADM_editor/include/ADM_edAudioTrackExternal.h"
5 
6 namespace ADM_qtScript
7 {
AudioOutput(IEditor * editor,EditableAudioTrack * track)8     AudioOutput::AudioOutput(IEditor* editor, EditableAudioTrack *track) : QtScriptObject(editor)
9     {
10         this->_track = track;
11         this->_trackObjectId = track->objectId;
12         this->_mapper = new AdmScriptMapper();
13     }
14 
~AudioOutput()15     AudioOutput::~AudioOutput()
16     {
17         delete this->_mapper;
18     }
19 
getAudioInputFile()20     QScriptValue AudioOutput::getAudioInputFile()
21     {
22         QScriptValue inputType = this->getAudioInputType();
23 
24         if (inputType.isUndefined() || inputType.toNumber() != AudioOutput::ExternalFile)
25         {
26             return QScriptValue(QScriptValue::UndefinedValue);
27         }
28         else
29         {
30             return this->_track->edTrack->castToExternal()->getMyName().c_str();
31         }
32     }
33 
getAudioInputIndex()34     QScriptValue AudioOutput::getAudioInputIndex()
35     {
36         QScriptValue inputType = this->getAudioInputType();
37 
38         if (inputType.isUndefined() || inputType.toNumber() != AudioOutput::SourceVideo)
39         {
40             return QScriptValue(QScriptValue::UndefinedValue);
41         }
42         else
43         {
44             return this->_track->poolIndex;
45         }
46     }
47 
getAudioInputType()48     QScriptValue AudioOutput::getAudioInputType()
49     {
50         if (this->verifyTrack())
51         {
52             if (this->_track->edTrack->getTrackType() == ADM_EDAUDIO_EXTERNAL)
53             {
54                 return AudioOutput::ExternalFile;
55             }
56             else
57             {
58                 return AudioOutput::SourceVideo;
59             }
60         }
61         else
62         {
63             return QScriptValue(QScriptValue::UndefinedValue);
64         }
65     }
66 
getEncoder()67     QScriptValue AudioOutput::getEncoder()
68     {
69         if (this->verifyTrack())
70         {
71             return this->engine()->newQObject(
72                        new AudioEncoder(
73                            this->engine(), this->_editor, ListOfAudioEncoder[this->_track->encoderIndex],
74                            this->_track->encoderIndex, this->_track), QScriptEngine::ScriptOwnership);
75         }
76         else
77         {
78             return QScriptValue(QScriptValue::UndefinedValue);
79         }
80     }
81 
getGainMode()82     QScriptValue AudioOutput::getGainMode()
83     {
84         if (this->verifyTrack())
85         {
86             return this->_mapper->toScriptValue(this->_track->audioEncodingConfig.gainParam.mode);
87         }
88         else
89         {
90             return QScriptValue(QScriptValue::UndefinedValue);
91         }
92     }
93 
getGainValue()94     QScriptValue AudioOutput::getGainValue()
95     {
96         if (this->verifyTrack())
97         {
98             if (this->_track->audioEncodingConfig.gainParam.mode == ADM_GAIN_MANUAL)
99             {
100                 return ((double)this->_track->audioEncodingConfig.gainParam.gain10) / 10;
101             }
102             else
103             {
104                 return 0;
105             }
106         }
107         else
108         {
109             return QScriptValue(QScriptValue::UndefinedValue);
110         }
111     }
112 
getMixer()113     QScriptValue AudioOutput::getMixer()
114     {
115         if (this->verifyTrack())
116         {
117             return _mapper->toScriptValue(this->_track->audioEncodingConfig.mixerConf);
118         }
119         else
120         {
121             return QScriptValue(QScriptValue::UndefinedValue);
122         }
123     }
124 
getDrcMode()125     QScriptValue AudioOutput::getDrcMode()
126     {
127         if (this->verifyTrack())
128         {
129             return this->_track->audioEncodingConfig.drcEnabled;
130         }
131         else
132         {
133             return QScriptValue(QScriptValue::UndefinedValue);
134         }
135     }
136 
getResample()137     QScriptValue AudioOutput::getResample()
138     {
139         if (this->verifyTrack())
140         {
141             if (this->_track->audioEncodingConfig.resamplerEnabled)
142             {
143                 return this->_track->audioEncodingConfig.resamplerFrequency;
144             }
145             else
146             {
147                 return 0;
148             }
149         }
150         else
151         {
152             return QScriptValue(QScriptValue::UndefinedValue);
153         }
154     }
155 
getStretchAudioMode()156     QScriptValue AudioOutput::getStretchAudioMode()
157     {
158         if (this->verifyTrack())
159         {
160             return _mapper->toScriptValue(this->_track->audioEncodingConfig.film2pal);
161         }
162         else
163         {
164             return QScriptValue(QScriptValue::UndefinedValue);
165         }
166     }
167 
getTimeShift()168     QScriptValue AudioOutput::getTimeShift()
169     {
170         if (this->verifyTrack())
171         {
172 			if (this->_track->audioEncodingConfig.shiftEnabled)
173 			{
174 				return this->_track->audioEncodingConfig.shiftInMs;
175 			}
176 			else
177 			{
178 				return 0;
179 			}
180         }
181         else
182         {
183             return QScriptValue(QScriptValue::UndefinedValue);
184         }
185     }
186 
setAudioInputFile(QScriptValue inputFile)187     void AudioOutput::setAudioInputFile(QScriptValue inputFile)
188     {
189         if (this->verifyTrack())
190         {
191             PoolOfAudioTracks* audioTracks = this->_editor->getPoolOfAudioTrack();
192             const char* constInputFile = inputFile.toString().toUtf8().constData();
193 
194             for (int trackIndex = 0; trackIndex < audioTracks->size(); trackIndex++)
195             {
196                 ADM_edAudioTrack* track = audioTracks->at(trackIndex);
197 
198                 if (track->getTrackType() == ADM_EDAUDIO_EXTERNAL &&
199                         track->castToExternal()->getMyName().compare(constInputFile))
200                 {
201                     this->_track->edTrack = track;
202                     return;
203                 }
204             }
205 
206             if (this->_editor->addExternalAudioTrack(constInputFile))
207             {
208                 this->_track->edTrack = audioTracks->at(audioTracks->size() - 1);
209                 this->_track->poolIndex = audioTracks->size() - 1;
210             }
211             else
212             {
213                 this->throwError("Unable to add external audio file.");
214             }
215         }
216     }
217 
setAudioInputIndex(QScriptValue inputIndex)218     void AudioOutput::setAudioInputIndex(QScriptValue inputIndex)
219     {
220         if (this->verifyTrack())
221         {
222             PoolOfAudioTracks* audioTracks = this->_editor->getPoolOfAudioTrack();
223 
224             if (audioTracks->size() == 0)
225             {
226                 this->throwError(
227                     "The source video doesn't contain a valid audio track to use for encoding.");
228                 return;
229             }
230 
231             QScriptValue result = this->validateNumber(
232                                       "inputIndex", inputIndex, 0, audioTracks->size() - 1);
233 
234             if (!result.isUndefined())
235             {
236                 return;
237             }
238 
239             this->_track->edTrack = audioTracks->at(inputIndex.toNumber());
240             this->_track->poolIndex = inputIndex.toNumber();
241         }
242     }
243 
setEncoder(QScriptValue encoder)244     void AudioOutput::setEncoder(QScriptValue encoder)
245     {
246         if (this->verifyTrack())
247         {
248             AudioEncoder *encoderObject = qobject_cast<AudioEncoder*>(encoder.toQObject());
249 
250             if (encoderObject != NULL)
251             {
252                 if (encoderObject->isEncoderUsed())
253                 {
254                     this->throwError("Audio encoder is already being used by another audio output.");
255                 }
256                 else
257                 {
258                     encoderObject->useEncoderForAudioOutput(this->_track);
259                     this->_editor->updateDefaultAudioTrack();
260                 }
261             }
262         }
263     }
264 
setDrcMode(QScriptValue drc)265     void AudioOutput::setDrcMode(QScriptValue drc)
266     {
267         if (this->verifyTrack() && drc.isBool())
268         {
269             this->_track->audioEncodingConfig.drcEnabled = drc.toBool();
270         }
271     }
272 
setGainMode(QScriptValue gainMode)273     void AudioOutput::setGainMode(QScriptValue gainMode)
274     {
275         QScriptValue validateResult = this->validateNumber("gainMode", gainMode);
276 
277         if (this->verifyTrack() && validateResult.isUndefined())
278         {
279             this->_track->audioEncodingConfig.gainParam.mode = _mapper->toAdmValue((GainMode)gainMode.toNumber());
280         }
281     }
282 
setGainValue(QScriptValue gainValue)283     void AudioOutput::setGainValue(QScriptValue gainValue)
284     {
285         QScriptValue validateResult = this->validateNumber("gainValue", gainValue, -10, 40);
286 
287         if (this->verifyTrack() && validateResult.isUndefined())
288         {
289             qsreal gain = gainValue.toNumber();
290 
291             if (gain == 0.0)
292             {
293                 this->_track->audioEncodingConfig.gainParam.mode = ADM_NO_GAIN;
294             }
295             else
296             {
297                 this->_track->audioEncodingConfig.gainParam.mode = ADM_GAIN_MANUAL;
298             }
299 
300             this->_track->audioEncodingConfig.gainParam.gain10 = (int)(gain * 10);
301         }
302     }
303 
setMixer(QScriptValue mixer)304     void AudioOutput::setMixer(QScriptValue mixer)
305     {
306         QScriptValue validateResult = this->validateNumber("mixer", mixer);
307 
308         if (this->verifyTrack() && validateResult.isUndefined())
309         {
310             this->_track->audioEncodingConfig.mixerConf = _mapper->toAdmValue((MixerMode)mixer.toNumber());
311             this->_track->audioEncodingConfig.mixerEnabled = (this->_track->audioEncodingConfig.mixerConf != CHANNEL_INVALID);
312         }
313     }
314 
setResample(QScriptValue resample)315     void AudioOutput::setResample(QScriptValue resample)
316     {
317         QScriptValue validateResult = this->validateNumber("resample", resample);
318 
319         if (this->verifyTrack() && validateResult.isUndefined())
320         {
321             int hz = resample.toNumber();
322 
323             if (hz == 0)
324             {
325                 this->_track->audioEncodingConfig.resamplerEnabled = 0;
326             }
327             else
328             {
329                 QScriptValue validateResult = this->validateNumber("resample", resample, 6000, 64000);
330 
331                 if (validateResult.isUndefined())
332                 {
333                     this->_track->audioEncodingConfig.resamplerEnabled = 1;
334                     this->_track->audioEncodingConfig.resamplerFrequency = hz;
335                 }
336             }
337         }
338     }
339 
setStretchAudioMode(QScriptValue stretchAudioMode)340     void AudioOutput::setStretchAudioMode(QScriptValue stretchAudioMode)
341     {
342         QScriptValue validateResult = this->validateNumber("stretchAudioMode", stretchAudioMode);
343 
344         if (this->verifyTrack() && validateResult.isUndefined())
345         {
346             this->_track->audioEncodingConfig.film2pal = _mapper->toAdmValue((StretchAudioMode)stretchAudioMode.toNumber());
347         }
348     }
349 
setTimeShift(QScriptValue timeShift)350     void AudioOutput::setTimeShift(QScriptValue timeShift)
351     {
352         QScriptValue validateResult = this->validateNumber("timeShift", timeShift, -99999, 99999);
353 
354         if (this->verifyTrack() && validateResult.isUndefined())
355         {
356 			int shift = timeShift.toNumber();
357 
358             if (shift == 0)
359 			{
360 				this->_track->audioEncodingConfig.shiftEnabled = false;
361 			}
362 			else
363 			{
364 				this->_track->audioEncodingConfig.shiftEnabled = true;
365 				this->_track->audioEncodingConfig.shiftInMs = timeShift.toNumber();
366 			}
367         }
368     }
369 
verifyTrack()370     bool AudioOutput::verifyTrack()
371     {
372         ActiveAudioTracks* tracks = this->_editor->getPoolOfActiveAudioTrack();
373         bool found = false;
374 
375         for (unsigned int trackIndex = 0; trackIndex < tracks->size(); trackIndex++)
376         {
377             EditableAudioTrack *track = tracks->atEditable(trackIndex);
378 
379             if (this->_track == track && this->_trackObjectId == track->objectId)
380             {
381                 found = true;
382                 break;
383             }
384         }
385 
386         return found;
387     }
388 }
389