読者です 読者をやめる 読者になる 読者になる

Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

リング変調と振幅変調

サウンド
f:id:aa_debdeb:20161228225217j:plain
/**
* Ring Modulation vs. Amplitude Modulation
*
* @author aa_debdeb
* @date 2017/01/02
*/

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

Minim minim;
AudioOutput out;
FFT fft;

Oscil carrier, mod;
boolean isAM;

void setup(){
  size(500, 500);
  
  minim = new Minim(this);
  out = minim.getLineOut();
  fft = new FFT(out.bufferSize(), out.sampleRate());
  carrier = new Oscil(440, 0.5, Waves.SINE);
  mod = new Oscil(200, 0.5, Waves.SINE);
  mod.patch(carrier.amplitude);
  carrier.patch(out);
  isAM = false;
  mousePressed();
}

void draw(){
  if(isAM){
    background(255);
    stroke(0);
  } else {
    background(0);
    stroke(255);
  }
  strokeWeight(1);
  for(int i = 0; i < out.bufferSize() - 1; i++){
    line(i, 50 + out.left.get(i) * 50, i + 1, 50 + out.left.get(i + 1) * 50);
    line(i, 150 + out.right.get(i) * 50, i + 1, 150 + out.right.get(i + 1) * 50);
  }  
  
  translate(0, 200);
  fft.forward(out.mix);
  for(int i = 0; i < 100; i++){
    float w = map(i, 0, 100, 0, width);
    float band = fft.getBand(i) * 2;
    line(w, height, w, height - band);
  }
}

void mouseMoved(){
  float carrierFreq = map(mouseX, 0, width, 200, 2000);
  carrier.setFrequency(carrierFreq);
  float modFreq = map(mouseY, 0, height, 0, 2000);
  mod.setFrequency(modFreq);
}

void mousePressed(){
  isAM = !isAM;
  if(isAM){ // amplitude modulation
    mod.setAmplitude(0.5);
    mod.offset.setLastValue(0.5);
  } else { // ring modulation
    mod.setAmplitude(1.0);
    mod.offset.setLastValue(0);  
  }
}