|
-
September 26th, 2005, 01:43 PM
#1
Questions about sound in Java
I have been playing around with some things, and wanted to ask you experts out there if there is a way to play sounds in Java that are, for lack of a better term, user interactive...
For example, using just BASIC, I can write a program that will create a beep, and I can add stuff to that program to change the tone of that beep as well as the lenght of that beep...
The idea is that a user can be asked for a lenght (in ms or s) and a tone frequency, and the program will sound out the tone for the correct lenght of time.
I looked into the Toolkit beep, but that is just a system beep and I cant see anyway to vary the tone or lenght of the beep.
Ultimately, I want to build an applet that will allow a user to enter a text string and then output that string as audible morse code, with user selected speed and tone. That way the user can simulate a morse code message in 5wpm through 40wpm speeds in various tone frequencies to make it easier to hear each letter being signaled out.
So I was looking at this two ways... the first way which is more dynamic and more preferable in any case looks something like this in bad pseudocode:
Code:
Open User Input dialog
User enters text string
User selects code speed in Words Per Minute
User selsects tone frequency to change the pitch of the sounds
Parse input string and remove spaces and illegal characters
read each char in the new string
find the morse equivalent for the char
play a beep for that char that lasts the correct lenght at the correct frequency
move to next char and repeat until the end of the string
reopen user dialog with option to exit program
In that case all the sound generation is done by the computer. It simply creates a beep that is of dotBeepLen or dashBeepLen length in milliseconds, and of userFreq frequency, so a lower frequency would yield a lower note and a higher freq would create a higher note.
The next idea would be (borrowing from similar apps available on the web) probably simpler, but involve far more files:
Code:
Open User Dialog
Get text string
Get character speed
parse string
strip out bad chars
find first char in string
find matching sound file
play sound file at appropriate speed
get next char
repeat
Roughly, thats how I would like it to go. The upside of the first one is that it uses only the code I write. But I have no earthly idea where to even begin wiht the sound creation.
The upside of the second one is no sound creation, but the downside is that I need at least 40 premade .au files in addition to the source code.
So, Any ideas, or suggestions, or comments would be appreciated. I have found lots on playing sounds that come in .wav or .mp3 or .au or whatever, and plenty on making a simple system error beep using java.awt.Toolkit, but nothing so far that really helps me figure this out...
Like I said, in something as simple as BASIC I could create the sounds easily (and have done so quite a few times on little programs) but I have no clue how to do this in Java, if it is even possible...
Thanks all!
Jeff
-
September 26th, 2005, 02:15 PM
#2
Re: Questions about sound in Java
Check out the javax.sound.sampled package. It's not as basic at the beep(freq, duration)...
- petter
-
September 26th, 2005, 03:40 PM
#3
Re: Questions about sound in Java
UPDATE: after posting this reply, I did some more digging around and found some stuff at jsresources.org that has info on catting sound files together...
Found another site that also has a demo that generates sound mathematically, allowing for variable length and frequency tones...
wonder if I am just making too much work for myself, or over-complicating things...
END UPDATE
Thaks for pointing me to javax.sound.sampled. I found some interesting stuff there (and since I know nothing of creating audio players beyond some very basics) it helps a lot. That does lead me to other issues though.
For instance, what I want to do with this as a tool for me to learn more java, is writing a morse code converter... so, using teh sampled package, as I have read it so far, the process would look something like this:
Code:
User inputs string "FOO"
change foo to lower case
parse the string
first char is "f"
find file f.au
open AudioInputStream for f.au
read in f.au
write f.au to audio output
close AudioInputStream for f.au
get next char ("o")
find file o.au
open AudioInputStream for o.au
read in o.au
write o.au to audio output
close AudioInputStream for o.au
get next char ("o")
find file o.au
open AudioInputStream for o.au
read in o.au
write o.au to output
close AudioInputStream for o.au
End
So I guess that the two main issues I have with that method are that
A: It reqires me to have a pre-recorded sound file for each and every character in the Morse Code world [0-9][A-Z][punctuation marks and such]
B: requires opening and closing the input stream many many times. A string of 100 chars would require opening and closing the input stream and output stream 100 times in a row, to play sound files that will last a very short time (at 5wpm, an "o" would last approximately 10 seconds and at 40 wpm, the "o" would be only about a second or so...)
That is the biggest reason I was looking for a way to actually generate the notes in java without having to rely on an external sound file or midi file...
Hrmm, but using sampled does point me in one direction at least. There are quite a few java applets out there that do exactly what I want to do, but I dont want to even look at their source code until I have exhausted all other means of figuring this out... I want this to be my own, not just a rewrite of someone elses code...
Cheers
Jeff
Last edited by bladernr; September 26th, 2005 at 04:25 PM.
Reason: Found more info
-
September 26th, 2005, 04:28 PM
#4
Re: Questions about sound in Java
If you dig deeper into the sampled packace, you'll discover that you can generate the audio streams yourself (I'm sure Sun has some examples of generating/playing sine waves on the fly). And that won't require any external files...
But, if you use external files, you don't need 40 different files. I thought Morse was a combination of "short" and "long" tones... so basically you just need two files. A "short.au" and a "long.au", and then combine those two to make the different combinations.
And, if you don't wan't to read those files over and over again, I'm also sure that you could load the files into a memory stream, and then use the memory streams instead...
- petter
-
September 26th, 2005, 04:34 PM
#5
Re: Questions about sound in Java
Cool... I am at work doing all this right now, so I dont have the time to dig too deeply into it... but when I get home I will check that out as well...
The multiple files thing was one of the big concerns I had (opening too many streams). I did find some samples that showed concating files into one stream. The problem with the dot and dash files only is that for 100 chars, there could be up to 500 seperate reads (Morse has up to 5 dots or dashes in a single character)...
I did also think about perhaps doing something in ram... I found another sample that basically creates the sound in memory and then outputs it... so thats another thing to look at.
Well, its definitely food for thought... this is gonna take a good bit of work, I think... but it will definitely be a learning experience...
Cheers
Jeff
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|