Last active
November 23, 2021 06:24
-
-
Save rngtm/6de187e7a5039a6552fbb6753c642258 to your computer and use it in GitHub Desktop.
GLSL Sound : 遺跡っぽいBGM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float Tone(int i) | |
{ | |
return 420.0 * exp2(float(i + 2) / 12.0); | |
} | |
int g_key = 0; | |
#define PI acos(-1.0) | |
float SinWave(float time, float freq) | |
{ | |
return sin(2.0 * PI * time * freq); | |
} | |
float SawWave(float time, float freq) | |
{ | |
return fract(time * freq); | |
} | |
float MonoWave(float time, float freq) | |
{ | |
return SinWave(time, freq); | |
} | |
// nの階乗を求める | |
float Factorial(int n) | |
{ | |
int result = 1; | |
for (int i = 2; i <= n; i++) | |
{ | |
result *= i; | |
} | |
return float(result); | |
} | |
// メインのメロディ | |
float Wave1(float time, float freq) | |
{ | |
freq *= exp2(float(g_key) / 12.0); | |
float s = 1.0; | |
float wave = 0.0; | |
int fi = 1; | |
float e = -1.0; | |
for (int i = 0; i < 3; i++) | |
{ | |
wave += s * SinWave(time, freq * Factorial(fi)) * exp2(e); | |
s = -s; | |
fi += 2; | |
e -= 0.8; | |
} | |
float th = 0.01; | |
wave = clamp(wave, -th, th) / th; | |
return wave; | |
} | |
// オルガンっぽい音 | |
float OrganWave(float time, float freq) | |
{ | |
float wave; | |
int n = 8; | |
for (int i = 0; i < n; i++) | |
{ | |
wave += SinWave(time, freq); | |
freq *= exp2(1.0 + 1.0 / 12.0 / float(n)); | |
} | |
wave /= float(n); | |
return wave; | |
} | |
struct Note | |
{ | |
int index; | |
float freq; | |
float volume; | |
}; | |
struct AudioSettings | |
{ | |
float tempo; | |
}; | |
Note GetNote(int index, float volume) | |
{ | |
Note n; | |
n.freq = Tone(index); | |
n.volume = volume; | |
return n; | |
} | |
float remap(float x, float a, float b, float c, float d) | |
{ | |
return (x - a) * (d - c) / (b - a) + c; | |
} | |
float hihat(float time) | |
{ | |
return 0.5*sin(2e6*time); | |
} | |
Note GetArp2(float time) | |
{ | |
float tempo = 0.25; | |
time *= tempo; | |
int noteCount = 8; | |
int noteIndex = int(time) % noteCount; | |
// メロディ | |
Note[] notes = Note[] | |
( | |
GetNote(0, 1.), | |
GetNote(8, 1.), | |
GetNote(7, 1.), | |
GetNote(0, 0.3) | |
); | |
return notes[noteIndex]; | |
} | |
// オルガンっぽい音 | |
float audio2(float time) | |
{ | |
float wave; | |
Note note = GetArp2(time); | |
float freq = note.freq ; | |
//freq *= exp2(float(g_key) / 12.0); | |
wave += OrganWave(time, freq); | |
wave += OrganWave(time, freq * 1.002); | |
wave += OrganWave(time, freq * 1.007); | |
wave /= 3.0; | |
return wave; | |
} | |
float HighHat(float time) | |
{ | |
float unit = 4.0; | |
float vel = sin(floor(time * unit)*1e3) * 0.5 + 0.5; | |
float amp = time - floor(time * unit) / unit; | |
amp = 1.0 - amp; | |
amp = pow(amp, 64.0) * vel; | |
return fract(sin(time*1e3)*1e6) * amp; | |
} | |
#define VOLUME_1 0.12 | |
#define VOLUME_2 0.1 | |
#define VOLUME_3 0.5 | |
// ハイハット | |
float audio3(float time) | |
{ | |
return HighHat(time); | |
float tempo = 2.0; | |
time -= floor(time * tempo) / tempo; | |
float t = max(0.0, 1.0 - time * 4.0); | |
t = pow(t, 4.0); | |
float freq = 440.0 * exp2(-2.); | |
freq *= mix(0.5, 1.0, t); | |
return SinWave(time, freq); | |
} | |
// メインのメロディg | |
float audio1(float time) | |
{ | |
float waveTime = time; | |
float tempo = 4.0; | |
time *= tempo; | |
int noteCount = 8; | |
int noteIndex = int(time) % noteCount; | |
int keyCount = 4; | |
int keyRepeat = 4; | |
int keyIndex = int(time / float(keyRepeat * noteCount)) % keyCount; | |
int keyOffset = 0; | |
int[] KeyTable = int[] | |
( | |
0, | |
0, | |
0, | |
6 | |
); | |
// メロディ | |
Note[] notes = Note[] | |
( | |
GetNote(0, 1.), | |
GetNote(7, 1.), | |
GetNote(3, 1.), | |
GetNote(10, 1.), | |
GetNote(0, 1.), | |
GetNote(7, 1.), | |
GetNote(3, 1.), | |
GetNote(2, 1.) | |
); | |
g_key = KeyTable[keyIndex] + keyOffset; | |
time = fract(time); | |
Note note = notes[noteIndex]; | |
float wave = | |
Wave1(waveTime, note.freq + (0.003 * SinWave(time, 4.0))) | |
* exp(-3.0 * time) * note.volume; | |
return wave; | |
} | |
vec2 mainSound(float time) | |
{ | |
time -= 0.1; | |
if (time < 0.0) return vec2(time); | |
float delay = 0.2; | |
float wave = 0.0; | |
float wave2 = audio2(time); | |
float wave3 = audio3(time); | |
// // delay | |
float wave1; | |
for (int i = 5; i >= 1; i--) | |
{ | |
wave1 += audio1(time - delay * float(i)); | |
wave1 *= 0.4; | |
} | |
wave1 += audio1(time); | |
wave = | |
wave1 * VOLUME_1 + | |
wave2 * VOLUME_2 + | |
wave3 * VOLUME_3; | |
return vec2(wave); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment