Reputation: 1491
I am working on sound processing with the use of Java now. Within my project, I have to deal with the stream. So I have a lot of staffs to do with DataLine
and OutputStream
or InputStream
.
But to me, they are too similar:(
Is there someone who can help me with this question? Thanks in advance! Here are some code I used :
TargetDataLine line;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int frameSizeInBytes = format.getFrameSize();
int bufferLengthInFrames = line.getBufferSize() / 8;
int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
byte[] data = new byte[bufferLengthInBytes];
int numBytesRead;
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format, line.getBufferSize());
} catch (LineUnavailableException ex) {
shutDown("Unable to open the line: " + ex);
return;
} catch (SecurityException ex) {
shutDown(ex.toString());
return;
} catch (Exception ex) {
shutDown(ex.toString());
return;
}
line.start();
while (thread != null) {
if ((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1) {
break;
}
out.write(data, 0, numBytesRead);
}
I have read the documentation of the class TargetDataLine, it is said :"'read(byte[] b, int off, int len)' Reads audio data from the data line's input buffer."
But where do we define it?
Also the line of type TargetDataLine has not been attached to any mixer, so how can we know for which mixer it is for???
Upvotes: 2
Views: 1241
Reputation: 114787
An InputStream
represents a stream of bytes, where we can read bytes one be one (or in blocks) until it is empty. An OutputStream
is the other direction - we write bytes one be one (or in blocks) until we have nothing more to write.
Streams are used to send or receive unstructured byte data.
DataLine
handles audio data, in other words, bytes with a special meaning. And it offers some special methods to control the line (start/stop), get the actual format of the audio data and some other characteristics.
Upvotes: 1
Reputation: 308041
A DataLine
is an interface related to handling sampled sound (a.k.a PCM data) in Java. I don't really know a lot of that.
An OutputStream
is an interface that represents anything that can get bytes written to it. A simple sample of an OutputStream
is a FileOutputStream
: all bytes written to that stream will be written to the file it was opened for.
An InputStream
is the other end: it's an interface that represents anything from which bytes can be read. A simple sample of an InputStream
is a FileInputStream
: it can be used to read the data from a file.
So if you were to read audio data from the hard disk, you'd eventually use a FileInputStream
to read the data. If you manipulate it and later want to write the resulting data back to the hard disk, you'd use a FileOutputStream
to do the actual writing.
Upvotes: 2