LoginSignup
3
1

More than 5 years have passed since last update.

GLSLで音楽(今までの応用の一つ)

Last updated at Posted at 2018-06-29

アルペジオ

和音の構成音を順番にならすこと。

今までのスキルの応用

アルペジオをカノン進行でやってみます。音源は、自分的にはpianoに近くしてるけど、世間の評価が気になるところ。アルペジオできるなら、ベースラインは難しくないと思う。それで、コードは

#define BPM 120.
#define A (15./BPM)
#define B (240./BPM)

float sound(float f, float t)
{
  float gain = 0.0, a, b = 0.0;
  for(float j = 1.0; j < 8.0; j++){
      b += a = exp(-j *0.5);
      gain +=  a * sin( 6.2831 * f*j*t ) * exp(-t*1.5/(1.0-j*0.1));
  }
  gain *= 1.0 + 1.5*exp(-8.0*t);
  return gain / b;
}

float note2freq(int n)
{
    return 440.0*exp2((float(n)-69.0)/12.0);
}

int sequenceOrder(int s,float time)
{
    int n =-1;
    for(int i=0;i<=int(mod(time,B)/A);i++){
        if((s>>i&1)==1)n++;
    }
    return n;
}

float sequenceTime(int s,float time)
{
  float n =mod(time,A);
  for(int i=0;i<16;i++){
    if((s>>(int(time/A)-i)%16&1)==1)break;
    n+=A;
  }
  return n;
}

#define Scale int[](0,2,4,5,7,9,11,12,14,16,17) 
#define Kanon int[](1,5,6,3,4,1,4,5)
#define Key 60

vec2 mainSound( float time )
{
    int b = int(time/B);
    int s=int[](0x5111,0x1551,0x5151)[b%3];
    int o = sequenceOrder(s,time);
    float f = note2freq(Key + Scale[Kanon[b%8]-1 + o%3*2]);
    return vec2(sound(f,sequenceTime(s, time))) ; 
}

この音はshader toyで聞けます。

GLSLで音楽の記事

GLSLで音楽(はじめに)
GLSLで音楽(まずは、ドラムだ)
GLSLで音楽(メロディーいってみます)
GLSLで音楽(和音を使ってみる)

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1