12.6. Using Text to Shift Between MediaAs we said at the beginning of this chapter, we can think about text as the unimedia. We can map from sound to text and back again, and the same with pictures. And more interestingly, we can go from sound to text ... to pictures! Why would we want to do any of this? Why should we care about transforming media in this way? For the same reasons that we care about digitizing media at all. Digital media transformed into text can be more easily transmitted from place to place, checked for errors, and even corrected for errors. It turns out that very often when you are attaching binary files to an e-mail message, your binary file is actually converted to text first! In general, choosing a new representation allows you to do new things. Mapping sound to text is easy. Sound is just a series of samples (numbers). We can easily write these out to a file. Let's add a method to do this to the Sound class. We will need to convert the number to a string. We can use the method valueOf on the String class to do this. This is a class (static) method so it can be invoked using String.valueOf. Program 113. Write a Sound to a File as Text Numbers
To run this we can use the following main method in the Sound class. public static void main(String[] args) { Sound s = new Sound(FileChooser.getMediaPath("her.wav")); s.writeSamplesAsText(FileChooser.getMediaPath("her.txt")); } After we have compiled the Sound class we can run it using: > java Sound How it WorksWe're taking the file name to write to as a parameter. We open the file for writing and then we loop though each sample, converting it to a string and writing the string to a file. We use the newLine method of BufferedReader to add a new line after we write each sample value as a string. What can we do with sound values written as text? We can manipulate it as a series of numbers, such as with Excel (Figure 12.6)! We can open Excel and open the file. We can tell it the delimiters are spaces and it will read the numbers into Column A. Once we have the data in Excel we can do modifications, such as multiplying each sample by 2.0. Figure 12.6. Sound-as-text file read into Excel.![]() We can even graph the numbers, and see the same kind of sound graph as we've seen in MediaTools (Figure 12.7). Select the column of numbers and then click on INSERT and CHART. You can just click on the Finish button to use the defaults. (You might get an error, thoughExcel doesn't like to graph more than 32,000 points, and even at 22,000 samples per second, that's not a lot of samples.) Figure 12.7. Sound-as-text file graphed in Excel. |
![]() /** * Method to create a sound from a text file * @param fileName the name of the file to read from * @return the created sound object */ public static Sound createSoundFromTextFile(String fileName) { String line = null; int value = 0; // create the sound to read into Sound s = new Sound(FileChooser.getMediaPath("sec3silence.wav")); |
The method createSoundFromTextFile takes a filename as input that contains the samples as text. We open up a silent three-second sound to hold the sound. Next we create a BufferedReader to read the file. We loop reading the file until either the end of the file is reached or the index is equal to the length of the sound. In the body of the loop we convert the string to an integer and then set the sample value at the current index. We increment the current index. When we are done we return the created Sound object.
But we don't have to map from sounds to text and back to sounds. We could map a sound to a picture instead! The below program takes a sound and maps each sample to a pixel. All we have to do is to define our mapping, how we want to represent the samples. We chose a very simple one: If the sample is greater than 1,000, the pixel is red; less than -1,000 is blue, everything else is green (Figure 12.8).
We also have to deal with the case where we run out of samples before we run out of pixels. We can add this to the continuation test of a for loop using 'and' (&&).
![]() /** * Method to turn a sound into a picture * @return a created picture */ |
In createPicture(), we open up a 640x480 blank picture. We loop while i is less than the number of pixels in the picture and also less than the samples in the current sound. Each time through the loop we get a sample value at i and figure out a mapping to a color, then set the pixel value to that color. Then we increment i. We return the created Picture object at the end of the method.
Think about how WinAmp does its visualizations, or how Excel or MediaTools graph, or how this program does its visualization. Each is just deciding a different way of mapping from samples to colors and space. It's just a mapping. It's all just bits.
|
A really smart mathematician, Kurt Gödel, used the notion of encodings to come up with one of the most brilliant proofs of the twentieth century. He proved the Incompleteness Theorem, in which he proved that any powerful mathematical system cannot prove all mathematical truths. He figured out a mapping from mathematical statements of truth to numbers. This was long before we had ASCII, which maps numbers to characters. Once the mathematical statements were numbers, he was able to show that there are numbers representing true statements that could not be derived from the mathematical system. In this way, he showed that no system of logic can prove all true statements. By changing his encoding, he gained new capabilities, and thus was able to prove something that no one knew before.