• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to write Java that produces musical sounds

 
Ranch Hand
Posts: 248
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I write a Java program that makes musical sounds? In particular, I'd like to write code that would take one second to play C, then D, then C#, and then C again, taking exactly one quarter second for each note. I'd also like to write code that does the same thing for E-F#-F-E, and that does the same thing for G#-A#-A-G#. Can anyone tell me how I can do this?
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you done a search on that topic?  I see several java programs on stackexchange that do that.
 
Kevin Simonson
Ranch Hand
Posts: 248
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Have you done a search on that topic?  I see several java programs on stackexchange that do that.


I did a search on "java music sounds" and it came up with two results that looked like they were worth exploring, "https://www.youtube.com/watch?v=VMSTTg5EEnY" and "https://www.youtube.com/watch?v=3q4f6I5zi2w". But both of those involved reading external audio files. All I want to do is have my Java program produce a simple melody consisting of D-C-C#-D at certain times in my program, and F#-E-F-F# at other times, and A#-G#-A-A# at other times. (This isn't what I asked in my first post; I had it backwards then.) So I don't have an audio file. How can I generate these three melodies and have the Java program play them? Is there some other string I should do a Google search on to find web pages that will help me with that?

Now I did find an article at "https://docs.oracle.com/javase/tutorial/sound/index.html". Is this a good website to figure out how to generate my short melodies?
 
Marshal
Posts: 28263
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Oracle tutorial wants to teach you everything there is to know about sound production in Java. Normally the Oracle tutorials include sample text but this one is twenty or thirty pages of detailed information. So, not what you really want.

I searched for simpler tutorials and didn't come up with much except, I think what you want to focus on is MIDI processing. There are some fairly simple tutorials out there, even some with sample code, but I didn't see any which explained how to play a C#. (Apparently there are 127 notes and they are mapped to the notes on your piano in some way.) But of course playing musical notes does involve telling the speaker to start playing and telling it to stop playing and telling it how loud to play and so forth, so it isn't all that simple.

You could try Java | MIDI Introduction or MIDI basics and a lot of people liked About Java's Sound Stuff.

I didn't look at any Youtube tutorials because I'm a text-oriented person and I'm not good at understanding video explanations of text.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My query to Google returned this site: https://codereview.stackexchange.com/questions/115559/playing-midi-instruments-in-java
 
Kevin Simonson
Ranch Hand
Posts: 248
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You could try Java | MIDI Introduction or MIDI basics and a lot of people liked About Java's Sound Stuff.

I didn't look at any Youtube tutorials because I'm a text-oriented person and I'm not good at understanding video explanations of text.


I went to that website you pointed me to, the MIDI Introduction, and tried to copy the program {MyMidiPlayer} it had there, with a few modifications. (I like to have my {main} method at the end of the code, for one thing.) My code follows.
<br /> When I tried to run it I got: <br /> <br /> D:\Ct>j8 MyMidiPlayer
Enter the number of notes to be played:
3
Jul 29, 2021 9:08:48 PM java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.
/Undefined external error./
javax.sound.midi.MidiUnavailableException: Undefined external error.
       at com.sun.media.sound.MidiOutDevice.nOpen(Native Method)
       at com.sun.media.sound.MidiOutDevice.implOpen(MidiOutDevice.java:54)
       at com.sun.media.sound.AbstractMidiDevice.doOpen(AbstractMidiDevice.java:154)
       at com.sun.media.sound.AbstractMidiDevice.openInternal(AbstractMidiDevice.java:144)
       at com.sun.media.sound.AbstractMidiDevice.getReceiverReferenceCounting(AbstractMidiDevice.java:350)
       at javax.sound.midi.MidiSystem.getReceiver(MidiSystem.java:273)
       at javax.sound.midi.MidiSystem.getSequencer(MidiSystem.java:481)
       at javax.sound.midi.MidiSystem.getSequencer(MidiSystem.java:392)
       at MyMidiPlayer.setUpPlayer(MyMidiPlayer.java:35)
       at MyMidiPlayer.main(MyMidiPlayer.java:63)

D:\Ct>

Any idea what this is trying to tell me, or what I'm doing wrong?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code compiles and executes OK for me.  I am using Java 8 on Win10.
 
Kevin Simonson
Ranch Hand
Posts: 248
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You could try Java | MIDI Introduction or MIDI basics and a lot of people liked About Java's Sound Stuff.


I took a look at the section titled "MIDI basics" at "http://rememberjava.com/midi/2017/01/13/midi_basics.html", and tried incorporating the method {testPlayNote()} it lists like so:

but when I try to compile it the compiler very understandably complains:

Before this snippet of code the website says, "To play a single C note through the default included 'Gervill' soft synthezier, the following snippet will do." Clearly that's not true. What's the object {getSynthesizer()} needs to be called on?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What's the object {getSynthesizer()} needs to be called on?


To find what class a Java method is in, go to the API doc page, Click on INDEX in the blue bar at the top of the page, select the first letter of the method's name from the list and scroll/search the list for the desired method.
http://docs.oracle.com/javase/8/docs/api/index.html
 
Marshal
Posts: 79462
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be quicker to put the name of the method into the search box at the upper right of the API documentation.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just did a quick search and this came up first…
https://objectcomputing.com/resources/publications/sett/january-2008-writing-music-in-java-two-approaches

I haven’t tried it, but the article may help you find other search terms.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic