Press "Enter" to skip to content

Sélecteur de son par analyse de spectre / Processing

Le programme analyse le spectre sonore du microphone en 10 sections. Il lance la lecture d’un fichier mp3 correspondant à l’index de la plus haute fréquence enregistrée.

Source: soundSpectrumSelector.pde / Processing 3.5.3 + librairie Minim

import ddf.minim.*;
import ddf.minim.analysis.*;

Minim minim;
AudioInput micro;
AudioPlayer[] filePlayer = new AudioPlayer[10];
int mp3Id;
FFT fftLin;

float height13;
float height23;

float spectrumScale = 10;
float bandFrequency = 0;
float centerFrequency = 0;
float soundSample;
boolean onPlaying = false;
int avgWidth;

void setup(){
  size(800,600);
  background(255);
  
  height13 = height/3;
  height23 = height/3*2;
  
  // Lance l'analyse du son //////////////////////////////
  minim = new Minim(this);
  micro = minim.getLineIn();
  soundSample = micro.sampleRate();  // choix de la fréquence maximale à analysée
  fftLin = new FFT(micro.bufferSize(), soundSample);
  
  // Charge un son externe /////////////////////////////////:
  for(int i=0; i<filePlayer.length; i++){
    filePlayer[i] = minim.loadFile((i+1) + ".mp3", 2048);
  }
  
  // Definition du nombre de section du spectre pour les moyennes
  fftLin.linAverages(filePlayer.length);
}

void draw(){
  background(255);
  stroke(0,255,0);
  noFill();
  
  fftLin.forward(micro.mix);
  
  processDrawSound(fftLin);
}

void processDrawSound(FFT fftSelect){
    noStroke();
    fill(0,255,0);
    float allTimeHigh = 0;
    int allTimeHighId = 0;
    
    // Visualisation du spectre: Calcul de la largeur des bandes moyennes
    avgWidth = width/fftSelect.avgSize();

    for(int i=0; i<fftSelect.avgSize(); i++){
      centerFrequency = fftSelect.getAvg(i);
      
      if(allTimeHigh < centerFrequency * soundSample){
        allTimeHigh = centerFrequency * soundSample;
        allTimeHighId = i;
        
      }
      
      //Visualisation des bandes moyennes du spectre
      float xA, yA, xB, yB;
      rectMode(CORNERS);
      xA = i*avgWidth;
      yA = height23 - fftSelect.getAvg(i)*spectrumScale/2;
      xB = i*avgWidth + avgWidth;
      yB = height23 + fftSelect.getAvg(i)*spectrumScale/2;
      rect(xA, yA, xB, yB);
    }
    
    println("allTimeHigh: " + allTimeHigh + "/" + soundSample);
    println("allTimeHighId: " + allTimeHighId);
    
    
    if(!onPlaying){
      filePlayer[allTimeHighId].play(); 
    }
    
    // Vérifier si un des sons du tableau est en lecture
    onPlaying = false; //On suppose qu'aucun son n'est lu
    for(int i = 0; i<filePlayer.length; i++){
      if(filePlayer[i].isPlaying()){
        onPlaying = true; //Dés q'une lecture de son est repérée, on modifie onPlaying
      }
    }
}

Comments are closed.