Susie & Mental’s Lab 11

record_player_istock
import ddf.minim.*;
PImage img;
AudioPlayer player;
Minim minim;
void setup() {
  img=loadImage(“record_player_istock.jpg”);
  image(img, 0, 0);
  size(640, 480, P2D);
  minim=new Minim(this);
  player=minim.loadFile(“Weirddough – Remedy.mp3”);//locate file
  player.play();
}
void draw() {
  if (mousePressed==true) {
    player.close();
    minim.stop();
    super.stop();
  }
}

Lab #11: Dancing Queen

I was not entirely sure what the concept of this lab was, but I think we had to do something with audio? So I kept it simple and just rendered a set-up that included a dancing banana as my cursor, a discoball at the top and a dance floor. The background color was controlled by mousePressed and would change every time I clicked. And then finally I just loaded Dancing Queen by ABBA as the audio file to accompany my banana man.

bananaCode:

import ddf.minim.*;

Minim minim;
AudioPlayer player;

float time = 0;

color bColor = color(255, 255, 255);

PImage ball, banana;

void setup() {
size(1080, 800);

ball=loadImage(“discoball.png”);
banana=loadImage(“banana.gif”);

minim = new Minim(this);

player = minim.loadFile(“113-Dancing Queen.mp3”);

player.play();
}

void draw() {
background(bColor);
noCursor();

image(banana, mouseX, mouseY);
image(ball, 465, 0);

float x = 0;

while (x < width) {
line(x, 700 + 50 * noise(x / 100, time), x, height);
x = x + 1;
}

time = time + 0.02;
}

void mousePressed() {
bColor = color(random(255), random(255), random(255));
}

 

 

Morris’s Lab #11 Doc

[Processing]

processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port

import ddf.minim.*;

Minim drums;
AudioPlayer song;
boolean playedOnce = false;

void setup()
{
String portName = Serial.list()[4];
myPort = new Serial(this, portName, 9600);
size(100, 100);
background(147, 224, 255);
drums = new Minim(this);
song = drums.loadFile(“deng.mp3”);
}

void draw()
{
if ( myPort.available() > 0) { // If data is available,
val = myPort.read(); // read it and store it in val
println(val);
if (val == 49 && playedOnce == false) {
playDrums();
playedOnce = true;
}
else if (val == 48) {
playedOnce = false;
}
}
}

void playDrums() {
song.rewind();
song.play();
}

void stop()
{
song.close();
drums.stop();
super.stop();
}

 

[Arduino]

// constants won’t change. They’re used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin

const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
Serial.print(1);
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
Serial.print(0);
digitalWrite(ledPin, LOW);
}
delay(500);
}

Lab 11: Modifying a music player

SoundLab

Basically, started with the Example sketch called AnalyzeSound. Changed the jingle file to be “Wake me Up” by Avicii. Then I changed the color of the visual by tying the color to the average frequency of the song. This was a little complicated and required setting up the frequency in an array. On top of that I merged in the slider we used in another lab to control the blue-green color change.

Here’s the code:

/**
* This sketch demonstrates how to use an FFT to analyze
* the audio being generated by an AudioPlayer.
* <p>
* FFT stands for Fast Fourier Transform, which is a
* method of analyzing audio that allows you to visualize
* the frequency content of a signal. You’ve seen
* visualizations like this before in music players
* and car stereos.
*/

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

Minim minim;
AudioPlayer jingle;
FFT fft;

float average;

Slider slider = new Slider(100,70,300,20);

void setup()
{
size(512, 400, P3D);

minim = new Minim(this);

// specify that we want the audio buffers of the AudioPlayer
// to be 1024 samples long because our FFT needs to have
// a power-of-two buffer size and this is a good size.
jingle = minim.loadFile(“jingle.mp3”, 1024);

// loop the file indefinitely
jingle.loop();

// create an FFT object that has a time-domain buffer
// the same size as jingle’s sample buffer
// note that this needs to be a power of two
// and that it means the size of the spectrum will be half as large.
fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );

}

void draw()
{
background(0);
stroke(255);

slider.sliderSet();

// perform a forward FFT on the samples in jingle’s mix buffer,
// which contains the mix of both the left and right channels of the file
fft.forward( jingle.mix );

//jingle.setVolume(slider.sliderValue);

// println(jingle.getVolume());
float[] allVals = new float[0];

for(int i = 0; i < fft.specSize(); i++)

{

// draw the line for frequency band i, scaling it up a bit so we can see it
line( i, height, i, height – (fft.getBand(i)+0.5)*30 );
strokeWeight(3);
float a = map(fft.getBand(i),0,1,0,255);
stroke(average,average*1.8,average+slider.sliderValue);

allVals = append(allVals, a);

//println(map(fft.getBand(i),0,0.06,0,255));
//println(fft.getBand(i));
}

float allFloats = 0;
for(int i = 0; i < allVals.length; i++) {
allFloats += allVals[i];
}

average = allFloats / allVals.length;
println(average);
}

void loop () {

}

void mouseDragged() {
slider.sliderDrag();
}

void mouseReleased() {
slider.sliderDragging = false;
}