audioloop.cpp 2.86 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
jpbl's avatar
jpbl committed
3
4
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *
5
 *  Inspired by tonegenerator of
jpbl's avatar
jpbl committed
6
7
 *   Laurielle Lea <laurielle.lea@savoirfairelinux.com> (2004)
 *  Inspired by ringbuffer of Audacity Project
8
 *
jpbl's avatar
jpbl committed
9
10
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 3 of the License, or
jpbl's avatar
jpbl committed
12
13
14
15
16
17
18
19
20
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
21
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
22
23
24
25
26
27
28
29
30
31
32
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
jpbl's avatar
jpbl committed
33
 */
34

35
36
37
38
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

jpbl's avatar
jpbl committed
39
#include "audioloop.h"
40

41
42
#include <cmath>
#include <numeric>
Rafaël Carré's avatar
Rafaël Carré committed
43
#include <cstring>
Rafaël Carré's avatar
Rafaël Carré committed
44
#include <cassert>
45
#include "logger.h"
jpbl's avatar
jpbl committed
46

47
48
AudioLoop::AudioLoop(unsigned int sampleRate) : buffer_(0), pos_(0)
{
49
    buffer_ = new AudioBuffer(0);
50
51
    buffer_->setSampleRate(sampleRate);
}
jpbl's avatar
jpbl committed
52

Emmanuel Milou's avatar
[#2402]    
Emmanuel Milou committed
53
AudioLoop::~AudioLoop()
54
55
56
{
    delete buffer_;
}
jpbl's avatar
jpbl committed
57

58
59
60
void
AudioLoop::seek(double relative_position)
{
61
    pos_ = static_cast<double>(buffer_->frames() * relative_position * 0.01);
62
63
}

64
void
65
AudioLoop::getNext(AudioBuffer& output, double gain)
Emmanuel Milou's avatar
[#2402]    
Emmanuel Milou committed
66
{
67
68
69
70
71
    if (!buffer_) {
        ERROR("buffer is NULL");
        return;
    }

72
    const size_t buf_samples = buffer_->frames();
73
    size_t pos = pos_;
74
    size_t total_samples = output.frames();
75
    size_t output_pos = 0;
Rafaël Carré's avatar
Rafaël Carré committed
76

77
    if (buf_samples == 0) {
Tristan Matthews's avatar
Tristan Matthews committed
78
        ERROR("Audio loop size is 0");
79
        return;
80
    } else if (pos >= buf_samples) {
Tristan Matthews's avatar
Tristan Matthews committed
81
        ERROR("Invalid loop position %d", pos);
82
        return;
83
84
    }

Tristan Matthews's avatar
Tristan Matthews committed
85
    while (total_samples > 0) {
86
        size_t samples = std::min(total_samples, buf_samples - pos);
87

88
        output.copy(*buffer_, samples, pos, output_pos);
89

90
        output_pos += samples;
Adrien Béraud's avatar
Adrien Béraud committed
91
        pos = (pos + samples) % buf_samples;
92

93
        total_samples -= samples;
jpbl's avatar
jpbl committed
94
    }
95

96
    output.applyGain(gain);
97

98
    pos_ = pos;
99

100
    onBufferFinish();
jpbl's avatar
jpbl committed
101
102
}

103
void AudioLoop::onBufferFinish() {}