1 /*
2  * Olive. Olive is a free non-linear video editor for Windows, macOS, and Linux.
3  * Copyright (C) 2018  {{ organization }}
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  *along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include "audionoiseeffect.h"
19 
20 #include <QDateTime>
21 #include <QtMath>
22 
AudioNoiseEffect(Clip * c,const EffectMeta * em)23 AudioNoiseEffect::AudioNoiseEffect(Clip* c, const EffectMeta *em) : Effect(c, em) {
24   EffectRow* amount_row = new EffectRow(this, tr("Amount"));
25   amount_val = new DoubleField(amount_row, "amount");
26   amount_val->SetMinimum(0);
27   amount_val->SetDefault(20);
28   amount_val->SetMaximum(100);
29 
30   EffectRow* mix_row = new EffectRow(this, tr("Mix"));
31   mix_val = new BoolField(mix_row, "mix");
32   mix_val->SetValueAt(0, true);
33 }
34 
process_audio(double timecode_start,double timecode_end,quint8 * samples,int nb_bytes,int)35 void AudioNoiseEffect::process_audio(double timecode_start, double timecode_end, quint8 *samples, int nb_bytes, int) {
36   double interval = (timecode_end - timecode_start)/nb_bytes;
37   for (int i=0;i<nb_bytes;i+=4) {
38     double timecode = timecode_start+(interval*i);
39 
40     qint16 left_noise_sample = this->randomNumber<qint16>();
41     qint16 right_noise_sample = this->randomNumber<qint16>();
42 
43     // set noise volume
44     double vol = log_volume( amount_val->GetDoubleAt(timecode)*0.01 );
45     left_noise_sample *= vol;
46     right_noise_sample *= vol;
47 
48     // mix with source audio
49     if (mix_val->GetBoolAt(timecode)) {
50       qint16 left_sample = static_cast<qint16> (((samples[i+1] & 0xFF) << 8) | (samples[i] & 0xFF));
51       qint16 right_sample = static_cast<qint16> (((samples[i+3] & 0xFF) << 8) | (samples[i+2] & 0xFF));
52       left_noise_sample = mix_audio_sample(left_noise_sample, left_sample);
53       right_noise_sample = mix_audio_sample(right_noise_sample, right_sample);
54     }
55 
56     samples[i+3] = static_cast<quint8> (right_noise_sample >> 8);
57     samples[i+2] = static_cast<quint8> (right_noise_sample);
58     samples[i+1] = static_cast<quint8> (left_noise_sample >> 8);
59     samples[i] = static_cast<quint8> (left_noise_sample);
60   }
61 }
62