Buffers, byte order, streaming... Forget that complication. Here's the general solution for making your speakers and microphones do anything you want. With only basic Java programming skills (a first year college class) you can do what took me years to learn: Define sound as numbers from -1 to 1 at each instant in time.
Most people think computer programming is boring and tedious. That's true for a lot of kinds of programs, but building programs to change your voice or create new kinds of musical instruments, and then using them, can be more fun than music-based video games. Its an upgrade from a set path (like the notes you have to play in Guitar Hero) to complete freedom of what you want the speakers and microphone (or electric guitar in the microphone hole) to do. Computers do billions of calculations per second. Your code will run 44100 times per second for normal 44.1 khz audio (same data speed as CDs), so each vibration of the audio you can do many thousands of calculations if you want. That's your new tools for defining interactions between speakers and microphones. I've made it easy enough anyone can learn it.
http://sourceforge.net/projects/jsoundcard
Here's an example of how to use it (names of things may change after version 0.4):
In the same folder as jsoundcard.jar, create a text file called X.java:
import jsoundcard.*;
public class X implements SoundFunc{
public static void main(String args[]) throws Exception{
JSoundCard.play(new X(), 2, 1, 44100); //2 speakers, 1 microphone, 44.1 khz
}
public void readWriteFrame(double frame[]){
frame[0] = .5*Math.sin(50*frame[2])-frame[1]*.001;
frame[1] = .4*Math.cos(40*frame[2]);
//write any code you want as long as it runs fast and keeps numbers in range -1 to 1
}
public int frameSize(){ return 3; } //If you want to be able to use up to frame[34], return 35
}
What does that code do? It makes a strange echo (if the microphone can hear the speakers) and makes your voice sound scratchy when you talk louder. To play the microphone as it is (on the left speaker), simply use frame[0] = frame[2]; Because there are 2 speakers, frame[0] and frame[1] are where you put the numbers to create those 2 sounds. Those numbers have to be between -1 and 1. After that is a third number for getting the microphone as frame[2]. Math.sin is the sine function in math. Multiplying the left speaker by .5 makes it a little louder than the right speaker which is multiplied by .4. Multiplying the microphone amplitude by 50 makes it higher frequency than multiplying it by 40, but in a kind of screwed up way that makes it vibrate multiple times and then reverse direction in the sine/cosine circle. That's just an example. You can calculate the numbers however you want to create different sound effects. I like to do it by plugging an electric guitar into the microphone hole.
Install Java Development Kit (JDK) 1.5 or higher and set your PATH to include its folder with javac.exe in it (or use the whole path to javac.exe).
Type this on the command-line to compile it: javac -cp .;jsoundcard.jar X.java
(On Linux use colon instead of semicolon)
Type this on the command-line to run it: java -cp .;jsoundcard.jar X
Then you hear the sound you just programmed, an interaction between microphone and speakers.
To make there be less delay between microphone and speakers, in Windows, you can crtl+alt+delete (each time you run the program) and Set Priority of java.exe or javaw.exe to High or Realtime. JSoundCard will detect the increased available speed and use it automatically. It won't use extra cpu. It will just update the sound buffers more often then go back to sleep.
You can build programs that are 1 file you double-click to run, but that takes more setup in building it (not using it). When programming, what I wrote above is the easiest way to start. I'm building an easier way to use it, but that will be a separate program. This is if you want to keep the code small and simple.
If you want your new program to be in 1 file that works instantly when anyone double-clicks it, rename jsoundcard.jar to jsoundcard.zip and unzip it. Then change the META-INF/MANIFEST.MF file so it says the Main-Class is X instead of jsoundcard.TestJSoundCard. Then zip all those files, including X.java and X.class (which javac created), into a new zip file. Then rename that zip file to YourProgram.jar. Then double-click YourProgram.jar and it plays your sound effects (interaction between speakers and microphone). That's how anyone with very little training can create their own sound programs. Give YourProgram.jar to your friends, and have them repeat the same steps with YourProgram.jar as you did for jsoundcard.jar, to build their own programs. Or just double-click it to use your new program.
There is still the problem of how to stop the sound effects (close the program) after double-clicking your new file. You could put at the end of that "main" function this code to make it end after 1 minute: Thread.sleep(60*1000); JSoundCard.stop();
If you follow these instructions, you've done what took me years to learn. Of course I didn't have anyone giving me the solution. Now lets build that "Multimedia Playground".
Its a very small software you use to build new audio software. All it does is give you easy access to the sound-card. Future versions will not contain specific sound-effects or other complexity. The point is to be as simple as possible and put that complexity in other programs that use this program. That way theres billions of people who could do audio programming instead of only professionals. It could catch on, if those who try it tell 1 or 2 friends each, who do the same... This really is the simplest it has ever been. I started audio programming 10 years ago (in some of my free time). I build different audio softwares and finally forged the common parts of them into the most simple thing it could be. I've done some things in audio programming that nobody else would have known how to do without reading my code (other programs). Of course I do it by "standing on the shoulders of giants", but to make sure I can stand higher years from now I'm reducing the learning curve for audio programming so new giants can grow for me to stand on. I'm making it simple enough that anyone who wants to learn audio programming can learn it. Hopefully someone who uses this will eventually build some open-source software we all can use later, and we can finally build the "Multimedia Playground" which is some combination of interactive audio and video.
Most people think computer programming is boring and tedious. That's true for a lot of kinds of programs, but building programs to change your voice or create new kinds of musical instruments, and then using them, can be more fun than music-based video games. Its an upgrade from a set path (like the notes you have to play in Guitar Hero) to complete freedom of what you want the speakers and microphone (or electric guitar in the microphone hole) to do. Computers do billions of calculations per second. Your code will run 44100 times per second for normal 44.1 khz audio (same data speed as CDs), so each vibration of the audio you can do many thousands of calculations if you want. That's your new tools for defining interactions between speakers and microphones. I've made it easy enough anyone can learn it.
http://sourceforge.net/projects/jsoundcard
Here's an example of how to use it (names of things may change after version 0.4):
In the same folder as jsoundcard.jar, create a text file called X.java:
import jsoundcard.*;
public class X implements SoundFunc{
public static void main(String args[]) throws Exception{
JSoundCard.play(new X(), 2, 1, 44100); //2 speakers, 1 microphone, 44.1 khz
}
public void readWriteFrame(double frame[]){
frame[0] = .5*Math.sin(50*frame[2])-frame[1]*.001;
frame[1] = .4*Math.cos(40*frame[2]);
//write any code you want as long as it runs fast and keeps numbers in range -1 to 1
}
public int frameSize(){ return 3; } //If you want to be able to use up to frame[34], return 35
}
What does that code do? It makes a strange echo (if the microphone can hear the speakers) and makes your voice sound scratchy when you talk louder. To play the microphone as it is (on the left speaker), simply use frame[0] = frame[2]; Because there are 2 speakers, frame[0] and frame[1] are where you put the numbers to create those 2 sounds. Those numbers have to be between -1 and 1. After that is a third number for getting the microphone as frame[2]. Math.sin is the sine function in math. Multiplying the left speaker by .5 makes it a little louder than the right speaker which is multiplied by .4. Multiplying the microphone amplitude by 50 makes it higher frequency than multiplying it by 40, but in a kind of screwed up way that makes it vibrate multiple times and then reverse direction in the sine/cosine circle. That's just an example. You can calculate the numbers however you want to create different sound effects. I like to do it by plugging an electric guitar into the microphone hole.
Install Java Development Kit (JDK) 1.5 or higher and set your PATH to include its folder with javac.exe in it (or use the whole path to javac.exe).
Type this on the command-line to compile it: javac -cp .;jsoundcard.jar X.java
(On Linux use colon instead of semicolon)
Type this on the command-line to run it: java -cp .;jsoundcard.jar X
Then you hear the sound you just programmed, an interaction between microphone and speakers.
To make there be less delay between microphone and speakers, in Windows, you can crtl+alt+delete (each time you run the program) and Set Priority of java.exe or javaw.exe to High or Realtime. JSoundCard will detect the increased available speed and use it automatically. It won't use extra cpu. It will just update the sound buffers more often then go back to sleep.
You can build programs that are 1 file you double-click to run, but that takes more setup in building it (not using it). When programming, what I wrote above is the easiest way to start. I'm building an easier way to use it, but that will be a separate program. This is if you want to keep the code small and simple.
If you want your new program to be in 1 file that works instantly when anyone double-clicks it, rename jsoundcard.jar to jsoundcard.zip and unzip it. Then change the META-INF/MANIFEST.MF file so it says the Main-Class is X instead of jsoundcard.TestJSoundCard. Then zip all those files, including X.java and X.class (which javac created), into a new zip file. Then rename that zip file to YourProgram.jar. Then double-click YourProgram.jar and it plays your sound effects (interaction between speakers and microphone). That's how anyone with very little training can create their own sound programs. Give YourProgram.jar to your friends, and have them repeat the same steps with YourProgram.jar as you did for jsoundcard.jar, to build their own programs. Or just double-click it to use your new program.
There is still the problem of how to stop the sound effects (close the program) after double-clicking your new file. You could put at the end of that "main" function this code to make it end after 1 minute: Thread.sleep(60*1000); JSoundCard.stop();
If you follow these instructions, you've done what took me years to learn. Of course I didn't have anyone giving me the solution. Now lets build that "Multimedia Playground".
Its a very small software you use to build new audio software. All it does is give you easy access to the sound-card. Future versions will not contain specific sound-effects or other complexity. The point is to be as simple as possible and put that complexity in other programs that use this program. That way theres billions of people who could do audio programming instead of only professionals. It could catch on, if those who try it tell 1 or 2 friends each, who do the same... This really is the simplest it has ever been. I started audio programming 10 years ago (in some of my free time). I build different audio softwares and finally forged the common parts of them into the most simple thing it could be. I've done some things in audio programming that nobody else would have known how to do without reading my code (other programs). Of course I do it by "standing on the shoulders of giants", but to make sure I can stand higher years from now I'm reducing the learning curve for audio programming so new giants can grow for me to stand on. I'm making it simple enough that anyone who wants to learn audio programming can learn it. Hopefully someone who uses this will eventually build some open-source software we all can use later, and we can finally build the "Multimedia Playground" which is some combination of interactive audio and video.
Tue, Jan 11, 2011 Permanent link
Categories: audio, sound, programming, math, simple, java, speaker, microphone
Sent to project: Proposal for a multimedia playground
Categories: audio, sound, programming, math, simple, java, speaker, microphone
Sent to project: Proposal for a multimedia playground
![]() |
RSS for this post |