sound_engine_defs.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #define NUM_CHANNELS 4
  5. #define RANDOM_SEED 0xf31782ce
  6. #define ACC_BITS 23
  7. #define ACC_LENGTH (1 << (ACC_BITS - 1))
  8. #define OUTPUT_BITS 16
  9. #define WAVE_AMP (1 << OUTPUT_BITS)
  10. #define SINE_LUT_SIZE 256
  11. #define SINE_LUT_BITDEPTH 8
  12. #define MAX_ADSR (0xff << 17)
  13. #define MAX_ADSR_VOLUME 0x80
  14. #define BASE_FREQ 22050
  15. #define envspd(eng, slope) ((slope) != 0 ? (((uint64_t)MAX_ADSR / ((slope) * (slope)*256 / 8)) * BASE_FREQ / eng->sample_rate) : ((uint64_t)MAX_ADSR * BASE_FREQ / eng->sample_rate))
  16. typedef enum
  17. {
  18. SE_WAVEFORM_NONE = 0,
  19. SE_WAVEFORM_NOISE = 1,
  20. SE_WAVEFORM_PULSE = 2,
  21. SE_WAVEFORM_TRIANGLE = 4,
  22. SE_WAVEFORM_SAW = 8,
  23. SE_WAVEFORM_NOISE_METAL = 16,
  24. SE_WAVEFORM_SINE = 32,
  25. } SoudEngineWaveformType;
  26. typedef enum
  27. {
  28. SE_ENABLE_FILTER = 1,
  29. SE_ENABLE_GATE = 2,
  30. SE_ENABLE_RING_MOD = 4,
  31. SE_ENABLE_HARD_SYNC = 8,
  32. SE_ENABLE_KEYDOWN_SYNC = 16, // sync oscillators on keydown
  33. } SoundEngineFlags;
  34. typedef enum
  35. {
  36. FIL_OUTPUT_LOWPASS = 1,
  37. FIL_OUTPUT_HIGHPASS = 2,
  38. FIL_OUTPUT_BANDPASS = 3,
  39. /* ============ */
  40. FIL_MODES = 4,
  41. } SoundEngineFilterModes;
  42. typedef enum
  43. {
  44. ATTACK = 1,
  45. DECAY = 2,
  46. SUSTAIN = 3,
  47. RELEASE = 4,
  48. DONE = 5,
  49. } SoundEngineEnvelopeStates;
  50. typedef struct
  51. {
  52. uint8_t a, d, s, r, volume, envelope_state;
  53. uint32_t envelope, envelope_speed;
  54. } SoundEngineADSR;
  55. typedef struct
  56. {
  57. int32_t f, q, p;
  58. int32_t b0, b1, b2, b3, b4; // filter coefficients
  59. } SoundEngineFilter;
  60. typedef struct
  61. {
  62. uint32_t accumulator;
  63. uint32_t frequency;
  64. uint8_t waveform;
  65. uint16_t pw;
  66. uint32_t lfsr;
  67. SoundEngineADSR adsr;
  68. uint16_t flags;
  69. uint8_t ring_mod, hard_sync; // 0xff = self
  70. uint8_t sync_bit;
  71. uint8_t filter_mode;
  72. uint16_t filter_cutoff;
  73. uint16_t filter_resonace;
  74. SoundEngineFilter filter;
  75. } SoundEngineChannel;
  76. typedef struct
  77. {
  78. SoundEngineChannel channel[NUM_CHANNELS];
  79. uint32_t sample_rate;
  80. uint16_t *audio_buffer;
  81. uint32_t audio_buffer_size;
  82. bool external_audio_output;
  83. uint8_t sine_lut[SINE_LUT_SIZE];
  84. } SoundEngine;