Bladeren bron

feat: Initial version

Raylib + Simple Sine oscillator
Douglas Andreani 2 jaren geleden
commit
2db888d0bc
8 gewijzigde bestanden met toevoegingen van 178 en 0 verwijderingen
  1. 2 0
      .gitignore
  2. 6 0
      .gitmodules
  3. 12 0
      CMakeLists.txt
  4. 36 0
      Oscillator.cpp
  5. 73 0
      Oscillator.h
  6. 47 0
      main.cpp
  7. 1 0
      raylib
  8. 1 0
      spdlog

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+.vscode
+build

+ 6 - 0
.gitmodules

@@ -0,0 +1,6 @@
+[submodule "raylib"]
+	path = raylib
+	url = https://github.com/raysan5/raylib
+[submodule "spdlog"]
+	path = spdlog
+	url = https://github.com/gabime/spdlog

+ 12 - 0
CMakeLists.txt

@@ -0,0 +1,12 @@
+cmake_minimum_required(VERSION 3.0.0)
+project(values VERSION 0.1.0 LANGUAGES CXX)
+
+add_subdirectory(spdlog)
+add_subdirectory(raylib)
+
+add_executable(values main.cpp 
+                      Oscillator.cpp)
+target_include_directories(values PRIVATE ${PROJECT_SOURCE_DIR}/spdlog/include)
+target_include_directories(values PRIVATE ${PROJECT_SOURCE_DIR}/raylib/src)
+
+target_link_libraries(values PRIVATE raylib)

+ 36 - 0
Oscillator.cpp

@@ -0,0 +1,36 @@
+#include "Oscillator.h"
+#include <cmath>
+
+namespace Audio
+{
+
+    void SineOscillator::update()
+    {
+        for (size_t i = 0; i < BUFFER_SIZE; i++)
+        {
+            m_phase += m_phase_stride;
+            if (m_phase >= 1.0f)
+                m_phase -= 1.0f;
+
+            buffer[i] = sinf(2.0f * PI_A * m_phase);
+        }
+        m_phase_stride = m_frequency * sample_duration;
+    };
+
+    void SquareOscillator::update()
+    {
+        for (size_t i = 0; i < BUFFER_SIZE; i++)
+        {
+            m_phase += m_phase_stride;
+            if (m_phase >= 1.0f)
+                m_phase -= 1.0f;
+
+            buffer[i] = m_phase;
+            m_phase_stride = m_frequency * sample_duration;
+            
+        }
+
+    }
+
+    
+};

+ 73 - 0
Oscillator.h

@@ -0,0 +1,73 @@
+#pragma once
+
+namespace Audio
+{
+
+    const float SAMPLE_RATIO = 44100.0f;
+    const float PI_A = 3.14159265359f;
+    const size_t BUFFER_SIZE = 1024;
+
+    enum Shape
+    {
+        SINE
+    };
+
+    class Oscillator
+    {
+
+    protected:
+        float m_phase;
+        float m_phase_stride;
+        float m_frequency;
+        Shape m_shape;
+        float sample_duration;
+        
+        float buffer[BUFFER_SIZE] = {0};
+    
+    public:
+        
+        Oscillator() = default;
+        float frequency() { return m_frequency; }
+        Shape shape() { return m_shape; }
+        /* float phase() { return m_phase; }
+        float phase_stide() { return m_phase_stride; }
+        void phase(float phase) { m_phase = phase; }
+        void phase_stride(float phase_stride) { m_phase_stride = phase_stride; }
+ */
+        void* data() { return (void *)buffer; }
+
+
+        virtual void update() {} ;
+    };
+
+
+    class SquareOscillator: public Oscillator
+    {
+        
+        public:
+        SquareOscillator(float frequency) {
+            sample_duration = 1 / SAMPLE_RATIO;
+            m_frequency = frequency;
+            m_phase = 0;
+            m_phase_stride = frequency * sample_duration;
+        };
+
+        void update() override;
+    };
+
+
+    class SineOscillator: public Oscillator {
+
+        public: 
+        SineOscillator(float frequency) {
+            sample_duration = 1 / SAMPLE_RATIO;
+            m_frequency = frequency;
+            m_phase = 0;
+            m_phase_stride = frequency * sample_duration;
+        }
+
+        void update() override;
+
+    };
+};
+

+ 47 - 0
main.cpp

@@ -0,0 +1,47 @@
+#if defined(_WIN32)
+#define NOGDI  // All GDI defines and routines
+#define NOUSER // All USER defines and routines
+#endif
+
+#include "spdlog/spdlog.h"
+#include "raylib.h"
+#include "Oscillator.h"
+
+int main(int, char **)
+{
+    const int window_width = 1280;
+    const int window_height = 768;
+
+    InitWindow(window_width, window_height, "basic window");
+    SetTargetFPS(60);
+
+    Audio::SineOscillator primary = Audio::SineOscillator(440.0);
+
+    InitAudioDevice();
+    SetMasterVolume(0.3f);
+    SetAudioStreamBufferSizeDefault(Audio::BUFFER_SIZE);
+    AudioStream stream = LoadAudioStream(Audio::SAMPLE_RATIO, 32, 1);
+
+    PlayAudioStream(stream);
+
+    while (!WindowShouldClose())
+    {
+        if (IsAudioStreamProcessed(stream))
+        {
+            primary.update();
+            UpdateAudioStream(stream, primary.data(), Audio::BUFFER_SIZE);
+        }
+
+        BeginDrawing();
+        {
+            ClearBackground(WHITE);
+            DrawText("This is a raylib window!", 190, 200, 20, BLACK);
+        }
+        EndDrawing();
+    }
+
+    UnloadAudioStream(stream);
+    CloseAudioDevice();
+    CloseWindow();
+    return (EXIT_SUCCESS);
+}

+ 1 - 0
raylib

@@ -0,0 +1 @@
+Subproject commit 7392c4b0c592b45d113c7c869fbfdfc8d8de3d59

+ 1 - 0
spdlog

@@ -0,0 +1 @@
+Subproject commit 5a6b6cafa8d4aee3e6d0dd16a2cae9169141c831