LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-15-2007, 12:15 PM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Rep: Reputation: 83
Java sound: line.write() returns 0


I'm trying to play a sine wave.

I have a SourceDataLine, but when I try to write to it, the write method return 0, meaning that it coulen't write anything to the mixer's output stream, right? I also don't get to hear a thing, by the way.

Here's the code I'm using:
Code:
byte [] sample = new byte[4];
int written = 0;
while (true) {
	int valor = (int) ((2 << 15 -1) * wave.height());
	//System.out.println(valor);
	sample[0] = sample[2] = (byte) (valor & 0xff);
	sample[1] = sample[3] = (byte) (valor >> 8);
	written = line.write(sample, 0, 4);
	if (written != 4) {
		System.err.println("Couldn't write all the bytes!");
		System.exit(1);
	}
	line.drain();
	wave.tic();
}
wave.height() returns the height of the wave (-1 to 1) for that moment in time.

The Audio Format is PCM_SIGNED, at 44.1 KHtz, 16 bits per sample, Stereo, little endian.

Any ideas?

Last edited by eantoranz; 10-15-2007 at 12:16 PM.
 
Old 10-15-2007, 10:13 PM   #2
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Original Poster
Rep: Reputation: 83
I will refrase the problem:

How can I get a SourceDataLine I could use to hear a wave? Im doing this, but I don't get a single line that works:

Code:
AudioFormat format = new AudioFormat(44100f, 16, 2, true, false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

SourceDataLine line = null;
Mixer.Info [] mixerInfo = AudioSystem.getMixerInfo();
Mixer mixer;
for (int i = 0; line == null && i < mixerInfo.length; i++) {
	mixer = AudioSystem.getMixer(mixerInfo[i]);
	try {
		line = (SourceDataLine) mixer.getLine(info);
		if (!line.getLineInfo().matches(info)) {
			line = null;
		}
	} catch (LineUnavailableException e) {
		line = null;
	} catch (IllegalArgumentException e) {
		line = null;
	}
}
		
if (line == null) {
	System.out.println("No line was found");
	System.exit(1);
}
The funny thing is that sometimes mixer.getLine(info) does return a mixer, but then the matches(info) fails. How can I get that done?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
read line by line form text file in java. spank Programming 1 10-18-2006 03:46 PM
How to pass array to a function in Java so that it returns changed? kornerr Programming 1 09-19-2006 01:16 PM
how to write command line program? vito_huang Linux - Software 3 04-14-2006 01:20 AM
How write new MBR using command line only lonecrow Linux - Newbie 2 11-02-2005 11:17 AM
Write long file name in a line shadkong Linux - General 2 04-09-2005 09:52 PM


All times are GMT -5. The time now is 11:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration