wip: refactor out application methods

This commit is contained in:
2023-08-07 15:01:35 +04:00
parent e3825b341d
commit 56b68bc963
4 changed files with 102 additions and 80 deletions

View File

@@ -15,9 +15,11 @@ private:
Note* m_current_note;
Renderer m_renderer;
std::size_t detect_note_pressed(Note* note);
void init_gui();
void init_synth();
void init_audio();
void update_on_note_input();
void play_buffered_audio();
void fill_audio_buffer();
public:
Application(/* args */);

View File

@@ -10,12 +10,6 @@ public:
void Draw();
};
Renderer::Renderer(/* args */)
{
}
Renderer::~Renderer()
{
}

View File

@@ -2,11 +2,11 @@
#include "Settings.h"
#include "Logger.h"
#include <string>
Application::Application(/* args */)
{
m_ring_buffer = new RingBuffer<float>((std::size_t)STREAM_BUFFER_SIZE);
m_temp_buffer = new float[STREAM_BUFFER_SIZE];
init_gui();
init_synth();
init_audio();
}
@@ -17,12 +17,6 @@ Application::~Application()
delete m_temp_buffer;
}
void Application::init_gui()
{
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "SeeSynth - v0.2");
SetTargetFPS(60);
}
void Application::init_audio()
{
m_sound_played_count = 0;
@@ -36,10 +30,12 @@ void Application::init_audio()
PlayAudioStream(m_synth_stream);
}
void Application::init_synth() {
void Application::init_synth()
{
//todo: move that variables to Synth declaration
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)
};
@@ -57,101 +53,120 @@ void Application::init_synth() {
// }
}
std::size_t Application::detect_note_pressed(Note* note) {
std::size_t Application::detect_note_pressed(Note* note)
{
std::size_t is_pressed = 0;
note->length = 8;
if (IsKeyPressed(KEY_A)) {
if (IsKeyPressed(KEY_A))
{
note->name.assign("A4");
is_pressed = 1;
}
if (IsKeyPressed(KEY_B)) {
if (IsKeyPressed(KEY_B))
{
note->name.assign("B4");
is_pressed = 1;
}
if (IsKeyPressed(KEY_C)) {
if (IsKeyPressed(KEY_C))
{
note->name.assign("C4");
is_pressed = 1;
}
if (IsKeyPressed(KEY_D)) {
if (IsKeyPressed(KEY_D))
{
note->name.assign("D4");
is_pressed = 1;
}
if (IsKeyPressed(KEY_E)) {
if (IsKeyPressed(KEY_E))
{
note->name.assign("E4");
is_pressed = 1;
}
if (IsKeyPressed(KEY_F)) {
if (IsKeyPressed(KEY_F))
{
note->name.assign("F4");
is_pressed = 1;
}
if (IsKeyPressed(KEY_G)) {
if (IsKeyPressed(KEY_G))
{
note->name.assign("G4");
is_pressed = 1;
}
return is_pressed;
}
// Update On Input
void Application::update_on_note_input()
{
if (detect_note_pressed(m_current_note))
{
m_synth.ProduceNoteSound((*m_current_note));
m_sound_played_count = 0;
write_log("Note played: %s\n", m_current_note->name);
}
}
// Play ring-buffered audio
void Application::play_buffered_audio()
{
if (IsAudioStreamProcessed(m_synth_stream) && !m_ring_buffer->IsEmpty())
{
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
{
// Update Audio states
//----------------------------------------------------------------------------------
// Fill ring buffer from current sound
std::vector<float> sound = m_synth.GetOutSignal();
size_t size_for_buffer = 0;
if (!m_ring_buffer->IsFull() && sound.size() != m_sound_played_count) {
write_log("[INFO] IsFull:%d Samples:%zu Played:%d\n",
m_ring_buffer->IsFull(),
sound.size(),
m_sound_played_count);
// how many samples need write
std::size_t size_to_fill = 0;
if ((sound.size() - m_sound_played_count) > m_ring_buffer->GetCapacity()) {
size_to_fill = m_ring_buffer->GetCapacity();
} else {
size_to_fill = sound.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] = sound[i];
}
m_ring_buffer->Write(m_temp_buffer, size_to_fill);
m_sound_played_count += size_to_fill;
}
// Play ring-buffered audio
if (IsAudioStreamProcessed(m_synth_stream) && !m_ring_buffer->IsEmpty()) {
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 (sound.size() == m_sound_played_count) {
m_ring_buffer->Reset();
}
}
//----------------------------------------------------------------------------------
fill_audio_buffer();
play_buffered_audio();
update_on_note_input();
// Update On Input
//----------------------------------------------------------------------------------
if (detect_note_pressed(m_current_note)) {
m_synth.ProduceNoteSound((*m_current_note));
sound = m_synth.GetOutSignal();
m_sound_played_count = 0;
write_log("Note played: %s\n", m_current_note->name);
}
//----------------------------------------------------------------------------------
m_renderer.Draw();
}
}

View File

@@ -1,6 +1,17 @@
#include "Renderer.h"
#include "raylib.h"
#include "raygui.h"
#include "Settings.h"
Renderer::Renderer(/* args */)
{
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "SeeSynth - v0.2");
SetTargetFPS(60);
}
Renderer::~Renderer()
{
}
void Renderer::Draw()
{