Week 14 Final Progress.

Summary:

For this week’s progress, the original plan was to accomplish at least 70% of the production of the project without counting the aesthetic part. However, while working on the project, there were several challenges that I encountered and currently, about 50% of the project is completed. I have started and almost completed the general function of the piano and also started implementing basic features that do not take that much time.

Progress:

import ddf.minim.*;
import processing.sound.*;
Game game;
String filename;
Minim minim;
AudioPlayer[] notes = new AudioPlayer[89];
int phase = 0;


void setup()
{
  size(910, 700);
  minim = new Minim(this);
  for (int i = 1; i < 89; i++){
      filename =  i + ".mp3";
      notes[i] = minim.loadFile(filename);
  }
  game = new Game(notes);
}

void draw(){
  game.drawGame();
  if(keyPressed)
  {
    if(key == 'd')
    {
      println('d');
      game.play0(); 
    }
    else if(key == 'f')
    {
      println('f');
      game.play1();
    }
    else if(key == 'g')
    {
      println('g');
      game.play2();
    }
    else if(key == 'h')
    {
      println('h');
      game.play3();
    }
    
  }
}


void keyPressed()
  {
    if(key == CODED)
    {
      if(keyCode == RIGHT)
      {
        game.moveright();
      }
      else if(keyCode == LEFT)
      {
        game.moveleft();
      }
      else if(keyCode == 'd')
      {
        println("here");
      }
      else if(key == 'f')
      {
        println("here");
      }
      else if(key == 'g')
      {
        println("here");
      }
      else if(key == 'h')
      {
        println("here");
      }
    }
  }
  
  void mouseClicked()
  {
    game.mouseClicked();
  }
class Game{
  Tile[] tilearray = new Tile[89];
  int screenx, screeny;
  int numoftiles = 89;
  Minim minim;
  AudioPlayer[] notes;
  int screenPosition;
  int whitekeys = 0;
  int blackkeys = 0;
  int screen = 0;
  int screenvelo = 120;
  int position = 0;
  Tile[][] notearray = new Tile[22][4];
  boolean isrecording;
  int phase = 0;
  Record record = new Record();
  
  Game(AudioPlayer[] sound){
    notes = sound;
    for( int i = 1; i < numoftiles; i++){
      if(i == 2 || (i - 5)%12 == 0 || (i - 7)%12 == 0 || (i - 10)%12 == 0 || (i - 12)%12 == 0 || (i - 14)%12 == 0)
      {
        tilearray[i] = new Tile(i, notes[i], 1, blackkeys);
        blackkeys++;
      }
      else{
        tilearray[i] = new Tile(i, notes[i], 0, whitekeys);
        whitekeys++;
      }  
    }
    int j = -1; 
    int k = 0;
    for(int i = 1;  i < numoftiles; i++)
    {
      if( (i-1)% 4 == 0)
      {
        j++;
      }
      k = (i - 1)%4;
      notearray[j][k] = tilearray[i];
    }
  }
  
  void moveright()
  {
    if(screen < 2540)
    {
      println(screen);
      position++;
      screen += screenvelo;
      for( int i = 1; i < numoftiles; i++)
      {
        tilearray[i].position(screen);
      }
    }
  }
  
  void moveleft()
  {
    if(screen > 0)
    {
      screen -= screenvelo;
      position--;
      for( int i = 1; i < numoftiles; i++)
      {
        tilearray[i].position(screen);
      }
    }
  }
  
  void play0()
  {
    notearray[position][0].playtile();
  }
  
  void play1()
  {
    notearray[position][1].playtile();
  }
  
  void play2()
  {
    notearray[position][2].playtile();
  }
  
  void play3()
  {
    notearray[position][3].playtile();
  }
   
  void indicators()
  {
    color c = color(255, 0, 0);
    for(int i = 0; i < 4 ; i++)
     {
       notearray[position][i].indicator(c);
     }
  }
  
  void startrecord()
  {
    if(isrecording == false)
    {
      isrecording = true;
    }
  }
 
