I understand, how to convert AudioInputStream from one format to another. Now, when I have stream converted, how to write it to matching SourceDataLine I have?
Just loop over read() and write() methods? Or there are better premade methods?
Yes. Usually done in a while loop with a boolean to allow a means of stopping via an "external" thread.
There is a good example in the Java Tutorials, under the heading "Using a SourceDataLine".
http://docs.oracle.com/javase/tutorial/sound/playing.html
There are extra steps for opening and closing the lines, but the tutorial covers this as well.
Related
It seems to me that InputStream and OutputStream are ambiguous names for I/O.
InputStream can be thought of as "to input into a stream", and OutputStream can be thought of as "get output of a stream".
After all, we read from an "input" stream, but shouldn't you be reading from an "output"?
What was the rationale behind choosing these two names and what is a good way to remember Input/Output stream without confusing one for the other?
The streams are named not for how you use them inside your code but for what they accomplish. An InputStream accomplishes reading input from somewhere outside your program (the console, a file, etc.), whereas an OutputStream accomplishes writing an output to somewhere else (again, console, file, etc.). Your Java code is only the intermediary in this scenario: In order to make use of the input, you have to read it from the stream, and in order to produce an output, you first have to write something to the stream.
The problem with the naming is only that streams by design always have something that goes in and something that comes out - you can always read and write on/with any stream. All you have to remember is that they are named for the more important task they do: interacting with something outside your code.
Think of your program/code as the Actor.
When the Actor wants to read something in, it seeks an handle to
InputStream cause its this stream that will provide the Input. And hence when you Read from it.
When the Actor wants to write something out, it seeks an handle
to OutputStream and then start writing to the handle which will do
the rest. Likewise you Write to it.
I hope this answers. I just visualize my code as the classic Stick Diagram Actor and InputStream and OutputStream as the entities with which you interact.
Looking at the documentation of Font#loadFont I came across this remark:
This method does not close the input stream.
Unfortunately, this is not explained or expanded upon. So my question is:
What are possible reasons the API won't close the input stream? Is it likely you would like to re-use the stream?
I mostly use this method like this:
Font.loadFont(getClass().getResourceAsStream("path/to/font"), 13.0);
to make sure the font is available for my application, so I never re-use the input stream, and I can't really think of a reason I'd want to.
Should I close the input stream myself? Should I expect any problems if I'm not closing the input stream?
In the past I've had problems with a font loaded this way, where some labels configured with this font started showing squares, while others (on the same scene!) kept working fine. Could this be related to not closing the input stream?
The documentation for every API involving scarce or external resources (such as file descriptors or streams) will make it clear whose responsibility it is to clean up (in this case, close the stream). This is sometimes referred to as "ownership".
In this case the documentation states that the loadFont method does not take ownership of the stream. Therefore it still belongs to you: It is your responsibility to close the stream.
The try-with-resources statement is the best way to do this.
This is my understanding regarding reading a file using BufferedReader in java. Please correct me if I am wrong somewhere...
Recently I had a requirement where we are required to read a file multiple times.
The usual way which I use is setting a mark() and doing a reset. But the input parameters to
a mark is an integer and it cannot accept a long number. Is there a way in which we can read the file, a large number of times.
In c++ we can do a seekg on the fstream and read the contents once again irrespective of the number of times we want to do so. Is there anything in java which is of this nature.
Just close the file and read it again.
But review your requirement. Why can't you process it in one pass?
Not much of a good answer but if you want to do random reading and writing then you can use Channels in java.nio package.
BufferedReader is for reading a file when you logically see it as a series of records and records are generally accessed sequentially.
Channels allow you to view your file as a series of blocks. Blocks are meant to be read randomly. :)
Using subclass of channel, FileChannel, you can read what you want from wherever you want. You need to specify two things:
Where to read from.
How much to read.
It has a read(dst,pstn) where dst is a ByteBuffer and pstn is a long position.
Don't worry that it is abstract because you use it via Files.newByteChannel() which does all the voodoo needed to make it work :)
I am still a newbie java programmer. I was learning about Java IO and noticed that in the book as well as in the online tutorials they donot talk about scanner class. They always mention, creating input/output stream reader objects and use them to read or write.
I am very familiar with scanner class and after reading I started to think may be scanner is not the right way to read console input/files in java.
Please clarify my doubt and if you could point me to an easy to understand tutorial, it will be great. I have already looked up oracle docs and other popular websites. Read Herbert schildt's book & the awful head first java book (barf..barf)
You to understand that a) a lot of material about Java was written years ago and Scanner is relatively recent. b) Scanner the right tool in some situations but you can use raw stream for binary or readers for text in all situations.
As you suspect Scanner is the right choice for simple text documents.
You have evaluate the material you are reading and give it context (like how old is it) There isn't any tutorial which will help you with that. ;)
The scanner class is a special file reader which is optimized for reading text files. If you want to read other file types the scanner class is not optimal.
A good overview can be found here java i/o. The summary form there:
The java.io package contains many classes that your programs can use to read and write data. Most of the classes implement sequential access streams. The sequential access streams can be divided into two groups: those that read and write bytes and those that read and write Unicode characters. Each sequential access stream has a speciality, such as reading from or writing to a file, filtering data as its read or written, or serializing an object.
After reading this, you should look into Apache Commons I/O which give you some handy utility classes for i/o.
Java io package supports byte level and character level operations. Both can be done in streamed or buffered fashion done. Examples about these IO types can be found here.
A Scanner object is useful for breaking down formatted input into tokens and translating individual tokens according to their data type.
Example about the Scanner
I have to implement the LPC (linear predictive coding) Algorithm with Java and quiet frankly don't have a clue where to start. Could someone please point me into a right direction.. I can't. of course, use already implemented algorithms from the java sound api (if its provides a solution).
Java comes with an AudioInputStream.
You can get the inputStream by calling avax.sound.sampled.AudioSystem.getAudioInputStream(File f).
The AudioInputStream has a read() method which reads the data. Probably, you will want to read all the data, do something with it and store it back to a file...