8STEP SEQUENCER

DSC09626


あまりにベタだなぁ、と思いつつも8STEP SEQUENCER。
パラメータはTempo、Duration、Pitchだけ。あと、再生/停止。


DSC09628 DSC09624


とりあえず動けば良いのでTempoとDurationのツマミはミノムシクリップ使ってしまう。
青いツマミがPitch。各STEPのボタン押したら、そのSTEPのエディットモードに入ってPitchを可変できる。
そのSTEPのエディットモードから抜けるには別のSTEPのボタン押すしかない。
さすがにI/O足りないので再生位置のLEDは74HC595で。


DSC09635


以下、コード。
たいしたことは何もやってないけど。

//Digital In
#define STEP0 0
#define STEP1 1
#define STEP2 2
#define STEP3 3
#define STEP4 4
#define STEP5 5
#define STEP6 6
#define STEP7 7 
#define PLAYMODE 10
#define AUDIOOUT 9

//Analog In
#define TempoInput 0
#define DurationInput 1
#define FreqInput 2

//Step LED
#define LatchPIN 8
#define ClockPIN 12
#define DataPIN 11

int playing = true;
int pitch[] = {100, 100, 100, 100, 100, 100, 100, 100};
int currentStep = 0;
int duration = 100;
int tempo = 100;

void setup() {
  pinMode (STEP0, INPUT);
  pinMode (STEP1, INPUT);
  pinMode (STEP2, INPUT);
  pinMode (STEP3, INPUT);
  pinMode (STEP4, INPUT);
  pinMode (STEP5, INPUT);
  pinMode (STEP6, INPUT);
  pinMode (STEP7, INPUT);                
  pinMode (PLAYMODE, INPUT);

  digitalWrite(STEP0, HIGH);
  digitalWrite(STEP1, HIGH);
  digitalWrite(STEP2, HIGH);
  digitalWrite(STEP3, HIGH);
  digitalWrite(STEP4, HIGH);
  digitalWrite(STEP5, HIGH);
  digitalWrite(STEP6, HIGH);
  digitalWrite(STEP7, HIGH);
  digitalWrite(PLAYMODE, HIGH);

  pinMode (AUDIOOUT, OUTPUT);
  
  pinMode(LatchPIN, OUTPUT);
  pinMode(ClockPIN, OUTPUT);
  pinMode(DataPIN, OUTPUT);
} 

void loop() {
  for (int i=0; i<8; i++) {
    duration = analogRead(DurationInput);
    tempo = (analogRead(TempoInput) / 2);
    
    if     (digitalRead(STEP0) == LOW) currentStep = 0;
    else if(digitalRead(STEP1) == LOW) currentStep = 1;
    else if(digitalRead(STEP2) == LOW) currentStep = 2;
    else if(digitalRead(STEP3) == LOW) currentStep = 3;
    else if(digitalRead(STEP4) == LOW) currentStep = 4;
    else if(digitalRead(STEP5) == LOW) currentStep = 5;
    else if(digitalRead(STEP6) == LOW) currentStep = 6;
    else if(digitalRead(STEP7) == LOW) currentStep = 7;

    if     (currentStep == 0) pitch[0] = analogRead(FreqInput);
    else if(currentStep == 1) pitch[1] = analogRead(FreqInput);
    else if(currentStep == 2) pitch[2] = analogRead(FreqInput);
    else if(currentStep == 3) pitch[3] = analogRead(FreqInput);
    else if(currentStep == 4) pitch[4] = analogRead(FreqInput);
    else if(currentStep == 5) pitch[5] = analogRead(FreqInput);
    else if(currentStep == 6) pitch[6] = analogRead(FreqInput);
    else if(currentStep == 7) pitch[7] = analogRead(FreqInput);
    
    if (digitalRead(PLAYMODE) == LOW) playing = !playing;
    if (playing) freqout(pitch[i], duration);
    
    digitalWrite(LatchPIN, LOW);
    shiftOut(DataPIN, ClockPIN, MSBFIRST, 1<<i);
    digitalWrite(LatchPIN, HIGH);
    
    delay(tempo);
  }
}

//http://www.arduino.cc/playground/Main/Freqout
void freqout(int freq, int t)              // freq in hz, t in ms
{
  int hperiod;                             //calculate 1/2 period in us 
  long cycles, i;
  pinMode(AUDIOOUT, OUTPUT);               // turn on output pin
  
  hperiod = 500000 / freq - 7;             // subtract 7 us to make up for digitalWrite overhead
  
  cycles = ((long)freq * (long)t) / 1000;  // calculate cycles
  
  for (i=0; i<= cycles; i++) {             // play note for t ms  
    digitalWrite(AUDIOOUT, HIGH);
    delayMicroseconds(hperiod);
    digitalWrite(AUDIOOUT, LOW);
    delayMicroseconds(hperiod - 1);        // - 1 to make up for fractional microsecond in digitaWrite overhead 
  }
  pinMode(AUDIOOUT, INPUT);                // shut off pin to avoid noise from other operations
}


DSC09620 DSC09618


出力低いし。音しょぼいし。
もっと色々考えないとダメだね。