Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

about SinOsc array: difficulty in stop the sound (sounds stupid!)

$
0
0

Hi, I'm writing a coding for assignment, just one last step: stop the sound. I use an array to store the sound, but when I call it to stop ( sin[y].stop(); ), it would not! It keeps saying Null Pointer Exception. I know the reason that it is null before I selected a square, but it does not work either when I use specific value (sin[0].stop(); etc.).

Here's the full coding.

import processing.sound.*;
SinOsc [] sin;

int t=0;
int spacing = 10;
int rectSize = 40;
boolean [][] selected;
int [] notes = {880, 990, 1110, 1320, 1480};

void setup()
{
  size(810,500);
  background(129,199,212);

  selected = new boolean[16][5];
  for (int i = 0; i < selected.length; i++)
  {
    for(int j = 0; j < selected[i].length; j++)
    {
    selected[i][j] = false;
    }
  }
  sin = new SinOsc[5];
}
void draw()
{
  if(frameCount%20==1)
  {
    t=(t+1)%16;

    sin[0].stop();    //the question is here!~

    for (int y=0; y < 5; y++)
      {
        if(selected[t][y])
        {
        sin[y] = new SinOsc(this);
        sin[y].freq(notes[y]);
        sin[y].play();
        }
      }
  }

 //draw rectangles
  for(int i = 0; i < 16; i++)
  {
    for (int j = 0; j < 5; j++)
    {
    int a = spacing*(i+1)+rectSize*i;
    int b = spacing*(j+1)+rectSize*j;
    if(selected[i][j]) fill(150,150,255);               //selected = blue
    else fill(254,223,225);                             //unselected = pink
    if(i==t) fill (208,90,110);                         //red bar according to time
    noStroke();
    rect(a, b, rectSize, rectSize);
    }
  }

  //interaction buttons
  fill(254,223,225);
  rect(270, 2*height/3,80,80);
  rect(375, 2*height/3,80,80);
  rect(480, 2*height/3,80,80);

  fill(208,90,110);
  rect(285, 2*height/3+15, 50, 50);
  triangle(390,2*height/3+10,390, 2*height/3+70, 440,2*height/3+40);
  translate(105,0);
  triangle(390,2*height/3+40, 440, 2*height/3+10, 440, 2*height/3+70);
  rect(380, 2*height/3+10,10,60);
}

void mousePressed()
{
  int redX = mouseX/(spacing+rectSize);
  int redY = mouseY/(spacing+rectSize);

  if (redX>-1 && redX<selected.length && redY>-1 && redY<selected[redY].length)
  selected[redX][redY]=!selected[redX][redY];

  //buttons
  if(dist(mouseX,mouseY,310,2*height/3+40)<40) noLoop();         //pause
  else if(dist(mouseX,mouseY,415,2*height/3+40)<40) loop();      //start
  else if(dist(mouseX,mouseY,520,2*height/3+40)<40) t=0;         //restart: change the variables to the initial value
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles