Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bucket420 committed May 9, 2022
1 parent f1d0711 commit bc18f34
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 48 deletions.
9 changes: 5 additions & 4 deletions TextComposer/src/Chord.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "Chord.h"
#pragma once
#include <algorithm>
#include "Chord.h"

Chord::Chord(std::string chord, std::array<double, 25> scale, std::string scaleType)
{
this->chord = chord;
this->scale = scale;
this->scaleType = scaleType;
setWaveTable();
setWavetable();
}

std::string Chord::getChordSymbol(std::string chord)
Expand All @@ -21,7 +22,7 @@ std::string Chord::getChordSymbol(std::string chord)
return chord;
}

void Chord::setWaveTable()
void Chord::setWavetable()
{
double duration = getDuration(chord);
std::string chordSymbol = getChordSymbol(chord);
Expand All @@ -35,7 +36,7 @@ void Chord::setWaveTable()
if (chord[chordSymbol.size()] == 'b') step -= 1;
if (chord[chordSymbol.size()] == '#') step += 1;

this->Signal::setWaveTable(scale[step], duration);
this->Signal::setWavetable(scale[step], duration);

if (chord[chordSymbol.size()] == 'd' || (chord.size() > chordSymbol.size() + 1 && chord[chordSymbol.size() + 1] == 'd'))
{
Expand Down
18 changes: 14 additions & 4 deletions TextComposer/src/Chord.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
#pragma once
#include "Signal.h"
#include <array>
#include "Signal.h"

class Chord : public Signal
{
public:
Chord(std::string chord, std::array<double, 25> scale, std::string scaleType);

private:
/* The representation of a chord, e.g. I--, vi.., etc */
std::string chord;

/* Key signature */
std::array<double, 25> scale;

/* Major or Minor */
std::string scaleType;

static std::string getChordSymbol(std::string chord);
void setWaveTable();
static std::string toUpper(std::string string);
/* Chord symbol without the duration syntax, e.g. I, IVd, iii, etc. */
std::string getChordSymbol(std::string chord);

/* Set the wavetable */
void setWavetable();

/* Turn all characters in the string to uppercase letters */
std::string toUpper(std::string string);
};
7 changes: 4 additions & 3 deletions TextComposer/src/ChordProgression.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "ChordProgression.h"
#pragma once
#include <memory>
#include "ChordProgression.h"

ChordProgression::ChordProgression(std::string progression, std::string key, std::string scaleType)
{
this->progression = progression;
this->key = key;
this->scaleType = scaleType;
setWaveTable();
setWavetable();
}

std::array<double, 25> ChordProgression::createTwoOctaveScale(std::string key)
Expand Down Expand Up @@ -63,7 +64,7 @@ std::vector<std::string> ChordProgression::getChords()
}
}

void ChordProgression::setWaveTable()
void ChordProgression::setWavetable()
{
std::vector<std::string> chordList = getChords();
std::array<double, 25> scale = createTwoOctaveScale(key);
Expand Down
15 changes: 14 additions & 1 deletion TextComposer/src/ChordProgression.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@
class ChordProgression : public Signal
{
public:
/* Set all member variables */
ChordProgression(std::string progression, std::string key, std::string scaleType);

private:
/* The representation of a chord progression, e.g. I----VI----vi----V---- */
std::string progression;

/* Key signature */
std::string key;

/* Major or Minor */
std::string scaleType;

/* Frequencies of all possible notes in 2 octaves */
std::array<double, 25> createTwoOctaveScale(std::string key);

/* Get indexes of chord symbols in the progression string */
std::vector<int> getChordIndexes();

/* Extract representations of individual chords from the progression string */
std::vector<std::string> getChords();
void setWaveTable();

/* Set the wavetable */
void setWavetable();
};
4 changes: 2 additions & 2 deletions TextComposer/src/Melody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Melody::Melody(std::string melody)
{
this->melody = melody;
setWaveTable();
setWavetable();
}

std::vector<int> Melody::getNoteIndexes()
Expand Down Expand Up @@ -36,7 +36,7 @@ std::vector<std::string> Melody::getNotes()
}
}

void Melody::setWaveTable()
void Melody::setWavetable()
{
std::vector<std::string> noteList = getNotes();
if (noteList.empty()) return;
Expand Down
9 changes: 8 additions & 1 deletion TextComposer/src/Melody.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
class Melody : public Signal
{
public:
/* Set this->melody to melody */
Melody(std::string melody);

private:
/* The representation of a melody, e.g. C#5-E5-C#5-F#5---A5---G#5------. */
std::string melody;

/* Get indexes of note symbols in the melody string */
std::vector<int> getNoteIndexes();

/* Extract representations of individual notes from the melody string */
std::vector<std::string> getNotes();
void setWaveTable();

/* Set the wavetable */
void setWavetable();
};
6 changes: 3 additions & 3 deletions TextComposer/src/Note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
Note::Note(std::string note)
{
this->note = note;
setWaveTable();
setWavetable();
}

void Note::setWaveTable()
void Note::setWavetable()
{
std::string name;
double duration = getDuration(note);
Expand All @@ -19,5 +19,5 @@ void Note::setWaveTable()
{
name = note.substr(0, 2);
}
*this->waveTable = createWaveTable(getNoteFreq(name), duration);
*this->waveTable = createWavetable(getNoteFreq(name), duration);
}
5 changes: 4 additions & 1 deletion TextComposer/src/Note.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
class Note : public Signal
{
public:
/* Set this->note to note */
Note(std::string note);

private:
/* The representation of a note, e.g. A4--, B5.., etc */
std::string note;

void setWaveTable();
/* Set the wavetable */
void setWavetable();

};
15 changes: 7 additions & 8 deletions TextComposer/src/Signal.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#pragma once
#include <math.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <memory>
#include <vector>
#include "Signal.h"

#define M_PI (3.14159265)
Expand All @@ -25,7 +24,7 @@ Signal::Signal()

Signal::Signal(double freq, double duration)
{
*waveTable = createWaveTable(freq, duration);
*waveTable = createWavetable(freq, duration);
}

void Signal::add(Signal* signal)
Expand Down Expand Up @@ -86,7 +85,7 @@ std::vector<double> Signal::createSineLUT(int size)
return sineLUT;
}

