OpenShot Library | libopenshot-audio  0.2.0
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 
23 namespace 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 //==============================================================================
40 void ToneGeneratorAudioSource::setAmplitude (const float newAmplitude)
41 {
42  amplitude = newAmplitude;
43 }
44 
45 void ToneGeneratorAudioSource::setFrequency (const double newFrequencyHz)
46 {
47  frequency = newFrequencyHz;
48  phasePerSample = 0.0;
49 }
50 
51 //==============================================================================
52 void 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
juce::ToneGeneratorAudioSource::getNextAudioBlock
void getNextAudioBlock(const AudioSourceChannelInfo &) override
Implementation of the AudioSource method.
Definition: juce_ToneGeneratorAudioSource.cpp:63
juce::ToneGeneratorAudioSource::ToneGeneratorAudioSource
ToneGeneratorAudioSource()
Creates a ToneGeneratorAudioSource.
Definition: juce_ToneGeneratorAudioSource.cpp:26
juce::MathConstants
Commonly used mathematical constants.
Definition: juce_MathsFunctions.h:377
juce::ToneGeneratorAudioSource::~ToneGeneratorAudioSource
~ToneGeneratorAudioSource() override
Destructor.
Definition: juce_ToneGeneratorAudioSource.cpp:35
juce::AudioBuffer::setSample
void setSample(int destChannel, int destSample, Type newValue) noexcept
Sets a sample in the buffer.
Definition: juce_AudioSampleBuffer.h:580
juce::ToneGeneratorAudioSource::setAmplitude
void setAmplitude(float newAmplitude)
Sets the signal's amplitude.
Definition: juce_ToneGeneratorAudioSource.cpp:40
juce::AudioSourceChannelInfo::startSample
int startSample
The first sample in the buffer from which the callback is expected to write data.
Definition: juce_AudioSource.h:81
juce::AudioSourceChannelInfo::numSamples
int numSamples
The number of samples in the buffer which the callback is expected to fill with data.
Definition: juce_AudioSource.h:85
juce::ToneGeneratorAudioSource::setFrequency
void setFrequency(double newFrequencyHz)
Sets the signal's frequency.
Definition: juce_ToneGeneratorAudioSource.cpp:45
juce::ToneGeneratorAudioSource::releaseResources
void releaseResources() override
Implementation of the AudioSource method.
Definition: juce_ToneGeneratorAudioSource.cpp:59
juce::AudioBuffer::getNumChannels
int getNumChannels() const noexcept
Returns the number of channels of audio data that this buffer contains.
Definition: juce_AudioSampleBuffer.h:237
juce::AudioSourceChannelInfo::buffer
AudioBuffer< float > * buffer
The destination buffer to fill with audio data.
Definition: juce_AudioSource.h:77
juce::AudioSourceChannelInfo
Used by AudioSource::getNextAudioBlock().
Definition: juce_AudioSource.h:36
juce::ToneGeneratorAudioSource::prepareToPlay
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override
Implementation of the AudioSource method.
Definition: juce_ToneGeneratorAudioSource.cpp:52