Compare commits
16 Commits
refactor/c
...
feature/ad
| Author | SHA1 | Date | |
|---|---|---|---|
|
df7b886526
|
|||
|
54c4e540ac
|
|||
|
a0514bad98
|
|||
|
fd67e7b843
|
|||
|
de31b73673
|
|||
|
564955c911
|
|||
|
ef40eaf7ef
|
|||
|
d883bbbf12
|
|||
|
73aae9a490
|
|||
|
c6c2956ac0
|
|||
|
635de894ad
|
|||
|
c16447f30e
|
|||
|
c63db4fa07
|
|||
|
64fa6396bc
|
|||
|
268103d7da
|
|||
| a445fc44b3 |
216
.clang-format
Normal file
216
.clang-format
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
---
|
||||||
|
Language: Cpp
|
||||||
|
# BasedOnStyle: LLVM
|
||||||
|
AccessModifierOffset: -2
|
||||||
|
AlignAfterOpenBracket: Align
|
||||||
|
AlignArrayOfStructures: None
|
||||||
|
AlignConsecutiveAssignments:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: true
|
||||||
|
AlignConsecutiveBitFields:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: false
|
||||||
|
AlignConsecutiveDeclarations:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: false
|
||||||
|
AlignConsecutiveMacros:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: false
|
||||||
|
AlignEscapedNewlines: Right
|
||||||
|
AlignOperands: Align
|
||||||
|
AlignTrailingComments: true
|
||||||
|
AllowAllArgumentsOnNextLine: true
|
||||||
|
AllowAllParametersOfDeclarationOnNextLine: true
|
||||||
|
AllowShortEnumsOnASingleLine: true
|
||||||
|
AllowShortBlocksOnASingleLine: Never
|
||||||
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
|
AllowShortFunctionsOnASingleLine: All
|
||||||
|
AllowShortLambdasOnASingleLine: All
|
||||||
|
AllowShortIfStatementsOnASingleLine: Never
|
||||||
|
AllowShortLoopsOnASingleLine: false
|
||||||
|
AlwaysBreakAfterDefinitionReturnType: None
|
||||||
|
AlwaysBreakAfterReturnType: None
|
||||||
|
AlwaysBreakBeforeMultilineStrings: false
|
||||||
|
AlwaysBreakTemplateDeclarations: MultiLine
|
||||||
|
AttributeMacros:
|
||||||
|
- __capability
|
||||||
|
BinPackArguments: true
|
||||||
|
BinPackParameters: true
|
||||||
|
BraceWrapping:
|
||||||
|
AfterCaseLabel: false
|
||||||
|
AfterClass: false
|
||||||
|
AfterControlStatement: Never
|
||||||
|
AfterEnum: false
|
||||||
|
AfterFunction: false
|
||||||
|
AfterNamespace: false
|
||||||
|
AfterObjCDeclaration: false
|
||||||
|
AfterStruct: false
|
||||||
|
AfterUnion: false
|
||||||
|
AfterExternBlock: false
|
||||||
|
BeforeCatch: false
|
||||||
|
BeforeElse: false
|
||||||
|
BeforeLambdaBody: false
|
||||||
|
BeforeWhile: false
|
||||||
|
IndentBraces: false
|
||||||
|
SplitEmptyFunction: true
|
||||||
|
SplitEmptyRecord: true
|
||||||
|
SplitEmptyNamespace: true
|
||||||
|
BreakBeforeBinaryOperators: None
|
||||||
|
BreakBeforeConceptDeclarations: Always
|
||||||
|
BreakBeforeBraces: Attach
|
||||||
|
BreakBeforeInheritanceComma: false
|
||||||
|
BreakInheritanceList: BeforeColon
|
||||||
|
BreakBeforeTernaryOperators: true
|
||||||
|
BreakConstructorInitializersBeforeComma: false
|
||||||
|
BreakConstructorInitializers: BeforeColon
|
||||||
|
BreakAfterJavaFieldAnnotations: false
|
||||||
|
BreakStringLiterals: true
|
||||||
|
ColumnLimit: 80
|
||||||
|
CommentPragmas: '^ IWYU pragma:'
|
||||||
|
QualifierAlignment: Leave
|
||||||
|
CompactNamespaces: false
|
||||||
|
ConstructorInitializerIndentWidth: 4
|
||||||
|
ContinuationIndentWidth: 4
|
||||||
|
Cpp11BracedListStyle: true
|
||||||
|
DeriveLineEnding: true
|
||||||
|
DerivePointerAlignment: false
|
||||||
|
DisableFormat: false
|
||||||
|
EmptyLineAfterAccessModifier: Never
|
||||||
|
EmptyLineBeforeAccessModifier: LogicalBlock
|
||||||
|
ExperimentalAutoDetectBinPacking: false
|
||||||
|
PackConstructorInitializers: BinPack
|
||||||
|
BasedOnStyle: ''
|
||||||
|
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
||||||
|
AllowAllConstructorInitializersOnNextLine: true
|
||||||
|
FixNamespaceComments: true
|
||||||
|
ForEachMacros:
|
||||||
|
- foreach
|
||||||
|
- Q_FOREACH
|
||||||
|
- BOOST_FOREACH
|
||||||
|
IfMacros:
|
||||||
|
- KJ_IF_MAYBE
|
||||||
|
IncludeBlocks: Preserve
|
||||||
|
IncludeCategories:
|
||||||
|
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
|
||||||
|
Priority: 2
|
||||||
|
SortPriority: 0
|
||||||
|
CaseSensitive: false
|
||||||
|
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
|
||||||
|
Priority: 3
|
||||||
|
SortPriority: 0
|
||||||
|
CaseSensitive: false
|
||||||
|
- Regex: '.*'
|
||||||
|
Priority: 1
|
||||||
|
SortPriority: 0
|
||||||
|
CaseSensitive: false
|
||||||
|
IncludeIsMainRegex: '(Test)?$'
|
||||||
|
IncludeIsMainSourceRegex: ''
|
||||||
|
IndentAccessModifiers: false
|
||||||
|
IndentCaseLabels: true
|
||||||
|
IndentCaseBlocks: false
|
||||||
|
IndentGotoLabels: true
|
||||||
|
IndentPPDirectives: None
|
||||||
|
IndentExternBlock: AfterExternBlock
|
||||||
|
IndentRequiresClause: true
|
||||||
|
IndentWidth: 4
|
||||||
|
IndentWrappedFunctionNames: false
|
||||||
|
InsertBraces: false
|
||||||
|
InsertTrailingCommas: None
|
||||||
|
JavaScriptQuotes: Leave
|
||||||
|
JavaScriptWrapImports: true
|
||||||
|
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||||
|
LambdaBodyIndentation: Signature
|
||||||
|
MacroBlockBegin: ''
|
||||||
|
MacroBlockEnd: ''
|
||||||
|
MaxEmptyLinesToKeep: 1
|
||||||
|
NamespaceIndentation: None
|
||||||
|
ObjCBinPackProtocolList: Auto
|
||||||
|
ObjCBlockIndentWidth: 2
|
||||||
|
ObjCBreakBeforeNestedBlockParam: true
|
||||||
|
ObjCSpaceAfterProperty: false
|
||||||
|
ObjCSpaceBeforeProtocolList: true
|
||||||
|
PenaltyBreakAssignment: 2
|
||||||
|
PenaltyBreakBeforeFirstCallParameter: 19
|
||||||
|
PenaltyBreakComment: 300
|
||||||
|
PenaltyBreakFirstLessLess: 120
|
||||||
|
PenaltyBreakOpenParenthesis: 0
|
||||||
|
PenaltyBreakString: 1000
|
||||||
|
PenaltyBreakTemplateDeclaration: 10
|
||||||
|
PenaltyExcessCharacter: 1000000
|
||||||
|
PenaltyReturnTypeOnItsOwnLine: 60
|
||||||
|
PenaltyIndentedWhitespace: 0
|
||||||
|
PointerAlignment: Left
|
||||||
|
PPIndentWidth: -1
|
||||||
|
ReferenceAlignment: Pointer
|
||||||
|
ReflowComments: true
|
||||||
|
RemoveBracesLLVM: false
|
||||||
|
RequiresClausePosition: OwnLine
|
||||||
|
SeparateDefinitionBlocks: Leave
|
||||||
|
ShortNamespaceLines: 1
|
||||||
|
SortIncludes: CaseSensitive
|
||||||
|
SortJavaStaticImport: Before
|
||||||
|
SortUsingDeclarations: true
|
||||||
|
SpaceAfterCStyleCast: false
|
||||||
|
SpaceAfterLogicalNot: false
|
||||||
|
SpaceAfterTemplateKeyword: true
|
||||||
|
SpaceBeforeAssignmentOperators: true
|
||||||
|
SpaceBeforeCaseColon: false
|
||||||
|
SpaceBeforeCpp11BracedList: false
|
||||||
|
SpaceBeforeCtorInitializerColon: true
|
||||||
|
SpaceBeforeInheritanceColon: true
|
||||||
|
SpaceBeforeParens: ControlStatements
|
||||||
|
SpaceBeforeParensOptions:
|
||||||
|
AfterControlStatements: true
|
||||||
|
AfterForeachMacros: true
|
||||||
|
AfterFunctionDefinitionName: false
|
||||||
|
AfterFunctionDeclarationName: false
|
||||||
|
AfterIfMacros: true
|
||||||
|
AfterOverloadedOperator: false
|
||||||
|
AfterRequiresInClause: false
|
||||||
|
AfterRequiresInExpression: false
|
||||||
|
BeforeNonEmptyParentheses: false
|
||||||
|
SpaceAroundPointerQualifiers: Default
|
||||||
|
SpaceBeforeRangeBasedForLoopColon: true
|
||||||
|
SpaceInEmptyBlock: false
|
||||||
|
SpaceInEmptyParentheses: false
|
||||||
|
SpacesBeforeTrailingComments: 1
|
||||||
|
SpacesInAngles: Never
|
||||||
|
SpacesInConditionalStatement: false
|
||||||
|
SpacesInContainerLiterals: true
|
||||||
|
SpacesInCStyleCastParentheses: false
|
||||||
|
SpacesInLineCommentPrefix:
|
||||||
|
Minimum: 1
|
||||||
|
Maximum: -1
|
||||||
|
SpacesInParentheses: false
|
||||||
|
SpacesInSquareBrackets: false
|
||||||
|
SpaceBeforeSquareBrackets: false
|
||||||
|
BitFieldColonSpacing: Both
|
||||||
|
Standard: Latest
|
||||||
|
StatementAttributeLikeMacros:
|
||||||
|
- Q_EMIT
|
||||||
|
StatementMacros:
|
||||||
|
- Q_UNUSED
|
||||||
|
- QT_REQUIRE_VERSION
|
||||||
|
TabWidth: 8
|
||||||
|
UseCRLF: false
|
||||||
|
UseTab: Never
|
||||||
|
WhitespaceSensitiveMacros:
|
||||||
|
- STRINGIZE
|
||||||
|
- PP_STRINGIZE
|
||||||
|
- BOOST_PP_STRINGIZE
|
||||||
|
- NS_SWIFT_NAME
|
||||||
|
- CF_SWIFT_NAME
|
||||||
|
...
|
||||||
|
|
||||||
16
.vscode/launch.json
vendored
Normal file
16
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Debug",
|
||||||
|
"program": "${workspaceFolder}/bin/main",
|
||||||
|
"args": [],
|
||||||
|
"cwd": "${workspaceFolder}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
18
.vscode/tasks.json
vendored
18
.vscode/tasks.json
vendored
@@ -3,24 +3,12 @@
|
|||||||
{
|
{
|
||||||
"type": "cppbuild",
|
"type": "cppbuild",
|
||||||
"label": "C/C++: clang сборка активного файла",
|
"label": "C/C++: clang сборка активного файла",
|
||||||
"command": "/usr/bin/clang",
|
"command": "sh",
|
||||||
"args": [
|
"args": [
|
||||||
"-fcolor-diagnostics",
|
"${workspaceFolder}/build.sh"
|
||||||
"-fansi-escape-codes",
|
|
||||||
"-g",
|
|
||||||
"${file}",
|
|
||||||
"${fileDirname}/utils.c",
|
|
||||||
"${fileDirname}/ring_buffer.c",
|
|
||||||
"${fileDirname}/oscillator.c",
|
|
||||||
"${fileDirname}/parser.c",
|
|
||||||
"${fileDirname}/export.c",
|
|
||||||
"-lm",
|
|
||||||
"-lraylib",
|
|
||||||
"-o",
|
|
||||||
"${fileDirname}/bin/${fileBasenameNoExtension}"
|
|
||||||
],
|
],
|
||||||
"options": {
|
"options": {
|
||||||
"cwd": "${fileDirname}"
|
"cwd": "${workspaceFolder}"
|
||||||
},
|
},
|
||||||
"problemMatcher": [
|
"problemMatcher": [
|
||||||
"$gcc"
|
"$gcc"
|
||||||
|
|||||||
2
build.sh
2
build.sh
@@ -1,5 +1,5 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
CC="${CXX:-c++}"
|
CC="${CXX:-c++}"
|
||||||
LL="-lm -lraylib"
|
LL="-lm -lraylib"
|
||||||
FLAGS="-Wall -std=c++17 -I./inc/"
|
FLAGS="-Wall -std=c++17 -I./inc/ -g"
|
||||||
$CC $FLAGS $(find ./src -type f -iregex ".*\.cpp") $LL -o ./bin/main
|
$CC $FLAGS $(find ./src -type f -iregex ".*\.cpp") $LL -o ./bin/main
|
||||||
|
|||||||
BIN
docs/ADSR States.png
Normal file
BIN
docs/ADSR States.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 276 KiB |
@@ -26,6 +26,7 @@ release_samples = int(release_time * sample_rate)
|
|||||||
|
|
||||||
# Attack phase
|
# Attack phase
|
||||||
envelope[:attack_samples] = np.linspace(0, 1, num=attack_samples)
|
envelope[:attack_samples] = np.linspace(0, 1, num=attack_samples)
|
||||||
|
# 1/n * count;
|
||||||
|
|
||||||
# Decay phase
|
# Decay phase
|
||||||
decay_slope = (1 - sustain_level) / decay_samples
|
decay_slope = (1 - sustain_level) / decay_samples
|
||||||
|
|||||||
BIN
docs/Attack Formula.png
Normal file
BIN
docs/Attack Formula.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 76 KiB |
37
inc/ADSR.h
Normal file
37
inc/ADSR.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Effect.h"
|
||||||
|
#include "Ramp.h"
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
struct ADSRParameters {
|
||||||
|
float attack_time; // Attack time in seconds
|
||||||
|
float decay_time; // Decay time in seconds
|
||||||
|
float sustain_level; // Sustain level (0 to 1)
|
||||||
|
float release_time;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ADSRState { Off, Attack, Decay, Sustain, Release };
|
||||||
|
|
||||||
|
class ADSR : public Effect {
|
||||||
|
private:
|
||||||
|
ADSRParameters m_parameters;
|
||||||
|
ADSRState m_state;
|
||||||
|
Ramp* m_ramp;
|
||||||
|
|
||||||
|
void process_sample(float* sample);
|
||||||
|
bool is_attack_elapsed();
|
||||||
|
bool is_decay_elapsed();
|
||||||
|
bool is_release_elapsed();
|
||||||
|
void recheck_state();
|
||||||
|
|
||||||
|
public:
|
||||||
|
ADSR(/* args */);
|
||||||
|
ADSR(ADSRParameters param);
|
||||||
|
~ADSR();
|
||||||
|
void OnSetNote() override;
|
||||||
|
void OnUnsetNote() override;
|
||||||
|
// void RetriggerState() override;
|
||||||
|
void Process(std::vector<float>& samples) override;
|
||||||
|
void Reset();
|
||||||
|
void SetParameters(float attack, float decay, float sustain, float release);
|
||||||
|
};
|
||||||
28
inc/Adder.h
28
inc/Adder.h
@@ -1,30 +1,26 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include "Oscillator.h"
|
#include "Oscillator.h"
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
struct Adder
|
struct Adder {
|
||||||
{
|
static void SumOscillators(const std::vector<Oscillator*>& oscillators,
|
||||||
static std::vector<float> & SumOscillators(const std::vector<Oscillator*> & oscillators, float duration)
|
std::vector<float>& signal) {
|
||||||
{
|
size_t sample_count =
|
||||||
size_t sample_count = (size_t)(duration * SAMPLE_RATE);
|
STREAM_BUFFER_SIZE; //(size_t)(1.f/FPS * SAMPLE_RATE);
|
||||||
|
|
||||||
std::vector<float>* output = new std::vector<float>();
|
// std::vector<float>* output = new std::vector<float>();
|
||||||
output->reserve(sample_count);
|
// output->reserve(sample_count);
|
||||||
|
|
||||||
for (size_t i = 0; i < sample_count; i++)
|
for (size_t i = 0; i < sample_count; i++) {
|
||||||
{
|
|
||||||
float sample = 0.0f;
|
float sample = 0.0f;
|
||||||
for (Oscillator* osc : oscillators)
|
for (Oscillator* osc : oscillators) {
|
||||||
{
|
sample += osc->GenerateSample(1.f);
|
||||||
sample += osc->GenerateSample(duration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
output->push_back(sample);
|
signal[i] = sample;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (*output);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,32 +1,26 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Note.h"
|
#include "Note.h"
|
||||||
#include "Synth.h"
|
|
||||||
#include "raylib.h"
|
|
||||||
#include "RingBuffer.h"
|
|
||||||
#include "Renderer.h"
|
#include "Renderer.h"
|
||||||
|
#include "Synth.h"
|
||||||
#include "SynthGuiState.h"
|
#include "SynthGuiState.h"
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
class Application
|
class Application {
|
||||||
{
|
private:
|
||||||
private:
|
|
||||||
Synth m_synth;
|
Synth m_synth;
|
||||||
SynthGuiState m_synth_gui_state;
|
SynthGuiState m_synth_gui_state;
|
||||||
RingBuffer<float>* m_ring_buffer;
|
|
||||||
AudioStream m_synth_stream;
|
AudioStream m_synth_stream;
|
||||||
int m_sound_played_count;
|
int m_sound_played_count;
|
||||||
float* m_temp_buffer;
|
|
||||||
Note* m_current_note;
|
Note* m_current_note;
|
||||||
Renderer m_renderer;
|
Renderer m_renderer;
|
||||||
std::size_t detect_note_pressed(Note* note);
|
bool detect_note_pressed(Note* note);
|
||||||
void init_synth();
|
void init_synth();
|
||||||
void init_audio();
|
void init_audio();
|
||||||
void update_on_note_input();
|
void update_on_note_input();
|
||||||
void play_buffered_audio();
|
void play_buffered_audio();
|
||||||
void fill_audio_buffer();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Application(/* args */);
|
Application(/* args */);
|
||||||
~Application();
|
~Application();
|
||||||
void Run();
|
void Run();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
13
inc/Effect.h
Normal file
13
inc/Effect.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
class Effect {
|
||||||
|
private:
|
||||||
|
/* data */
|
||||||
|
public:
|
||||||
|
Effect(/* args */){};
|
||||||
|
~Effect(){};
|
||||||
|
virtual void OnSetNote(){};
|
||||||
|
virtual void OnUnsetNote(){};
|
||||||
|
// virtual void RetriggerState(){};
|
||||||
|
virtual void Process(std::vector<float>& samples){};
|
||||||
|
};
|
||||||
@@ -1,22 +1,23 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class KeyBoard
|
class KeyBoard {
|
||||||
{
|
private:
|
||||||
private:
|
|
||||||
/* data */
|
/* data */
|
||||||
static int get_semitone_shift_internal(const char* root_note, char* target_note) {
|
static int get_semitone_shift_internal(const char* root_note,
|
||||||
const char* pitch_classes[12] =
|
char* target_note) {
|
||||||
{ "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
|
const char* pitch_classes[12] = {"C", "C#", "D", "D#", "E", "F",
|
||||||
|
"F#", "G", "G#", "A", "A#", "B"};
|
||||||
|
|
||||||
// Extract the note number and pitch class for the root note
|
// Extract the note number and pitch class for the root note
|
||||||
int root_note_num = (int)root_note[strlen(root_note) - 1] - '0';
|
int root_note_num = (int)root_note[strlen(root_note) - 1] - '0';
|
||||||
|
|
||||||
char* root_pitch_class_str = (char*)malloc((strlen(root_note) - 1) * sizeof(char));
|
char* root_pitch_class_str =
|
||||||
|
(char*)malloc((strlen(root_note) - 1) * sizeof(char));
|
||||||
strncpy(root_pitch_class_str, root_note, strlen(root_note) - 1);
|
strncpy(root_pitch_class_str, root_note, strlen(root_note) - 1);
|
||||||
|
|
||||||
int root_pitch_class = -1;
|
int root_pitch_class = -1;
|
||||||
@@ -50,9 +51,10 @@ private:
|
|||||||
|
|
||||||
// Calculate the semitone shift using the formula
|
// Calculate the semitone shift using the formula
|
||||||
return (target_note_num - root_note_num) * 12 +
|
return (target_note_num - root_note_num) * 12 +
|
||||||
(target_pitch_class - root_pitch_class);
|
(target_pitch_class - root_pitch_class);
|
||||||
}
|
}
|
||||||
public:
|
|
||||||
|
public:
|
||||||
KeyBoard(/* args */);
|
KeyBoard(/* args */);
|
||||||
~KeyBoard();
|
~KeyBoard();
|
||||||
|
|
||||||
@@ -71,10 +73,6 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
KeyBoard::KeyBoard(/* args */)
|
KeyBoard::KeyBoard(/* args */) {}
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
KeyBoard::~KeyBoard()
|
KeyBoard::~KeyBoard() {}
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,40 +1,35 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include<vector>
|
|
||||||
#include "OscillatorType.h"
|
#include "OscillatorType.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class Oscillator {
|
||||||
|
private:
|
||||||
|
OscillatorType m_osc;
|
||||||
|
float m_freq;
|
||||||
|
float m_volume;
|
||||||
|
float m_phase;
|
||||||
|
float m_phase_dt;
|
||||||
|
float (Oscillator::*m_osc_function)(void);
|
||||||
|
float (Oscillator::*m_dt_function)(float freq);
|
||||||
|
void sine_osc_phase_incr();
|
||||||
|
void saw_osc_phase_incr();
|
||||||
|
float calc_saw_phase_delta(float freq);
|
||||||
|
float calc_sine_phase_delta(float freq);
|
||||||
|
float sawosc();
|
||||||
|
float triangleosc();
|
||||||
|
float squareosc();
|
||||||
|
float sign(float v);
|
||||||
|
float sineosc();
|
||||||
|
|
||||||
class Oscillator
|
public:
|
||||||
{
|
Oscillator(OscillatorType osc, float freq, float volume);
|
||||||
//typedef float (Oscillator::*OscFunction)(void);
|
~Oscillator();
|
||||||
//typedef float (Oscillator::*DtFunction)(float);
|
OscillatorType GetType() { return m_osc; }
|
||||||
|
void SetType(OscillatorType osc);
|
||||||
private:
|
float GetVolume() { return m_volume; }
|
||||||
OscillatorType m_osc;
|
void SetVolume(float volume) { m_volume = volume; }
|
||||||
float m_freq;
|
float GetFreq() { return m_freq; }
|
||||||
float m_volume;
|
void SetFreq(float freq);
|
||||||
float m_phase;
|
void Reset();
|
||||||
float m_phase_dt;
|
float GenerateSample(float duration);
|
||||||
//значение типа "float (Oscillator::*)()" нельзя присвоить сущности типа "float (*)()"
|
|
||||||
float (Oscillator::*m_osc_function)(void);
|
|
||||||
float (Oscillator::*m_dt_function)(float freq);
|
|
||||||
void sine_osc_phase_incr();
|
|
||||||
void saw_osc_phase_incr();
|
|
||||||
float calc_saw_phase_delta(float freq);
|
|
||||||
float calc_sine_phase_delta(float freq);
|
|
||||||
float sawosc();
|
|
||||||
float triangleosc();
|
|
||||||
float squareosc();
|
|
||||||
float sign(float v);
|
|
||||||
float sineosc();
|
|
||||||
public:
|
|
||||||
Oscillator(OscillatorType osc, float freq, float volume);
|
|
||||||
~Oscillator();
|
|
||||||
OscillatorType GetType() { return m_osc; }
|
|
||||||
void SetType(OscillatorType osc);
|
|
||||||
float GetVolume() { return m_volume; }
|
|
||||||
void SetVolume(float volume) { m_volume = volume; }
|
|
||||||
float GetFreq() { return m_freq; }
|
|
||||||
void SetFreq(float freq);
|
|
||||||
void Reset();
|
|
||||||
float GenerateSample(float duration);
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,2 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
typedef enum {
|
typedef enum { Sine, Triangle, Saw, Square } OscillatorType;
|
||||||
Sine,
|
|
||||||
Triangle,
|
|
||||||
Saw,
|
|
||||||
Square
|
|
||||||
} OscillatorType;
|
|
||||||
16
inc/Ramp.h
Normal file
16
inc/Ramp.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
class Ramp {
|
||||||
|
private:
|
||||||
|
float m_level;
|
||||||
|
float m_sample_rate;
|
||||||
|
float m_increment;
|
||||||
|
int m_counter;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Ramp(float starting_level, float sample_rate);
|
||||||
|
~Ramp();
|
||||||
|
void RampTo(float value, float time);
|
||||||
|
float Process();
|
||||||
|
bool IsCompleted();
|
||||||
|
};
|
||||||
@@ -1,27 +1,29 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "ADSR.h"
|
||||||
#include "Synth.h"
|
#include "Synth.h"
|
||||||
#include "SynthGuiState.h"
|
#include "SynthGuiState.h"
|
||||||
#include <vector>
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class Renderer
|
class Renderer {
|
||||||
{
|
private:
|
||||||
private:
|
void draw_main_panel(const Rectangle& panel_bounds);
|
||||||
void DrawMainPanel(const Rectangle& panel_bounds);
|
void draw_add_oscillator_button(Synth& synth, SynthGuiState& synth_gui,
|
||||||
void DrawAddOscillatorButton(Synth & synth, SynthGuiState & synthGui, Rectangle panel_bounds);
|
Rectangle panel_bounds);
|
||||||
void DrawOscillatorsPanels(const std::vector<Oscillator*>& oscillators,
|
float draw_oscillators_panels(
|
||||||
const std::vector<OscillatorGuiState*>& guiOscillators,
|
const std::vector<Oscillator*>& oscillators,
|
||||||
|
const std::vector<OscillatorGuiState*>& gui_oscillators,
|
||||||
const Rectangle& panel_bounds);
|
const Rectangle& panel_bounds);
|
||||||
void DrawOscillatorsShapeInputs(const std::vector<Oscillator*>& oscillators,
|
void draw_oscillators_shape_inputs(
|
||||||
|
const std::vector<Oscillator*>& oscillators,
|
||||||
const std::vector<OscillatorGuiState*>& guiOscillators);
|
const std::vector<OscillatorGuiState*>& guiOscillators);
|
||||||
void DrawUi(Synth & synth, SynthGuiState & synthGui);
|
void draw_ui(Synth& synth, SynthGuiState& synth_gui);
|
||||||
void DrawSignal(Synth & synth, SynthGuiState & synthGui);
|
void draw_signal(Synth& synth, SynthGuiState& synth_gui);
|
||||||
public:
|
void draw_adsr_panel(ADSR* adsr, ADSRGuiState& gui_adsr,
|
||||||
|
const Rectangle& panel_bounds, float panel_y_offset);
|
||||||
|
|
||||||
|
public:
|
||||||
Renderer(/* args */);
|
Renderer(/* args */);
|
||||||
~Renderer();
|
~Renderer();
|
||||||
void Draw(Synth& synth, SynthGuiState & synthGui);
|
void Draw(Synth& synth, SynthGuiState& synth_gui);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
100
inc/RingBuffer.h
100
inc/RingBuffer.h
@@ -1,10 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <cstddef>
|
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
template <typename T>
|
#include <cstddef>
|
||||||
class RingBuffer
|
template <typename T> class RingBuffer {
|
||||||
{
|
private:
|
||||||
private:
|
|
||||||
T* m_items; /* data */
|
T* m_items; /* data */
|
||||||
std::size_t m_head;
|
std::size_t m_head;
|
||||||
std::size_t m_tail;
|
std::size_t m_tail;
|
||||||
@@ -13,7 +11,8 @@ private:
|
|||||||
std::size_t m_size;
|
std::size_t m_size;
|
||||||
void advance_pointer();
|
void advance_pointer();
|
||||||
void retreat_pointer();
|
void retreat_pointer();
|
||||||
public:
|
|
||||||
|
public:
|
||||||
RingBuffer(std::size_t size);
|
RingBuffer(std::size_t size);
|
||||||
~RingBuffer();
|
~RingBuffer();
|
||||||
bool IsFull() { return m_is_full; }
|
bool IsFull() { return m_is_full; }
|
||||||
@@ -26,8 +25,7 @@ public:
|
|||||||
void Print();
|
void Print();
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T> RingBuffer<T>::RingBuffer(std::size_t size)
|
template <typename T> RingBuffer<T>::RingBuffer(std::size_t size) {
|
||||||
{
|
|
||||||
m_items = new T[size];
|
m_items = new T[size];
|
||||||
m_head = 0;
|
m_head = 0;
|
||||||
m_tail = 0;
|
m_tail = 0;
|
||||||
@@ -36,50 +34,42 @@ template <typename T> RingBuffer<T>::RingBuffer(std::size_t size)
|
|||||||
m_size = size;
|
m_size = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> RingBuffer<T>::~RingBuffer()
|
template <typename T> RingBuffer<T>::~RingBuffer() { delete[] m_items; }
|
||||||
{
|
|
||||||
delete[] m_items;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> void RingBuffer<T>::Reset()
|
template <typename T> void RingBuffer<T>::Reset() {
|
||||||
{
|
|
||||||
m_head = 0;
|
m_head = 0;
|
||||||
m_tail = 0;
|
m_tail = 0;
|
||||||
m_is_full = 0;
|
m_is_full = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void RingBuffer<T>::advance_pointer()
|
template <typename T> void RingBuffer<T>::advance_pointer() {
|
||||||
{
|
|
||||||
if (m_is_full) {
|
if (m_is_full) {
|
||||||
m_tail++;
|
m_tail++;
|
||||||
if (m_tail == m_size) {
|
if (m_tail == m_size) {
|
||||||
m_tail = 0;
|
m_tail = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_head++;
|
m_head++;
|
||||||
if (m_head == m_size) {
|
if (m_head == m_size) {
|
||||||
m_head = 0;
|
m_head = 0;
|
||||||
}
|
}
|
||||||
std::size_t p_is_full = m_head == m_tail ? 1 : 0;
|
std::size_t p_is_full = m_head == m_tail ? 1 : 0;
|
||||||
m_is_full = p_is_full;
|
m_is_full = p_is_full;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void RingBuffer<T>::retreat_pointer()
|
template <typename T> void RingBuffer<T>::retreat_pointer() {
|
||||||
{
|
m_is_full = 0;
|
||||||
m_is_full = 0;
|
|
||||||
m_tail++;
|
m_tail++;
|
||||||
if (m_tail == m_size) {
|
if (m_tail == m_size) {
|
||||||
m_tail = 0;
|
m_tail = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void RingBuffer<T>::Write(T* data, std::size_t count)
|
template <typename T> void RingBuffer<T>::Write(T* data, std::size_t count) {
|
||||||
{
|
|
||||||
if (m_is_full || m_head + count > m_size) {
|
if (m_is_full || m_head + count > m_size) {
|
||||||
write_log("[WARN] Trying to overfill the ring buffer: \n\tIsFull:%d\n\tHead:%zu\n\tCount:%zu\n\t",
|
write_log("[WARN] Trying to overfill the ring buffer: "
|
||||||
m_is_full,
|
"\n\tIsFull:%d\n\tHead:%zu\n\tCount:%zu\n\t",
|
||||||
m_head,
|
m_is_full, m_head, count);
|
||||||
count);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_is_empty = 0;
|
m_is_empty = 0;
|
||||||
@@ -88,11 +78,10 @@ template <typename T> void RingBuffer<T>::Write(T* data, std::size_t count)
|
|||||||
m_items[m_head] = data[i];
|
m_items[m_head] = data[i];
|
||||||
advance_pointer();
|
advance_pointer();
|
||||||
}
|
}
|
||||||
//m_is_empty = m_is_full && (m_head == m_tail);
|
// m_is_empty = m_is_full && (m_head == m_tail);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> bool RingBuffer<T>::Read(T* output, std::size_t count)
|
template <typename T> bool RingBuffer<T>::Read(T* output, std::size_t count) {
|
||||||
{
|
|
||||||
if (m_is_empty) {
|
if (m_is_empty) {
|
||||||
write_log("[WARN] Trying to read empty buffer");
|
write_log("[WARN] Trying to read empty buffer");
|
||||||
return 0;
|
return 0;
|
||||||
@@ -106,26 +95,21 @@ template <typename T> bool RingBuffer<T>::Read(T* output, std::size_t count)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> std::size_t RingBuffer<T>::GetSize()
|
template <typename T> std::size_t RingBuffer<T>::GetSize() {
|
||||||
{
|
size_t p_size = m_size;
|
||||||
size_t p_size = m_size;
|
if (!m_is_full) {
|
||||||
if(!m_is_full) {
|
if (m_head >= m_tail) {
|
||||||
if(m_head >= m_tail) {
|
p_size = (m_head - m_tail);
|
||||||
p_size = (m_head - m_tail);
|
} else {
|
||||||
}
|
p_size = (m_size + m_head - m_tail);
|
||||||
else {
|
}
|
||||||
p_size = (m_size + m_head - m_tail);
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return p_size;
|
return p_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void RingBuffer<T>::Print()
|
template <typename T> void RingBuffer<T>::Print() {
|
||||||
{
|
write_log("[INFO] The ring buffer: "
|
||||||
write_log("[INFO] The ring buffer: \n\tIsFull:%d\n\tIsEmpty:%d\n\tHead:%zu\n\tTail:%zu\n\t",
|
"\n\tIsFull:%d\n\tIsEmpty:%d\n\tHead:%zu\n\tTail:%zu\n\t",
|
||||||
m_is_full,
|
m_is_full, m_is_empty, m_head, m_tail);
|
||||||
m_is_empty,
|
|
||||||
m_head,
|
|
||||||
m_tail);
|
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define SAMPLE_RATE 48000.f
|
#define SAMPLE_RATE 44100.f
|
||||||
#define BPM 120.f
|
#define BPM 120.f
|
||||||
#define BEAT_DURATION 60.f/BPM
|
#define BEAT_DURATION 60.f / BPM
|
||||||
#define PITCH_STANDARD 440.f
|
#define PITCH_STANDARD 440.f
|
||||||
#define VOLUME 0.5f
|
#define VOLUME 0.5f
|
||||||
#define ATTACK_MS 100.f
|
#define ATTACK_MS 100.f
|
||||||
#define STREAM_BUFFER_SIZE 4096
|
#define STREAM_BUFFER_SIZE 1024
|
||||||
|
#define FPS 60
|
||||||
|
|
||||||
#define SYNTH_PI 3.1415926535f
|
#define SYNTH_PI 3.1415926535f
|
||||||
#define SYNTH_VOLUME 0.5f
|
#define SYNTH_VOLUME 0.5f
|
||||||
|
|||||||
37
inc/Synth.h
37
inc/Synth.h
@@ -1,26 +1,37 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
#include "ADSR.h"
|
||||||
#include "Oscillator.h"
|
|
||||||
#include "Note.h"
|
|
||||||
#include "Adder.h"
|
#include "Adder.h"
|
||||||
|
#include "Effect.h"
|
||||||
|
#include "Note.h"
|
||||||
|
#include "Oscillator.h"
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class Synth
|
class Synth {
|
||||||
{
|
private:
|
||||||
private:
|
bool is_note_triggered;
|
||||||
std::vector<Oscillator*> m_oscillators;
|
std::vector<Oscillator*> m_oscillators;
|
||||||
Adder m_adder;
|
std::vector<Effect*> m_effects;
|
||||||
//OscillatorUI* ui_oscillators;
|
// OscillatorUI* ui_oscillators;
|
||||||
//Note m_current_note;
|
// Note m_current_note;
|
||||||
std::vector<float> m_out_signal;
|
std::vector<float> m_out_signal;
|
||||||
std::vector<float> & get_note(int semitone, float beats);
|
void zero_signal();
|
||||||
|
void get_note();
|
||||||
|
void trigger_note_on_effects();
|
||||||
|
void untrigger_note_on_effects();
|
||||||
|
void apply_effects();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Synth(/* args */);
|
Synth(/* args */);
|
||||||
~Synth();
|
~Synth();
|
||||||
void ProduceNoteSound(Note input);
|
void TriggerNote(Note input);
|
||||||
|
void ProduceSound();
|
||||||
|
void StopSound();
|
||||||
void AddOscillator();
|
void AddOscillator();
|
||||||
const std::vector<float> & GetOutSignal() { return m_out_signal; }
|
void AddEffect(Effect* fx);
|
||||||
|
const std::vector<float>& GetOutSignal() { return m_out_signal; }
|
||||||
const std::vector<Oscillator*>& GetOscillators() { return m_oscillators; }
|
const std::vector<Oscillator*>& GetOscillators() { return m_oscillators; }
|
||||||
|
const bool& GetIsNoteTriggered() { return is_note_triggered; }
|
||||||
|
ADSR* GetADSR() { return (ADSR*)m_effects[0]; }
|
||||||
};
|
};
|
||||||
@@ -5,12 +5,20 @@
|
|||||||
|
|
||||||
struct OscillatorGuiState {
|
struct OscillatorGuiState {
|
||||||
float volume;
|
float volume;
|
||||||
float freq;//todo: remove or change to pitch shift
|
float freq; // todo: remove or change to pitch shift
|
||||||
OscillatorType waveshape;
|
OscillatorType waveshape;
|
||||||
bool is_dropdown_open;
|
bool is_dropdown_open;
|
||||||
Rectangle shape_dropdown_rect;
|
Rectangle shape_dropdown_rect;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct ADSRGuiState {
|
||||||
|
float attack;
|
||||||
|
float decay;
|
||||||
|
float sustain;
|
||||||
|
float release;
|
||||||
|
};
|
||||||
|
|
||||||
struct SynthGuiState {
|
struct SynthGuiState {
|
||||||
std::vector<OscillatorGuiState*> oscillators;
|
std::vector<OscillatorGuiState*> oscillators;
|
||||||
|
ADSRGuiState adsr;
|
||||||
};
|
};
|
||||||
97
src/ADSR.cpp
Normal file
97
src/ADSR.cpp
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
#include "ADSR.h"
|
||||||
|
#include "Logger.h"
|
||||||
|
#include "Settings.h"
|
||||||
|
|
||||||
|
ADSR::ADSR(/* args */) {
|
||||||
|
m_parameters.attack_time = 1.f;
|
||||||
|
m_parameters.decay_time = 0.4f;
|
||||||
|
m_parameters.sustain_level = 0.6f;
|
||||||
|
m_parameters.release_time = 0.8f;
|
||||||
|
m_ramp = new Ramp(0, SAMPLE_RATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
ADSR::ADSR(ADSRParameters param) { m_parameters = param; }
|
||||||
|
|
||||||
|
ADSR::~ADSR() { delete m_ramp; }
|
||||||
|
|
||||||
|
bool ADSR::is_attack_elapsed() {
|
||||||
|
return m_state == Attack && m_ramp->IsCompleted();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ADSR::is_decay_elapsed() {
|
||||||
|
return m_state == Decay && m_ramp->IsCompleted();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ADSR::is_release_elapsed() {
|
||||||
|
return m_state == Release && m_ramp->IsCompleted();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ADSR::recheck_state() {
|
||||||
|
switch (m_state) {
|
||||||
|
case Attack:
|
||||||
|
if (is_attack_elapsed()) {
|
||||||
|
m_state = Decay;
|
||||||
|
m_ramp->RampTo(m_parameters.sustain_level,
|
||||||
|
m_parameters.decay_time);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Decay:
|
||||||
|
if (is_decay_elapsed()) {
|
||||||
|
m_state = Sustain;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Release:
|
||||||
|
if (is_release_elapsed()) {
|
||||||
|
m_state = Off;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ADSR::process_sample(float* sample) {
|
||||||
|
if (m_state == Off) {
|
||||||
|
(*sample) = 0;
|
||||||
|
} else if (m_state == Attack) {
|
||||||
|
(*sample) = (*sample) * m_ramp->Process();
|
||||||
|
} else if (m_state == Decay) {
|
||||||
|
(*sample) = (*sample) * m_ramp->Process();
|
||||||
|
} else if (m_state == Sustain) {
|
||||||
|
(*sample) = (*sample) * m_parameters.sustain_level;
|
||||||
|
} else if (m_state == Release) {
|
||||||
|
(*sample) = (*sample) * m_ramp->Process();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ADSR::OnSetNote() {
|
||||||
|
write_log("Set ADSR\n");
|
||||||
|
if (m_state == Off) {
|
||||||
|
m_state = Attack;
|
||||||
|
} else if (m_state == Release) {
|
||||||
|
m_state = Attack;
|
||||||
|
};
|
||||||
|
|
||||||
|
m_ramp->RampTo(1, m_parameters.attack_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ADSR::OnUnsetNote() {
|
||||||
|
write_log("Unset ADSR\n");
|
||||||
|
m_state = Release;
|
||||||
|
m_ramp->RampTo(0, m_parameters.release_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ADSR::Process(std::vector<float>& samples) {
|
||||||
|
for (std::size_t i = 0; i < samples.size(); i++) {
|
||||||
|
recheck_state();
|
||||||
|
process_sample(&samples[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ADSR::SetParameters(float attack, float decay, float sustain,
|
||||||
|
float release) {
|
||||||
|
m_parameters.attack_time = attack;
|
||||||
|
m_parameters.decay_time = decay;
|
||||||
|
m_parameters.sustain_level = sustain;
|
||||||
|
m_parameters.release_time = release;
|
||||||
|
}
|
||||||
@@ -1,32 +1,25 @@
|
|||||||
#include "Application.h"
|
#include "Application.h"
|
||||||
#include "Settings.h"
|
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
|
#include "Settings.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
Application::Application(/* args */)
|
Application::Application(/* args */) {
|
||||||
{
|
|
||||||
m_ring_buffer = new RingBuffer<float>((std::size_t)STREAM_BUFFER_SIZE);
|
|
||||||
m_temp_buffer = new float[STREAM_BUFFER_SIZE];
|
|
||||||
init_synth();
|
init_synth();
|
||||||
init_audio();
|
init_audio();
|
||||||
}
|
}
|
||||||
|
|
||||||
Application::~Application()
|
Application::~Application() {
|
||||||
{
|
|
||||||
StopAudioStream(m_synth_stream);
|
StopAudioStream(m_synth_stream);
|
||||||
UnloadAudioStream(m_synth_stream);
|
UnloadAudioStream(m_synth_stream);
|
||||||
CloseAudioDevice();
|
CloseAudioDevice();
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
delete m_ring_buffer;
|
|
||||||
delete[] m_temp_buffer;
|
|
||||||
// todo: move to gui state class destructor (make it a class)
|
// todo: move to gui state class destructor (make it a class)
|
||||||
for(int i = 0; i < m_synth_gui_state.oscillators.size(); i++) {
|
for (int i = 0; i < m_synth_gui_state.oscillators.size(); i++) {
|
||||||
delete m_synth_gui_state.oscillators[i];
|
delete m_synth_gui_state.oscillators[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::init_audio()
|
void Application::init_audio() {
|
||||||
{
|
|
||||||
m_sound_played_count = 0;
|
m_sound_played_count = 0;
|
||||||
|
|
||||||
InitAudioDevice();
|
InitAudioDevice();
|
||||||
@@ -38,147 +31,101 @@ void Application::init_audio()
|
|||||||
PlayAudioStream(m_synth_stream);
|
PlayAudioStream(m_synth_stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::init_synth()
|
void Application::init_synth() {
|
||||||
{
|
// todo: move that variables to Synth declaration
|
||||||
//todo: move that variables to Synth declaration
|
|
||||||
std::string* nameString = new std::string(std::string(new char[3]));
|
std::string* nameString = new std::string(std::string(new char[3]));
|
||||||
m_current_note = new Note
|
m_current_note = new Note{.length = 1, .name = (*nameString)};
|
||||||
{
|
m_current_note->name.assign("G4");
|
||||||
.length = 1,
|
// todo: move somewhere in initialization
|
||||||
.name = (*nameString)
|
|
||||||
};
|
|
||||||
|
|
||||||
//todo: move somewhere in initialization
|
|
||||||
std::vector<Oscillator*> oscillators = m_synth.GetOscillators();
|
std::vector<Oscillator*> oscillators = m_synth.GetOscillators();
|
||||||
m_synth_gui_state.oscillators.reserve(oscillators.size());
|
m_synth_gui_state.oscillators.reserve(oscillators.size());
|
||||||
for (size_t i = 0; i < oscillators.size(); i++)
|
for (size_t i = 0; i < oscillators.size(); i++) {
|
||||||
{
|
|
||||||
Oscillator* osc = oscillators[i];
|
Oscillator* osc = oscillators[i];
|
||||||
assert(osc);
|
assert(osc);
|
||||||
|
|
||||||
OscillatorGuiState* ui = new OscillatorGuiState {
|
OscillatorGuiState* ui =
|
||||||
.freq = osc->GetFreq(),
|
new OscillatorGuiState{.freq = osc->GetFreq(),
|
||||||
.waveshape = osc->GetType(),
|
.waveshape = osc->GetType(),
|
||||||
.volume = osc->GetVolume()
|
.volume = osc->GetVolume()};
|
||||||
};
|
|
||||||
m_synth_gui_state.oscillators.push_back(ui);
|
m_synth_gui_state.oscillators.push_back(ui);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t Application::detect_note_pressed(Note* note)
|
bool Application::detect_note_pressed(Note* note) {
|
||||||
{
|
|
||||||
std::size_t is_pressed = 0;
|
std::size_t is_pressed = 0;
|
||||||
note->length = 8;
|
note->length = 8;
|
||||||
if (IsKeyPressed(KEY_A))
|
if (IsKeyDown(KEY_A)) {
|
||||||
{
|
note->name.assign("A2");
|
||||||
note->name.assign("A4");
|
|
||||||
is_pressed = 1;
|
is_pressed = 1;
|
||||||
}
|
}
|
||||||
if (IsKeyPressed(KEY_B))
|
if (IsKeyDown(KEY_B)) {
|
||||||
{
|
note->name.assign("B2");
|
||||||
note->name.assign("B4");
|
|
||||||
is_pressed = 1;
|
is_pressed = 1;
|
||||||
}
|
}
|
||||||
if (IsKeyPressed(KEY_C))
|
if (IsKeyDown(KEY_C)) {
|
||||||
{
|
note->name.assign("C2");
|
||||||
note->name.assign("C4");
|
|
||||||
is_pressed = 1;
|
is_pressed = 1;
|
||||||
}
|
}
|
||||||
if (IsKeyPressed(KEY_D))
|
if (IsKeyDown(KEY_D)) {
|
||||||
{
|
note->name.assign("D2");
|
||||||
note->name.assign("D4");
|
|
||||||
is_pressed = 1;
|
is_pressed = 1;
|
||||||
}
|
}
|
||||||
if (IsKeyPressed(KEY_E))
|
if (IsKeyDown(KEY_E)) {
|
||||||
{
|
note->name.assign("E2");
|
||||||
note->name.assign("E4");
|
|
||||||
is_pressed = 1;
|
is_pressed = 1;
|
||||||
}
|
}
|
||||||
if (IsKeyPressed(KEY_F))
|
if (IsKeyDown(KEY_F)) {
|
||||||
{
|
note->name.assign("F2");
|
||||||
note->name.assign("F4");
|
|
||||||
is_pressed = 1;
|
is_pressed = 1;
|
||||||
}
|
}
|
||||||
if (IsKeyPressed(KEY_G))
|
if (IsKeyDown(KEY_G)) {
|
||||||
{
|
note->name.assign("G2");
|
||||||
note->name.assign("G4");
|
|
||||||
is_pressed = 1;
|
is_pressed = 1;
|
||||||
}
|
}
|
||||||
return is_pressed;
|
return is_pressed == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_note_up() {
|
||||||
|
return IsKeyUp(KEY_A) || IsKeyUp(KEY_B) ||
|
||||||
|
IsKeyUp(KEY_C) || IsKeyUp(KEY_D) ||
|
||||||
|
IsKeyUp(KEY_E) || IsKeyUp(KEY_F) || IsKeyUp(KEY_G);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update On Input
|
// Update On Input
|
||||||
void Application::update_on_note_input()
|
void Application::update_on_note_input() {
|
||||||
{
|
if (detect_note_pressed(m_current_note)) {
|
||||||
if (detect_note_pressed(m_current_note))
|
|
||||||
{
|
if (!m_synth.GetIsNoteTriggered()) {
|
||||||
m_synth.ProduceNoteSound((*m_current_note));
|
m_synth.TriggerNote((*m_current_note));
|
||||||
m_sound_played_count = 0;
|
}
|
||||||
|
|
||||||
|
// m_sound_played_count = 0;
|
||||||
write_log("Note played: %s\n", m_current_note->name.c_str());
|
write_log("Note played: %s\n", m_current_note->name.c_str());
|
||||||
|
} else if (is_note_up() && m_synth.GetIsNoteTriggered()) {
|
||||||
|
m_synth.StopSound();
|
||||||
}
|
}
|
||||||
|
// will produce 0 signal if ADSR is in off state
|
||||||
|
m_synth.ProduceSound();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Play ring-buffered audio
|
// Play ring-buffered audio
|
||||||
void Application::play_buffered_audio()
|
void Application::play_buffered_audio() {
|
||||||
{
|
if (IsAudioStreamProcessed(m_synth_stream)) {
|
||||||
if (IsAudioStreamProcessed(m_synth_stream) && !m_ring_buffer->IsEmpty())
|
// const float audio_frame_start_time = GetTime();
|
||||||
{
|
|
||||||
std::size_t size_to_read = m_ring_buffer->GetSize();
|
|
||||||
|
|
||||||
write_log("Samples to play:%zu \n", size_to_read);
|
|
||||||
//todo: try to start reading directly from ring buffer, avoiding temp_buffer
|
|
||||||
m_ring_buffer->Read(m_temp_buffer, size_to_read);
|
|
||||||
// can try the SetAudioStreamCallback
|
|
||||||
UpdateAudioStream(m_synth_stream, m_temp_buffer, size_to_read);
|
|
||||||
// can overwrite the ring buffer to avoid that
|
|
||||||
if (m_synth.GetOutSignal().size() == m_sound_played_count)
|
|
||||||
{
|
|
||||||
m_ring_buffer->Reset();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fill ring buffer from current sound
|
|
||||||
void Application::fill_audio_buffer()
|
|
||||||
{
|
|
||||||
if (!m_ring_buffer->IsFull() && m_synth.GetOutSignal().size() != m_sound_played_count)
|
|
||||||
{
|
|
||||||
write_log("[INFO] IsFull:%d Samples:%zu Played:%d\n",
|
|
||||||
m_ring_buffer->IsFull(),
|
|
||||||
m_synth.GetOutSignal().size(),
|
|
||||||
m_sound_played_count);
|
|
||||||
|
|
||||||
// how many samples need write
|
|
||||||
std::size_t size_to_fill = 0;
|
|
||||||
|
|
||||||
if ((m_synth.GetOutSignal().size() - m_sound_played_count) > m_ring_buffer->GetCapacity())
|
|
||||||
{
|
|
||||||
size_to_fill = m_ring_buffer->GetCapacity();
|
|
||||||
} else
|
|
||||||
{
|
|
||||||
size_to_fill = m_synth.GetOutSignal().size() - m_sound_played_count;
|
|
||||||
}
|
|
||||||
|
|
||||||
write_log("[INFO] SizeToFill:%zu\n", size_to_fill);
|
|
||||||
for (size_t i = 0; i < size_to_fill; i++)
|
|
||||||
{
|
|
||||||
m_temp_buffer[i] = m_synth.GetOutSignal()[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
m_ring_buffer->Write(m_temp_buffer, size_to_fill);
|
|
||||||
m_sound_played_count += size_to_fill;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Application::Run()
|
|
||||||
{
|
|
||||||
// Main game loop
|
|
||||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
||||||
{
|
|
||||||
fill_audio_buffer();
|
|
||||||
play_buffered_audio();
|
|
||||||
update_on_note_input();
|
update_on_note_input();
|
||||||
|
UpdateAudioStream(m_synth_stream, m_synth.GetOutSignal().data(),
|
||||||
|
STREAM_BUFFER_SIZE);
|
||||||
|
// const float audio_freme_duration = GetTime() -
|
||||||
|
// audio_frame_start_time; write_log("Frame time: %.3f%% \n", 100.0f /
|
||||||
|
// ((1.0f / audio_freme_duration) /
|
||||||
|
// ((float)SAMPLE_RATE/STREAM_BUFFER_SIZE)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Application::Run() {
|
||||||
|
// Main game loop
|
||||||
|
while (!WindowShouldClose()) {
|
||||||
|
play_buffered_audio();
|
||||||
m_renderer.Draw(m_synth, m_synth_gui_state);
|
m_renderer.Draw(m_synth, m_synth_gui_state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +1,23 @@
|
|||||||
#include "Oscillator.h"
|
#include "Oscillator.h"
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
|
|
||||||
#define TWO_PI 2*SYNTH_PI
|
#define TWO_PI 2 * SYNTH_PI
|
||||||
|
|
||||||
Oscillator::Oscillator(OscillatorType osc, float freq, float volume)
|
Oscillator::Oscillator(OscillatorType osc, float freq, float volume) {
|
||||||
{
|
|
||||||
SetType(osc);
|
SetType(osc);
|
||||||
m_freq = freq;
|
m_freq = freq;
|
||||||
m_volume = volume;
|
m_volume = volume;
|
||||||
}
|
}
|
||||||
|
|
||||||
Oscillator::~Oscillator()
|
Oscillator::~Oscillator() {}
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void Oscillator::Reset()
|
void Oscillator::Reset() {
|
||||||
{
|
|
||||||
m_volume = 0;
|
m_volume = 0;
|
||||||
m_phase = 0;
|
m_phase = 0;
|
||||||
m_phase_dt = 0;
|
m_phase_dt = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Oscillator::SetType(OscillatorType osc)
|
void Oscillator::SetType(OscillatorType osc) {
|
||||||
{
|
|
||||||
m_osc = osc;
|
m_osc = osc;
|
||||||
switch (m_osc) {
|
switch (m_osc) {
|
||||||
case Sine:
|
case Sine:
|
||||||
@@ -44,68 +39,53 @@ void Oscillator::SetType(OscillatorType osc)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Oscillator::SetFreq(float freq)
|
void Oscillator::SetFreq(float freq) {
|
||||||
{
|
|
||||||
m_freq = freq;
|
m_freq = freq;
|
||||||
m_phase = 0;
|
m_phase = 0;
|
||||||
m_phase_dt = (this->*m_dt_function)(freq);
|
m_phase_dt = (this->*m_dt_function)(freq);
|
||||||
}
|
}
|
||||||
|
|
||||||
float Oscillator::GenerateSample(float duration)
|
float Oscillator::GenerateSample(float duration) {
|
||||||
{
|
|
||||||
return (this->*m_osc_function)() * m_volume;
|
return (this->*m_osc_function)() * m_volume;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Oscillator::sine_osc_phase_incr()
|
void Oscillator::sine_osc_phase_incr() {
|
||||||
{
|
|
||||||
m_phase += m_phase_dt;
|
m_phase += m_phase_dt;
|
||||||
if (m_phase >= TWO_PI)
|
if (m_phase >= TWO_PI)
|
||||||
m_phase -= TWO_PI;
|
m_phase -= TWO_PI;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Oscillator::saw_osc_phase_incr()
|
void Oscillator::saw_osc_phase_incr() {
|
||||||
{
|
|
||||||
m_phase += m_phase_dt;
|
m_phase += m_phase_dt;
|
||||||
if (m_phase >= 1.0f)
|
if (m_phase >= 1.0f)
|
||||||
m_phase -= 1.0f;
|
m_phase -= 1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Oscillator::calc_saw_phase_delta(float freq)
|
float Oscillator::calc_saw_phase_delta(float freq) {
|
||||||
{
|
|
||||||
return freq / SAMPLE_RATE;
|
return freq / SAMPLE_RATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Oscillator::calc_sine_phase_delta(float freq)
|
float Oscillator::calc_sine_phase_delta(float freq) {
|
||||||
{
|
|
||||||
return (TWO_PI * freq) / SAMPLE_RATE;
|
return (TWO_PI * freq) / SAMPLE_RATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Oscillator::sineosc()
|
float Oscillator::sineosc() {
|
||||||
{
|
|
||||||
float result = sinf(m_phase);
|
float result = sinf(m_phase);
|
||||||
sine_osc_phase_incr();
|
sine_osc_phase_incr();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Oscillator::sign(float v)
|
float Oscillator::sign(float v) { return (v > 0.0) ? 1.f : -1.f; }
|
||||||
{
|
|
||||||
return (v > 0.0) ? 1.f : -1.f;
|
|
||||||
}
|
|
||||||
|
|
||||||
float Oscillator::squareosc()
|
float Oscillator::squareosc() { return sign(sineosc()); }
|
||||||
{
|
|
||||||
return sign(sineosc());
|
|
||||||
}
|
|
||||||
|
|
||||||
float Oscillator::triangleosc()
|
float Oscillator::triangleosc() {
|
||||||
{
|
|
||||||
float result = 1.f - fabsf(m_phase - 0.5f) * 4.f;
|
float result = 1.f - fabsf(m_phase - 0.5f) * 4.f;
|
||||||
saw_osc_phase_incr();
|
saw_osc_phase_incr();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Oscillator::sawosc()
|
float Oscillator::sawosc() {
|
||||||
{
|
|
||||||
float result = m_phase * 2.f - 1.f;
|
float result = m_phase * 2.f - 1.f;
|
||||||
saw_osc_phase_incr();
|
saw_osc_phase_incr();
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
27
src/Ramp.cpp
Normal file
27
src/Ramp.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include "Ramp.h"
|
||||||
|
#include "Logger.h"
|
||||||
|
|
||||||
|
Ramp::Ramp(float starting_level, float sample_rate) {
|
||||||
|
m_level = starting_level;
|
||||||
|
m_sample_rate = sample_rate;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ramp::~Ramp() {}
|
||||||
|
|
||||||
|
void Ramp::RampTo(float val, float time) {
|
||||||
|
m_increment = (val - m_level) / (m_sample_rate * time);
|
||||||
|
m_counter = (int)(m_sample_rate * time);
|
||||||
|
write_log("Ramping from: %.1f to: %.1f by: %lf for: %d\n", m_level, val,
|
||||||
|
m_increment, m_counter);
|
||||||
|
}
|
||||||
|
|
||||||
|
float Ramp::Process() {
|
||||||
|
if (m_counter > 0) {
|
||||||
|
m_counter--;
|
||||||
|
m_level += m_increment;
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_level;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Ramp::IsCompleted() { return m_counter == 0; }
|
||||||
257
src/Renderer.cpp
257
src/Renderer.cpp
@@ -1,56 +1,58 @@
|
|||||||
#include "Renderer.h"
|
#include "Renderer.h"
|
||||||
#define RAYGUI_IMPLEMENTATION
|
#define RAYGUI_IMPLEMENTATION
|
||||||
#include "raygui.h"
|
|
||||||
#include "Settings.h"
|
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
|
#include "Settings.h"
|
||||||
|
|
||||||
Renderer::Renderer(/* args */)
|
#pragma clang diagnostic push
|
||||||
{
|
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
|
||||||
|
#pragma clang diagnostic ignored "-Wunused-variable"
|
||||||
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
||||||
|
#include "raygui.h"
|
||||||
|
#pragma clang diagnostic pop
|
||||||
|
|
||||||
|
Renderer::Renderer(/* args */) {
|
||||||
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "SeeSynth - v0.2");
|
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "SeeSynth - v0.2");
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(FPS);
|
||||||
}
|
}
|
||||||
|
|
||||||
Renderer::~Renderer()
|
Renderer::~Renderer() {}
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void Renderer::Draw(Synth& synth, SynthGuiState& synthGui)
|
void Renderer::Draw(Synth& synth, SynthGuiState& synth_gui) {
|
||||||
{
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
|
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
//todo: implement renderer
|
// todo: implement renderer
|
||||||
DrawUi(synth, synthGui);
|
draw_ui(synth, synth_gui);
|
||||||
DrawSignal(synth, synthGui);
|
draw_signal(synth, synth_gui);
|
||||||
//DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
|
// DrawText("Congrats! You created your first window!", 190, 200, 20,
|
||||||
//DrawFPS(0,0);
|
// LIGHTGRAY); DrawFPS(0,0);
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawSignal(Synth & synth, SynthGuiState & synthGui)
|
void Renderer::draw_signal(Synth& synth, SynthGuiState& synth_gui) {
|
||||||
{
|
GuiGrid((Rectangle){0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}, "",
|
||||||
GuiGrid((Rectangle){0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}, "", WINDOW_HEIGHT / 8, 2);
|
WINDOW_HEIGHT / 8, 2);
|
||||||
auto signal = synth.GetOutSignal();
|
auto signal = synth.GetOutSignal();
|
||||||
Vector2* signal_points = new Vector2[signal.size()];
|
Vector2* signal_points = new Vector2[signal.size()];
|
||||||
const float screen_vertical_midpoint = (WINDOW_HEIGHT/2);
|
const float screen_vertical_midpoint = (WINDOW_HEIGHT / 2);
|
||||||
for (int point_idx = 0; point_idx < signal.size(); point_idx++)
|
for (int point_idx = 0; point_idx < signal.size(); point_idx++) {
|
||||||
{
|
|
||||||
signal_points[point_idx].x = (float)point_idx + OSCILLATOR_PANEL_WIDTH;
|
signal_points[point_idx].x = (float)point_idx + OSCILLATOR_PANEL_WIDTH;
|
||||||
signal_points[point_idx].y = screen_vertical_midpoint + (int)(signal[point_idx] * 300);
|
signal_points[point_idx].y =
|
||||||
|
screen_vertical_midpoint + (int)(signal[point_idx] * 300);
|
||||||
}
|
}
|
||||||
DrawLineStrip(signal_points, signal.size(), RED);
|
DrawLineStrip(signal_points, signal.size(), RED);
|
||||||
delete[] signal_points;
|
delete[] signal_points;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawOscillatorsShapeInputs(const std::vector<Oscillator*>& oscillators, const std::vector<OscillatorGuiState*>& guiOscillators)
|
void Renderer::draw_oscillators_shape_inputs(
|
||||||
{
|
const std::vector<Oscillator*>& oscillators,
|
||||||
#define WAVE_SHAPE_OPTIONS "Sine;Triangle;Sawtooth;Square"
|
const std::vector<OscillatorGuiState*>& gui_oscillators) {
|
||||||
|
#define WAVE_SHAPE_OPTIONS "Sine;Triangle;Sawtooth;Square"
|
||||||
|
|
||||||
// DRAW OSCILLATOR SHAPE INPUTS
|
// DRAW OSCILLATOR SHAPE INPUTS
|
||||||
for (int i = 0; i < oscillators.size(); i += 1)
|
for (int i = 0; i < oscillators.size(); i += 1) {
|
||||||
{
|
OscillatorGuiState* ui_osc = gui_oscillators[i];
|
||||||
OscillatorGuiState* ui_osc = guiOscillators[i];
|
|
||||||
assert(ui_osc);
|
assert(ui_osc);
|
||||||
|
|
||||||
Oscillator* osc = oscillators[i];
|
Oscillator* osc = oscillators[i];
|
||||||
@@ -58,32 +60,29 @@ void Renderer::DrawOscillatorsShapeInputs(const std::vector<Oscillator*>& oscill
|
|||||||
|
|
||||||
// Shape select
|
// Shape select
|
||||||
int shape_index = (int)(ui_osc->waveshape);
|
int shape_index = (int)(ui_osc->waveshape);
|
||||||
bool is_dropdown_click = GuiDropdownBox(ui_osc->shape_dropdown_rect,
|
bool is_dropdown_click =
|
||||||
WAVE_SHAPE_OPTIONS,
|
GuiDropdownBox(ui_osc->shape_dropdown_rect, WAVE_SHAPE_OPTIONS,
|
||||||
&shape_index,
|
&shape_index, ui_osc->is_dropdown_open);
|
||||||
ui_osc->is_dropdown_open
|
|
||||||
);
|
|
||||||
|
|
||||||
if (is_dropdown_click)
|
if (is_dropdown_click) {
|
||||||
{
|
|
||||||
write_log("Dropdown clicked!\n");
|
write_log("Dropdown clicked!\n");
|
||||||
ui_osc->is_dropdown_open = !ui_osc->is_dropdown_open;
|
ui_osc->is_dropdown_open = !ui_osc->is_dropdown_open;
|
||||||
ui_osc->waveshape = (OscillatorType)(shape_index);
|
ui_osc->waveshape = (OscillatorType)(shape_index);
|
||||||
// APPLY STATE TO REAL OSC
|
// APPLY STATE TO REAL OSC
|
||||||
osc->SetType(ui_osc->waveshape);
|
osc->SetType(ui_osc->waveshape);
|
||||||
}
|
}
|
||||||
if (ui_osc->is_dropdown_open) break;
|
if (ui_osc->is_dropdown_open)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawOscillatorsPanels(const std::vector<Oscillator*>& oscillators,
|
float Renderer::draw_oscillators_panels(
|
||||||
const std::vector<OscillatorGuiState*>& guiOscillators,
|
const std::vector<Oscillator*>& oscillators,
|
||||||
const Rectangle& panel_bounds)
|
const std::vector<OscillatorGuiState*>& gui_oscillators,
|
||||||
{
|
const Rectangle& panel_bounds) {
|
||||||
float panel_y_offset = 0;
|
float panel_y_offset = 0;
|
||||||
for (int i = 0; i < oscillators.size(); i++)
|
for (int i = 0; i < oscillators.size(); i++) {
|
||||||
{
|
OscillatorGuiState* ui_osc = gui_oscillators[i];
|
||||||
OscillatorGuiState* ui_osc = guiOscillators[i];
|
|
||||||
assert(ui_osc);
|
assert(ui_osc);
|
||||||
|
|
||||||
Oscillator* osc = oscillators[i];
|
Oscillator* osc = oscillators[i];
|
||||||
@@ -97,35 +96,25 @@ void Renderer::DrawOscillatorsPanels(const std::vector<Oscillator*>& oscillators
|
|||||||
const int osc_panel_x = panel_bounds.x + 10;
|
const int osc_panel_x = panel_bounds.x + 10;
|
||||||
const int osc_panel_y = panel_bounds.y + 50 + panel_y_offset;
|
const int osc_panel_y = panel_bounds.y + 50 + panel_y_offset;
|
||||||
panel_y_offset += osc_panel_height + 5;
|
panel_y_offset += osc_panel_height + 5;
|
||||||
GuiPanel((Rectangle){
|
GuiPanel((Rectangle){(float)osc_panel_x, (float)osc_panel_y,
|
||||||
(float)osc_panel_x,
|
(float)osc_panel_width, (float)osc_panel_height},
|
||||||
(float)osc_panel_y,
|
"");
|
||||||
(float)osc_panel_width,
|
|
||||||
(float)osc_panel_height
|
|
||||||
},
|
|
||||||
"");
|
|
||||||
|
|
||||||
const float slider_padding = 50.f;
|
const float slider_padding = 50.f;
|
||||||
const float el_spacing = 5.f;
|
const float el_spacing = 5.f;
|
||||||
Rectangle el_rect = {
|
Rectangle el_rect = {.x = (float)osc_panel_x + slider_padding + 30,
|
||||||
.x = (float)osc_panel_x + slider_padding + 30,
|
.y = (float)osc_panel_y + 10,
|
||||||
.y = (float)osc_panel_y + 10,
|
.width =
|
||||||
.width = (float)osc_panel_width - (slider_padding * 2),
|
(float)osc_panel_width - (slider_padding * 2),
|
||||||
.height = 25.f
|
.height = 25.f};
|
||||||
};
|
|
||||||
|
|
||||||
// Volume slider
|
// Volume slider
|
||||||
float decibels = (20.f * log10f(osc->GetVolume()));
|
float decibels = (20.f * log10f(osc->GetVolume()));
|
||||||
char amp_slider_label[32];
|
char amp_slider_label[32];
|
||||||
sprintf(amp_slider_label, "%.1f dB", decibels);
|
snprintf(amp_slider_label, 7, "%.1f dB", decibels);
|
||||||
decibels = GuiSlider(el_rect,
|
decibels =
|
||||||
amp_slider_label,
|
GuiSlider(el_rect, amp_slider_label, "", decibels, -60.0f, 0.0f);
|
||||||
"",
|
ui_osc->volume = powf(10.f, decibels * (1.f / 20.f));
|
||||||
decibels,
|
|
||||||
-60.0f,
|
|
||||||
0.0f
|
|
||||||
);
|
|
||||||
ui_osc->volume = powf(10.f, decibels * (1.f/20.f));
|
|
||||||
osc->SetVolume(ui_osc->volume);
|
osc->SetVolume(ui_osc->volume);
|
||||||
|
|
||||||
el_rect.y += el_rect.height + el_spacing;
|
el_rect.y += el_rect.height + el_spacing;
|
||||||
@@ -133,64 +122,124 @@ void Renderer::DrawOscillatorsPanels(const std::vector<Oscillator*>& oscillators
|
|||||||
// Defer shape drop-down box.
|
// Defer shape drop-down box.
|
||||||
ui_osc->shape_dropdown_rect = el_rect;
|
ui_osc->shape_dropdown_rect = el_rect;
|
||||||
el_rect.y += el_rect.height + el_spacing;
|
el_rect.y += el_rect.height + el_spacing;
|
||||||
/*
|
|
||||||
Rectangle delete_button_rect = el_rect;
|
Rectangle delete_button_rect = el_rect;
|
||||||
delete_button_rect.x = osc_panel_x + 5;
|
delete_button_rect.x = osc_panel_x + 5;
|
||||||
delete_button_rect.y -= el_rect.height + el_spacing;
|
delete_button_rect.y -= el_rect.height + el_spacing;
|
||||||
delete_button_rect.width = 30;
|
delete_button_rect.width = 30;
|
||||||
bool is_delete_button_pressed = GuiButton(delete_button_rect, "X");
|
bool is_delete_button_pressed = GuiButton(delete_button_rect, "X");
|
||||||
if (is_delete_button_pressed)
|
if (is_delete_button_pressed) {
|
||||||
{
|
// memmove(
|
||||||
memmove(
|
// synth->ui_oscillator + ui_osc_i,
|
||||||
synth->ui_oscillator + ui_osc_i,
|
// synth->ui_oscillator + ui_osc_i + 1,
|
||||||
synth->ui_oscillator + ui_osc_i + 1,
|
// (synth->ui_oscillator_count - ui_osc_i) *
|
||||||
(synth->ui_oscillator_count - ui_osc_i) * sizeof(UiOscillator)
|
// sizeof(UiOscillator)
|
||||||
);
|
// );
|
||||||
synth->ui_oscillator_count -= 1;
|
// synth->ui_oscillator_count -= 1;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return panel_y_offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawMainPanel(const Rectangle& panel_bounds)
|
void Renderer::draw_main_panel(const Rectangle& panel_bounds) {
|
||||||
{
|
|
||||||
bool is_shape_dropdown_open = false;
|
|
||||||
int shape_index = 0;
|
|
||||||
GuiPanel(panel_bounds, "");
|
GuiPanel(panel_bounds, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawAddOscillatorButton(Synth & synth, SynthGuiState & synthGui, Rectangle panel_bounds)
|
void Renderer::draw_add_oscillator_button(Synth& synth,
|
||||||
{
|
SynthGuiState& synth_gui,
|
||||||
bool click_add_oscillator = GuiButton((Rectangle){
|
Rectangle panel_bounds) {
|
||||||
panel_bounds.x + 10,
|
//clang-format off
|
||||||
panel_bounds.y + 10,
|
bool click_add_oscillator =
|
||||||
panel_bounds.width - 20,
|
GuiButton((Rectangle){panel_bounds.x + 10, panel_bounds.y + 10,
|
||||||
25.f
|
panel_bounds.width - 20, 25.f},
|
||||||
}, "Add Oscillator");
|
"Add Oscillator");
|
||||||
if (click_add_oscillator)
|
//clang-format on
|
||||||
{
|
|
||||||
|
if (click_add_oscillator) {
|
||||||
synth.AddOscillator();
|
synth.AddOscillator();
|
||||||
Oscillator* osc = synth.GetOscillators().back();
|
Oscillator* osc = synth.GetOscillators().back();
|
||||||
|
|
||||||
OscillatorGuiState* ui = new OscillatorGuiState {
|
OscillatorGuiState* ui =
|
||||||
.freq = osc->GetFreq(),
|
new OscillatorGuiState{.freq = osc->GetFreq(),
|
||||||
.waveshape = osc->GetType(),
|
.waveshape = osc->GetType(),
|
||||||
.volume = osc->GetVolume()
|
.volume = osc->GetVolume()};
|
||||||
};
|
synth_gui.oscillators.push_back(ui);
|
||||||
synthGui.oscillators.push_back(ui);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawUi(Synth & synth, SynthGuiState & synthGui)
|
void Renderer::draw_ui(Synth& synth, SynthGuiState& synth_gui) {
|
||||||
{
|
Rectangle panel_bounds = {.x = 0,
|
||||||
Rectangle panel_bounds = {.x = 0, .y = 0, .width = OSCILLATOR_PANEL_WIDTH, .height = WINDOW_HEIGHT };
|
.y = 0,
|
||||||
DrawMainPanel(panel_bounds);
|
.width = OSCILLATOR_PANEL_WIDTH,
|
||||||
DrawAddOscillatorButton(synth, synthGui, panel_bounds);
|
.height = WINDOW_HEIGHT};
|
||||||
|
draw_main_panel(panel_bounds);
|
||||||
|
draw_add_oscillator_button(synth, synth_gui, panel_bounds);
|
||||||
// Draw Oscillators
|
// Draw Oscillators
|
||||||
std::vector<Oscillator*> oscillators = synth.GetOscillators();
|
std::vector<Oscillator*> oscillators = synth.GetOscillators();
|
||||||
std::vector<OscillatorGuiState*> guiOscillators = synthGui.oscillators;
|
std::vector<OscillatorGuiState*> gui_oscillators = synth_gui.oscillators;
|
||||||
|
|
||||||
DrawOscillatorsPanels(oscillators, guiOscillators, panel_bounds);
|
float panel_y_offset =
|
||||||
DrawOscillatorsShapeInputs(oscillators, guiOscillators);
|
draw_oscillators_panels(oscillators, gui_oscillators, panel_bounds);
|
||||||
|
draw_oscillators_shape_inputs(oscillators, gui_oscillators);
|
||||||
|
|
||||||
|
draw_adsr_panel(synth.GetADSR(), synth_gui.adsr, panel_bounds,
|
||||||
|
panel_y_offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::draw_adsr_panel(ADSR* adsr, ADSRGuiState& gui_adsr,
|
||||||
|
const Rectangle& panel_bounds,
|
||||||
|
float panel_y_offset) {
|
||||||
|
// Draw ADSR Panel
|
||||||
|
const int osc_panel_width = panel_bounds.width - 20;
|
||||||
|
const int osc_panel_height = 120;
|
||||||
|
const int osc_panel_x = panel_bounds.x + 10;
|
||||||
|
const int osc_panel_y = panel_bounds.y + 50 + panel_y_offset;
|
||||||
|
panel_y_offset += osc_panel_height + 5;
|
||||||
|
GuiPanel((Rectangle){(float)osc_panel_x, (float)osc_panel_y,
|
||||||
|
(float)osc_panel_width, (float)osc_panel_height},
|
||||||
|
"ADSR");
|
||||||
|
|
||||||
|
const float slider_padding = 50.f;
|
||||||
|
const float el_spacing = 5.f;
|
||||||
|
Rectangle el_rect = {.x = (float)osc_panel_x + slider_padding + 30,
|
||||||
|
.y = (float)osc_panel_y + 10,
|
||||||
|
.width = (float)osc_panel_width - (slider_padding * 2),
|
||||||
|
.height = 25.f};
|
||||||
|
|
||||||
|
// Attack slider
|
||||||
|
float attack = gui_adsr.attack;
|
||||||
|
char attack_slider_label[32];
|
||||||
|
snprintf(attack_slider_label, 7, "%.1f s", attack);
|
||||||
|
attack = GuiSlider(el_rect, attack_slider_label, "", attack, 0.0f, 2.0f);
|
||||||
|
gui_adsr.attack = attack;
|
||||||
|
el_rect.y += el_rect.height + el_spacing;
|
||||||
|
|
||||||
|
// Decay slider
|
||||||
|
float decay = gui_adsr.decay;
|
||||||
|
char decay_slider_label[32];
|
||||||
|
snprintf(decay_slider_label, 7, "%.1f s", decay);
|
||||||
|
decay = GuiSlider(el_rect, decay_slider_label, "", decay, 0.0f, 1.0f);
|
||||||
|
gui_adsr.decay = decay;
|
||||||
|
el_rect.y += el_rect.height + el_spacing;
|
||||||
|
|
||||||
|
// Sustain slider
|
||||||
|
float sustain = gui_adsr.sustain;
|
||||||
|
char sustain_slider_label[32];
|
||||||
|
snprintf(sustain_slider_label, 7, "%.1f u", sustain);
|
||||||
|
sustain = GuiSlider(el_rect, sustain_slider_label, "", sustain, 0.0f, 1.0f);
|
||||||
|
gui_adsr.sustain = sustain;
|
||||||
|
el_rect.y += el_rect.height + el_spacing;
|
||||||
|
|
||||||
|
// Release slider
|
||||||
|
float release = gui_adsr.release;
|
||||||
|
char release_slider_label[32];
|
||||||
|
snprintf(release_slider_label, 7, "%.1f s", release);
|
||||||
|
release = GuiSlider(el_rect, release_slider_label, "", release, 0.0f, 5.0f);
|
||||||
|
gui_adsr.release = release;
|
||||||
|
el_rect.y += el_rect.height + el_spacing;
|
||||||
|
|
||||||
|
// apply values to real one
|
||||||
|
adsr->SetParameters(gui_adsr.attack, gui_adsr.decay, gui_adsr.sustain,
|
||||||
|
gui_adsr.release);
|
||||||
}
|
}
|
||||||
@@ -1,39 +1,83 @@
|
|||||||
#include "Synth.h"
|
#include "Synth.h"
|
||||||
#include "Settings.h"
|
#include "ADSR.h"
|
||||||
#include "KeyBoard.h"
|
#include "KeyBoard.h"
|
||||||
|
#include "Logger.h"
|
||||||
#include "OscillatorType.h"
|
#include "OscillatorType.h"
|
||||||
|
#include "Settings.h"
|
||||||
|
|
||||||
Synth::Synth(/* args */)
|
Synth::Synth(/* args */) {
|
||||||
{
|
|
||||||
AddOscillator();
|
AddOscillator();
|
||||||
|
AddEffect(new ADSR());
|
||||||
|
for (size_t i = 0; i < STREAM_BUFFER_SIZE; i++) {
|
||||||
|
float sample = 0.0f;
|
||||||
|
m_out_signal.push_back(sample);
|
||||||
|
}
|
||||||
|
zero_signal();
|
||||||
}
|
}
|
||||||
|
|
||||||
Synth::~Synth()
|
Synth::~Synth() {
|
||||||
{
|
m_oscillators.clear();
|
||||||
|
m_effects.clear();
|
||||||
|
m_out_signal.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<float> & Synth::get_note(int semitone, float beats)
|
void Synth::zero_signal() {
|
||||||
{
|
float sample = 0.0f;
|
||||||
float hz = KeyBoard::GetHzBySemitone(semitone);
|
for (size_t i = 0; i < STREAM_BUFFER_SIZE; i++) {
|
||||||
float duration = beats * BEAT_DURATION;
|
m_out_signal[i] = sample;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Synth::get_note() {
|
||||||
|
zero_signal();
|
||||||
|
Adder::SumOscillators(m_oscillators, m_out_signal);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Synth::apply_effects() {
|
||||||
|
for (Effect* effect : m_effects) {
|
||||||
|
effect->Process(m_out_signal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Synth::trigger_note_on_effects() {
|
||||||
|
for (Effect* effect : m_effects) {
|
||||||
|
effect->OnSetNote();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Synth::untrigger_note_on_effects() {
|
||||||
|
for (Effect* effect : m_effects) {
|
||||||
|
effect->OnUnsetNote();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Synth::TriggerNote(Note input) {
|
||||||
|
int semitone_shift = KeyBoard::GetSemitoneShift(input.name);
|
||||||
|
float hz = KeyBoard::GetHzBySemitone(semitone_shift);
|
||||||
|
|
||||||
// will change after oscillator starts to be more autonomous
|
// will change after oscillator starts to be more autonomous
|
||||||
for (Oscillator* osc : m_oscillators)
|
for (Oscillator* osc : m_oscillators) {
|
||||||
{
|
|
||||||
osc->SetFreq(hz);
|
osc->SetFreq(hz);
|
||||||
}
|
}
|
||||||
|
is_note_triggered = true;
|
||||||
return m_adder.SumOscillators(m_oscillators, duration); //todo: add other pipeline steps (e.g ADSR, Filters, FX);
|
trigger_note_on_effects();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Synth::ProduceNoteSound(Note input)
|
void Synth::ProduceSound() {
|
||||||
{
|
get_note();
|
||||||
float length = 1.f / input.length;
|
apply_effects();
|
||||||
int semitone_shift = KeyBoard::GetSemitoneShift(input.name);
|
|
||||||
m_out_signal = get_note(semitone_shift, length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Synth::AddOscillator()
|
// todo: rename to something like untrigger note
|
||||||
{
|
void Synth::StopSound() {
|
||||||
m_oscillators.push_back(new Oscillator(OscillatorType::Sine, 440.f, VOLUME));
|
zero_signal();
|
||||||
|
is_note_triggered = false;
|
||||||
|
untrigger_note_on_effects();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Synth::AddOscillator() {
|
||||||
|
m_oscillators.push_back(
|
||||||
|
new Oscillator(OscillatorType::Sine, 440.f, VOLUME));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Synth::AddEffect(Effect* fx) { m_effects.push_back(fx); }
|
||||||
|
|||||||
Reference in New Issue
Block a user