  void mouseClicked()
  {
    
  }
   
  void drawGame()
  {
    for( int i = 1; i < numoftiles; i++){
      tilearray[i].drawtile();
    }
    indicators();
  }
}
class NotePlay{
  float posx, posy;
  AudioPlayer sound;
  boolean isSelected;
  int posinarray;
  
  NotePlay(AudioPlayer note, int position)
  {
    sound = note;
    posinarray = position;
  }
  
  void playNote()
  {
    stroke(255); //Change to yellow or other color
    sound.play(0);
    stroke(0);
  }
  
  
  void drawNote()
  {
   //circle();
  }
 
}
class Record{
  int size, capacity;
  NotePlay[] record;

  
  Record()
  {
    capacity = 20;
    size = 0;
    record = new NotePlay[capacity];
  }
  
  void addNote(AudioPlayer sound)
  {
    if(size + 1 == capacity)
    {
      capacity *= 2;
      NotePlay[] copy = new NotePlay[capacity];
      for( int i = 0; i < size; i++)
      {
        copy[i] = record[i];
      }
      record = copy;
    record[size] = new NotePlay(sound, size);
    size++;
    }
  }
  
  void deleteNote(int position)
  {
    
  } 
}
class Tile{
  float type, number;
  float x, y;
  float tilew, tileh;
  AudioPlayer sound;
  AudioPlayer[] notes = new AudioPlayer[88]; 
  int keytype;
  color c;
  float xposition;

  
  Tile(int num, AudioPlayer note, int type, int xpos){
    keytype = type;
    if(keytype == 0)
    {
      tilew = 65;
      x = xpos * tilew;
      tileh = 330;
      c = color(255);
      
    }
    else
    {
      if(xpos == 0)
      {
       tilew = 50; 
       x = xpos + (tilew/2);
      }
      else
      {
        tilew = 50; 
        if(xpos%5 == 1)
        {
          x = 3 + 7*(xpos/5);
          
        }
        else if(xpos%5 == 2)
        {
          x = 4 + 7*(xpos/5);
          
        }
        else if(xpos%5 == 3)
        {
          x = 6 + 7*(xpos/5);
          
        }
        else if(xpos%5 == 4)
        {
          x = 7 + 7*(xpos/5);
        }
        else if(xpos%5 == 0)
        {
          x = 8 + 7*((xpos/5)-1);
          //println(x);
        }
        //x++;
        x *= 65;
        x -= tilew/2;
        println(x);
        
      }
      tileh = 250;
      c = color(100);  
    }
    y = 300;
    sound = note;
    xposition = x;
  }
  
  void playtile(){
    if( game.isrecording == true)
    {
      game.record.addNote(sound);
    }
    sound.play(0);
  }
  
  void position(int screenposition){
    xposition = x - screenposition;
  }
  
  void indicator(color c1)
  {
    stroke(c1);
    noFill();
    rect(xposition, y, tilew, tileh);
  }
  
  void drawtile(){
    stroke(0);
    fill(c);
    rect(xposition, y, tilew, tileh);
  }
}

Progress Work:

I have started working on the general look of the piano. Also, I have implemented the basic functions of a piano such as playing the notes and traveling through the piano. The files for the notes have been all uploaded. The selection for the notes being played has also been implemented. Finally, I have been working on creating the recording feature of the piano.

Challenges:

For this week’s progress, the main challenge was finding and uploading the files for each note. This process took me much longer than I expected. The process required work for more than one day. This was because I could not find files for piano notes. When I finally did, the files required editing. I edited (cropped, change format). This process was much longer because I had to edit a total of 88 files. Another challenge I faced was ordering the tiles to resemble the form of a piano. This part is not 100% accomplished but it is only missing one part. This process required calculation for the position of the keys because pianos have both white and black keys and are distributed unevenly throughout.

Plan:

My plan for accomplishing this project on time includes finishing the technical aspect for this Friday. (All coding in Processing and Arduino). Then work on the aesthetic part on Saturday and Sunday so that I can accomplish finishing this project on time.

Leave a Reply