• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to make specific parts of this Synthesizer code run simultaneously?

 
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code:


You will be given an int[][] that represents a song in the following fashion:

Each int[] is a musical event, which can consist of one or more notes either playing or stopping.
Each int[][] is a series of numbers representing the actions in the event. The series begins with an int representing the time that the computer must wait before beginning the next event. After this, multiple actions may take place. Each action is placed into groups of 4 as follows: [1 to play, 0 to stop], [Channel], [Note], [Volume]
So, if the numbers were 250 1 0 60 120 1 1 64 100, it would play note 60 on channel 0 at volume 120 and note 64 on channel 1 at volume 100. It would then sleep for 250 milliseconds.
There may be any number of groups of 4 (+1) in a set. Thus, if there are 5 numbers, that would represent one note being played or stopped during an event. If there were 13 numbers, there would be 3 notes played or stopped (like a chord), etc. During a "stop" event, the Volume int is ignored.
Your goal is to, given a properly formatted array, create the method public void play (int[][] song). Here is some code with a song that you can copy/paste to test out your code:



So far, I have:



The brackets in the brackets are able to have 5 or more values. If there are more than 5 values, the a chord should be played. However, this code only plays each note individually. How do I fix this?
 
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like that's covered in the tutorial: Synthesizing Sound.
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Seems like that's covered in the tutorial: Synthesizing Sound.




Sorry, I know how the synthesizing itself works, I just don't know how to get the notes to play at the same time with the for loop when there is a chord. Like if it's {10,0,1,40,100}, then only one note should be played. But if it is {190,1,9,37,100,1,1,40,100}, two notes should be played at the same time with the for loop.
 
Paul Clapham
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's in the tutorial which I linked to, in the section headed "Voices".
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Yes, that's in the tutorial which I linked to, in the section headed "Voices".



Oh I didn't notice that lol... Do you have any advice on which part of my code to start at to fix this?
 
Paul Clapham
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I don't. I have no experience with Synthesizer, I just looked for a relevant tutorial online and found one. I didn't even pause to see if the tutorial came with sample code -- most of Oracle's tutorials do, so maybe this one does as well. Usually what I do when I'm learning a new area of Java which I know nothing about is this: Find a tutorial with sample code, and then tinker with the sample code, changing it until it turns into the code I wanted to write in the first place.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!