# 開發紀錄 week3
###### tags: `數位音樂訊號分析`
### HW 1
---
#### 1. sine wave
<img width="500" height="300" src="https://i.imgur.com/Sw18Zkq.png"/>
```cpp=
//sine wave
float value = std::sin(currentAngle) * level * tailOff;
outputBuffer.addSample(0, i, value);
outputBuffer.addSample(1, i, value);
```
---
#### 2. 將sine wave改成square wave
<img width="500" height="300" src="https://i.imgur.com/1XiuUCQ.png"/>
<br>
<br>
* Think 1: 利用current angle θ
(1) 0+2πf < θ < π+2πf , 給value = 1
(2) π+2πf < θ < 2π+2πf , 給value = -1
* Think 2: 利用sin(θ)
(1) 0+2πf < θ < π+2πf , 此時sin(θ) > 0 , 給value = 1
(2) π+2πf < θ < 2π+2πf , 此時sin(θ) < 0 , 給value = -1
```cpp=
//square wave
float value = (std::sin(currentAngle) >= 0 ? 1 : -1) ;
value = value * level * tailOff;
outputBuffer.addSample(0, i, value);
outputBuffer.addSample(1, i, value);
```
---
#### 3. 將sine wave改成triangle wave
<img width="500" height="300" src="https://i.imgur.com/3G4jLF2.png"/>
<br>
<br>
* Think: 利用斜率
(1) 角度從0+2πf到π/2+2πf時, 斜率m = 2/π
(2) 角度從π/2+2πf到π+2πf時, 斜率m = -2/π
(3) 角度從π+2πf到3π/2+2πf時, 斜率m = -2/π
(4) 角度從3π/2+2πf到2π+2πf時, 斜率m = 2/π
```cpp=
if (currentAngle > juce::MathConstants<float>::twoPi)
{
currentAngle = currentAngle - juce::MathConstants<float>::twoPi;
}
if (currentAngle < 1 / 2 * juce::MathConstants<float>::pi)
{
float value = 2 / juce::MathConstants<float>::pi * currentAngle * level * tailOff;
outputBuffer.addSample(0, i, value);
outputBuffer.addSample(1, i, value);
}
else if (1 / 2 * juce::MathConstants<float>::pi < currentAngle < 3 / 2 * juce::MathConstants<float>::pi) {
float value = (-2 / juce::MathConstants<float>::pi * currentAngle + 2) * level * tailOff;
outputBuffer.addSample(0, i, value);
outputBuffer.addSample(1, i, value);
}
else if (currentAngle > 3 / 2 * juce::MathConstants<float>::pi)
{
float value = (2 / juce::MathConstants<float>::pi * currentAngle - 4) * level * tailOff;
outputBuffer.addSample(0, i, value);
outputBuffer.addSample(1, i, value);
}
```
---
#### 4. 將sine wave改成sawtooth wave
<img width="500" height="300" src="https://i.imgur.com/bPovjrV.png"/>
<br>
<br>
* Think: 利用斜率
角度從(-π)+2πf到π+2πf時,斜率m = 1/π
```cpp=
//sawtooth wave
if (currentAngle > juce::MathConstants<float>::twoPi)
{
currentAngle = currentAngle - juce::MathConstants<float>::twoPi;
}
float value = (1 / juce::MathConstants<float>::pi * currentAngle - 1) * level * tailOff;
outputBuffer.addSample(0, i, value);
outputBuffer.addSample(1, i, value);
```
<br>
### Note
---
#### Setting Project
<img width="500" height="300" src="https://i.imgur.com/sqaczsA.jpg"/>
---
#### SynthVoice.h
* 繼承 juce::SynthesiserVoice implement 以下函式
```cpp=
public:
bool canPlaySound(juce::SynthesiserSound* sound) override;
void startNote(int midiNoteNumber, float velocity, juce::SynthesiserSound* sound, int currentPitchWheelPosition) override;
void stopNote(float velocity, bool allowTailOff) override;
void pitchWheelMoved(int newPitchWheelValue) override;
void controllerMoved(int controllerNumber, int newControllerValue) override;
void renderNextBlock(juce::AudioBuffer <float> &outputBuffer, int startSample, int numSamples) override;
void setLevel(float newLevel);
private:
float level;
float frequency;
int noteMidiNumber;
float currentAngle;
float angleIncrement;
float tailOff;
```
* level: 決定音量大小
* frequency: 決定音高
* currentAngle: 目前sine wave相位
* angleIncrement: 增加多少相位
* tailOff: note off後音量大小
* noteMidiNumber: check [here](https://newt.phys.unsw.edu.au/jw/notes.html)
---
#### Plug-in
* 導入NewProject
<img width="360" height="300" src="https://i.imgur.com/zB4LEG3.jpg"/> <img width="360" height="300" src="https://i.imgur.com/ibzEPkR.jpg"/>
* JUCE視窗
<img width="360" height="300" src="https://i.imgur.com/sq8i8ny.jpg"/>