OpenShot Library | OpenShotAudio 0.2.2
juce_ToneGeneratorAudioSource.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
27 : frequency (1000.0),
28 sampleRate (44100.0),
29 currentPhase (0.0),
30 phasePerSample (0.0),
31 amplitude (0.5f)
32{
33}
34
36{
37}
38
39//==============================================================================
40void ToneGeneratorAudioSource::setAmplitude (const float newAmplitude)
41{
42 amplitude = newAmplitude;
43}
44
45void ToneGeneratorAudioSource::setFrequency (const double newFrequencyHz)
46{
47 frequency = newFrequencyHz;
48 phasePerSample = 0.0;
49}
50
51//==============================================================================
52void ToneGeneratorAudioSource::prepareToPlay (int /*samplesPerBlockExpected*/, double rate)
53{
54 currentPhase = 0.0;
55 phasePerSample = 0.0;
56 sampleRate = rate;
57}
58
60{
61}
62
64{
65 if (phasePerSample == 0.0)
66 phasePerSample = MathConstants<double>::twoPi / (sampleRate / frequency);
67
68 for (int i = 0; i < info.numSamples; ++i)
69 {
70 const float sample = amplitude * (float) std::sin (currentPhase);
71 currentPhase += phasePerSample;
72
73 for (int j = info.buffer->getNumChannels(); --j >= 0;)
74 info.buffer->setSample (j, info.startSample + i, sample);
75 }
76}
77
78} // namespace juce
int getNumChannels() const noexcept
Returns the number of channels of audio data that this buffer contains.
void setSample(int destChannel, int destSample, Type newValue) noexcept
Sets a sample in the buffer.
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override
Implementation of the AudioSource method.
void releaseResources() override
Implementation of the AudioSource method.
void setFrequency(double newFrequencyHz)
Sets the signal's frequency.
ToneGeneratorAudioSource()
Creates a ToneGeneratorAudioSource.
void getNextAudioBlock(const AudioSourceChannelInfo &) override
Implementation of the AudioSource method.
void setAmplitude(float newAmplitude)
Sets the signal's amplitude.
Used by AudioSource::getNextAudioBlock().
int numSamples
The number of samples in the buffer which the callback is expected to fill with data.
AudioBuffer< float > * buffer
The destination buffer to fill with audio data.
int startSample
The first sample in the buffer from which the callback is expected to write data.
Commonly used mathematical constants.