Skip to content

Instantly share code, notes, and snippets.

@rngtm
Created November 24, 2021 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rngtm/8e1143abd7a06ab9f34d44530a7e5db6 to your computer and use it in GitHub Desktop.
Save rngtm/8e1143abd7a06ab9f34d44530a7e5db6 to your computer and use it in GitHub Desktop.
GLSL Sound 04 - 和音を作るテスト
#define TAU (2.*3.14159265)
float DB(float db) { return pow(10.0, db/10.0); }
float sawWave(float phase)
{
return fract(TAU*phase);
}
float chordWave(float phase, int chord, int key)
{
float wave;
for (int i = 1; i <= 32; i++)
{
if ((chord % 2) != 0)
{
wave += sawWave(phase * exp2(float(i+key)/12.0));
}
chord /= 2;
}
return wave;
}
vec2 mainSound(float time){
#define bpm 60.0
#define STEP 2.0
#define BAR_STEP 16.0
int keyCount = 4;
int[] keys = int[](
0,-2,0,2
);
float amp = fract(time * bpm/60.0);
amp = pow(amp, 4.0);
int noteCount = 4;
int[] notes = int[](
(1 << 1+12) + (1 << 6+12) + (1 << 10+12),
(1 << 6 ) + (1 << 6+12) + (1 << 10+12) + (1 << 10 + 12),
(1 << 1+12) + (1 << 6+12) + (1 << 10+12),
(1 << 6 ) + (1 << 6+12) + (1 << 10+12)
);
int noteIndex = (int(time*bpm/60.0/STEP)) % noteCount;
int chord = notes[noteIndex];
int key = -2 - 48 + keys[(int(time*bpm/60.0/BAR_STEP)) % keyCount];
float wave = chordWave(440.0/2.0 * time, chord, key) * amp;
return vec2(wave * DB(-3.0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment