Guitar Simulation with Keyboard

Uses arrays, if and for loops

Page 1 of 1

1 Replies - 6245 Views - Last Post: 18 April 2009 - 03:20 PM Rate Topic: -----

#1 mandee2422  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-April 09

Guitar Simulation with Keyboard

Post icon  Posted 17 April 2009 - 05:22 PM

I am taking a beginner Computer Science class in college, focusing on Java. We have to create a guitar simulation, supporting 37 notes, which are:

String keyboard = "1234567890qwertyuiopasdfghjklzxcvbnm,";

The ith character must correspond to a frequency of 440 x 2^((i-24)/12). We already have access to StdAudio, StdDraw, RingBuffer and GuitarString (the last 2 model the string vibration) Here is my code so far. We need to use an array of 37 GuitarString objects:



public class GuitarHero {
	  public static void main(String[] args) {
		
		  String keyboard ="1234567890qwertyuiopasdfghjklzxcvbnm,";
		  int index;

		  double sample = 0.0;

		  GuitarString[] strings = new GuitarString [37];
		  
		for(int a =0; a<37; a++){
			double concert = 440.0 * Math.pow(2, (a - 24)/12); 
			strings[a] = new GuitarString(concert); }
		 
		  while (true) {

			  // check if the user has typed a key; if so, process it   
			  if (StdDraw.hasNextKeyTyped()) {
				  char key = StdDraw.nextKeyTyped();
				  index = keyboard.indexOf(key);
				  if	  (index >= 0 && index < 37)  { strings[index].pluck(); }
			  }
		  
			  // compute the superposition of samples
			  for(int a=0; a < 37; a++){
				sample = strings[a].sample();
  
			  // send the result to the sound card
				StdAudio.play(sample);}
  
			  // advance the simulation of each guitar string by one step   
			  for(int a=0; a < 37; a++){
				strings[a].tic(); }
		  
		  }
	   }
  }



When I run the program, and press keys all I get is this static sound that never ends. I understand that for the first for loop, the int a must correspond to the index, as should int i and int j, but I am not sure how to go about this. Also I know the increasing operator ++ isn't quite right, but I'm not sure what else to use. This is my first (and hopefully only) Java class. Any help would be greatly appreciated!!!!

Amanda

This post has been edited by mandee2422: 17 April 2009 - 05:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Guitar Simulation with Keyboard

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Guitar Simulation with Keyboard

Posted 18 April 2009 - 03:20 PM

View Postmandee2422, on 17 Apr, 2009 - 04:22 PM, said:

I am taking a beginner Computer Science class in college, focusing on Java.

And you qualified your post as "intermediate" ?
On the other hand your frequency stuff is "expert" stuff to me
And where is the definition of your GuitarString class ?

This post has been edited by pbl: 18 April 2009 - 03:25 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1