1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioFileFormat;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Vector;
import java.net.URL;
import java.lang.Exception;
import java.lang.Integer;
import java.lang.Byte;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.util.Enumeration;
public class Testsound {
static private URL inputURL;
static private AudioFileFormat audioFileFormat;
static private AudioInputStream audioInputStream;
static private AudioFormat audioFormat;
static private Vector samples;
static private DataOutputStream output = null;
public Testsound(String fichier) {
try {
inputURL = new URL("file:" + fichier);
audioFileFormat = AudioSystem.getAudioFileFormat(inputURL);
audioInputStream = AudioSystem.getAudioInputStream(inputURL);
audioFormat = audioInputStream.getFormat();
samples = new Vector();
} catch (Exception e) {
System.out.println("Constructor: " + e.toString());
}
}//End Testsound
public static int unsignedByteToInt(byte b) {
return (int) b & 0xFF;
}//End unsignedByteToInt
public static long byteArrayToInt(byte buf[], int size, boolean bigEndian) {
long i = 0;
int pos = 0;
if (!bigEndian) {
for (int j=size-1;j>=0;j--)
i += (new Byte(buf[pos++])).intValue() << (8 * j);
}
else {
for (int j=0;j<size;j++)
i += (new Byte(buf[pos++])).intValue() << (8 * j);
}
return i;
}//End byteArrayToInt
public int numBytes(AudioInputStream a) {
return (int) (a.getFrameLength() * a.getFormat().getFrameSize());
}//End numBytes
public void computeArray(ByteArrayInputStream a) {
int cnt = 0;
boolean bool = audioFormat.isBigEndian();
int sampleSize = audioFormat.getSampleSizeInBits() / 8;
byte tableau[] = new byte[sampleSize];
if (audioFormat.getChannels() == 1) { //Mono
while (cnt != -1) {
cnt = a.read(tableau, 0, sampleSize);
samples.add(new Integer((int) byteArrayToInt(tableau, sampleSize, bool)));
}
}
if (audioFormat.getChannels() == 2) { //Stereo
while (cnt != -1) {
cnt = a.read(tableau, 0, sampleSize);
samples.add(new Integer((int) byteArrayToInt(tableau, sampleSize, bool)));
if (cnt != -1) cnt = a.read(tableau, 0, sampleSize);
}
}
}//End computeArray
public void readSound() {
int cnt = 0;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte tempBuffer[] = new byte[10000];
try {
//Lecture du fichier audio par 10000 exemplaires a la fois
while( (cnt = audioInputStream.read(tempBuffer, 0, tempBuffer.length)) != -1) byteArrayOutputStream.write(tempBuffer, 0, cnt);
audioInputStream.close();
byte audioData[] = byteArrayOutputStream.toByteArray();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(audioData);
computeArray(byteArrayInputStream);
} catch (Exception e) {
System.out.println("readSound: " + e.toString());
}
}//End readSound
public Vector getSamples() {
return samples;
}//End getSamples
public void writeApplication(DataOutputStream out, String s) {
try {
byte[] array = new byte[s.length()];
array = s.getBytes("UTF-8");
for (int i = 0; i < s.length(); i++) {
out.writeByte(array[i]);
} //end for
} catch (Exception e) {
System.out.println(e);
} //end try
} //end writeApplication
public void getSamplesCSV() {
try {
output = new DataOutputStream(new FileOutputStream(new File("test.csv")));
writeApplication(output, "\"");
for (Enumeration o=samples.elements(); o.hasMoreElements();) writeApplication(output, o.nextElement().toString() + "\",\n\"");
writeApplication(output, "\"");
output.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}//End getSamplesCSV
public static void main(String args[]) {
Testsound t = new Testsound(args[0]);
t.readSound();
t.getSamplesCSV(); //Pour faire un chart, c'est plus rapide... :D
}//End main
}//End class |
Partager