std::vector<double> Signal::createWaveTable(double freq, double duration)
std::vector<double> Signal::createWavetable(double freq, double duration)
{
if (freq == 0) return {};
double phase = 0.0;
Expand Down Expand Up @@ -148,7 +147,7 @@ double Signal::getPhase()
}


std::vector<double> Signal::getWaveTable()
std::vector<double> Signal::getWavetable()
{
return *waveTable;
}
Expand Down Expand Up @@ -182,7 +181,7 @@ void Signal::setTimeSignatureLowerAndBPM(std::string timeSignatureLower, std::st
Signal::BPM = std::stod(BPM);
}

void Signal::setWaveTable(double freq, double duration)
void Signal::setWavetable(double freq, double duration)
{
*waveTable = createWaveTable(freq, duration);
*waveTable = createWavetable(freq, duration);
}
77 changes: 64 additions & 13 deletions TextComposer/src/Signal.h
Original file line number Diff line number Diff line change
@@ -1,45 +1,96 @@
#pragma once
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <memory>

class Signal
{
public:
/* Beats per minute */
static double BPM;

/* The lower part of time signature */
static double timeSignatureLower;

/* Frequency of every note in the first octave */
const static double FIRST_OCTAVE_FREQ[7];

/* The Lookup Table used to generate signals */
const static std::vector<double> LUT;

/* The position of each note in the major scale (1 means the key, 3 means 2 half-steps from the key, etc. */
const static int MAJOR_SCALE_STEPS[7];

/* The position of each note in the minor scale (1 means the key, 3 means 2 half-steps from the key, etc.) */
const static int MINOR_SCALE_STEPS[7];

/* The musical alphabet */
const static char OCTAVE[7];

/* Roman numbers from 1 to 7 */
const static std::string ROMAN_NUMBERS[7];

/* Sample rate */
const static int SAMPLE_RATE = 44100;

/* Default constructor, the wavetable is set to an empty vector */
Signal();

/* The wavetable is set to a specific frequency and duration */
Signal(double freq, double duration);

void add(Signal* signal);
void append(Signal* signal);

/* Get the data point at current phase */
double get();

/* Get the duration of the signal */
double getDuration();

/* Get the phase of the signal */
double getPhase();
std::vector<double> getWaveTable();

/* Get the wavetable of the signal */
std::vector<double> getWavetable();

/* Increment the phase of the signal */
void incrementPhase();

/* Return true if the wavetable is empty */
bool isEmpty();
void normalize();
void setWaveTable(double freq, double duration);

static std::vector<double> createGuitarLUT(int size);
static std::vector<double> createSineLUT(int size);
static std::vector<double> createWaveTable(double freq, double duration);
static double getDuration(std::string input);
static double getNoteFreq(std::string name);
/* Set the lower part of time signature and beats per minute */
static void setTimeSignatureLowerAndBPM(std::string timeSignatureLower, std::string BPM);

/* Set the wavetable to a specific frequency and duration */
void setWavetable(double freq, double duration);

protected:
/* The wavetable of the signal */
std::unique_ptr<std::vector<double>> waveTable = std::unique_ptr<std::vector<double>>(new std::vector<double>);

/* The phase of the signal (an index of a value in the wavetable) */
int phase = 0;

/* Add a signal to this signal */
void add(Signal* signal);

/* Append a signal to this signal */
void append(Signal* signal);

/* Normalize the signal so that the highest amplitude is 1 */
void normalize();

/* Create a Lookup Table for the signal that guitar produces */
static std::vector<double> createGuitarLUT(int size);

/* Create a Lookup Table for the signal that guitar produces */
static std::vector<double> createSineLUT(int size);

/* Create a Lookup Table a sine wave */
static std::vector<double> createWavetable(double freq, double duration);

/* Get the intended duration from an input */
static double getDuration(std::string input);

/* Get the frequency of a note */
static double getNoteFreq(std::string name);
};
7 changes: 3 additions & 4 deletions TextComposer/src/TextToAudio.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#pragma once
#include "TextToAudio.h"
#include "Melody.h"
#include "ChordProgression.h"
#include <chrono>

#include "ChordProgression.h"
#include "Melody.h"
#include "TextToAudio.h"

#define FRAMES_PER_BUFFER (64)

Expand Down
Loading

0 comments on commit bc18f34

Please sign in to comment.