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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
| public class AudioFrame extends javax.swing.JFrame {
static double[] audioFile;
static String audioToDecode;
static String audioKey;
static String outputName;
static String outAudio;
static String name;
static int command;
static String textToEncode;
private PrintStream standardOut;
/**
* Creates new form AudioFrame
*/
public AudioFrame() throws UnsupportedEncodingException, IOException {
initComponents();
PrintStream printStream = new PrintStream(new CustomOutputStream(jTextArea1));
// keeps reference of standard output stream
standardOut = System.out;
}
private void jBtnStartActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Thread thread = new Thread();
try {
trait();
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(AudioFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AudioFrame.class.getName()).log(Level.SEVERE, null, ex);
}
thread.start();
}
private void jBtnstopActionPerformed(java.awt.event.ActionEvent evt) {
try {
// TODO add your handling code here:
trait();
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(AudioFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AudioFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new AudioFrame().setVisible(true);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(AudioFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AudioFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
// Makes a 5 second audio clip of static. 44100 values per second.
public static double[] makeAudio () {
double[] audio = new double[5*44100];
int i;
for (i=0; i < audio.length; i++)
audio[i] = 2*Math.random() - 1; // Generates a random double between -1 and 0.999999
return audio;
}
// Returns the audio file given the name or uses another method to generate a clip of static if the argument is 1
public static double[] getAudioFile(String input) throws UnsupportedEncodingException, IOException {
double[] audioFile;
if (input.equals("1")) {
audioFile = makeAudio(); // Generates a clip of static.
StringBuilder build = new StringBuilder();
char[] buffer = new char[512];
new AudioFrame().jTextArea1.setText("Please enter a name for the generated audio clip : ");
Scanner scan = new Scanner(System.in);
if(scan.hasNext()) {
name = scan.next();
}
if (!name.endsWith(".wav")) // Check if the filename ends with .wav and add the extension if it doesn't
name += ".wav";
StdAudio.save(name, audioFile);
}
else {
if (!input.endsWith(".wav")) // Check if the filename ends with .wav and add the extension if it doesn't
input += ".wav";
audioFile = StdAudio.read(input); // Reads in the audio as an array of doubles
}
return audioFile;
}
private void trait() throws UnsupportedEncodingException, IOException {
Scanner scan = new Scanner(System.in);
int maxChar = 40000;
//ew AudioFrame().jTextArea1.setText
StringBuilder build = new StringBuilder();
char[] buffer = new char[512];
String response = build.toString();
jTextArea1.setText("saisissez votre choix(choisissez un nombre)\n");
jTextArea1.append("1 - Codage du fichier texte vers un fichier audio\n");
jTextArea1.append("2 - Decodage d'un fichier audio(la clé est nécessaire)\n");
if(scan.hasNext()) {
command = scan.nextInt();
}
jTextArea1.append("\n\n");
if (command == 1) {
char [] contents = new char[maxChar]; // Initialize an array of characters to read the text file into
jTextArea1.append("Entrez le nom du fichier texte voulant codé (un fichier texe) :\n ");
if(scan.hasNext()) {
textToEncode = scan.next(); // The text file to read in
}
if (!textToEncode.endsWith(".txt")) // Check if the filename ends with .txt and add the extension if it doesn't
textToEncode += ".txt";
File file = new File(textToEncode);
try {
Scanner s = new Scanner(file,"UTF-8");
s.useDelimiter("\\Z");
if(s.hasNext()) {
contents = s.next().toCharArray();
}
// The \\Z delimiter in combination with .next() will read input until there isn't any left
//System.out.println(contents);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
jTextArea1.append("Entrez le nom du fichier audio que vous voulez codé le texte dedans (fichier .wav)\n");
jTextArea1.append("Si vous voulez générer un clip de static tapez '1' : \n");
if(scan.hasNext()) {
audioFile = getAudioFile(scan.next());
}
///DEBUG
jTextArea1.append("Le Fichier audio avant l'écriture des données");
for (int p = 0; p < 100; p++) {
jTextArea1.append(audioFile[p] + " ");
}jTextArea1.append("\n\n");
//System.out.println();
int audioCounter = 0; // an extra counter for the audio file since its values cap at 1.0 and data would be lost if we tried adding the text to those points. The extra counter allows us to skip over those points.
for (int i = 0; i < contents.length; i++) {
while ((audioFile[i+audioCounter] + (double)(contents[i])/10000.0) >= 1.0) {audioCounter++;} // Increments audioCounter if the encoding would make the value at that point go above 1.0
audioFile[i+audioCounter] += ((double)(contents[i]))/10000.0;
}
// DEBUG
jTextArea1.append("Le Fichier audio après écriture de données");
for (int p = 0; p < 100; p++) {
jTextArea1.append(audioFile[p] + " ");
} jTextArea1.append("\n\n");
jTextArea1.append("Entrer le nom de fichier audio en sortie avec le texte écrit dedans: ");
if(scan.hasNext()) {
outAudio = scan.next();
}
if (!outAudio.endsWith(".wav")) // Check if the filename ends with .wav and add the extension if it doesn't
outAudio += ".wav";
StdAudio.save(outAudio, audioFile);
if(scan.hasNext()) {
scan.close();
}
}
if (command == 2) {
jTextArea1.append("Entrer le nom du fichier audio qu'on veut décoder (fichier .wav): ");
if(scan.hasNext()) {
audioToDecode = scan.next();
}
if (!audioToDecode.endsWith(".wav")) // Check if the filename ends with .wav and add the extension if it doesn't
audioToDecode += ".wav";
jTextArea1.append("Entrer le fichier audio que vous voulez décoder et qui contient la clé de décodage audio (chier wav) : ");
if(scan.hasNext()) {
audioKey = scan.next();
}
//System.out.println();
if (!audioKey.endsWith(".wav")) // Check if the filename ends with .wav and add the extension if it doesn't
audioKey += ".wav";
double[] audioEncoded = StdAudio.read(audioToDecode);
double[] key = StdAudio.read(audioKey);
char [] contents = new char[maxChar];
/* This section was for debugging purposes. It seems that there's some issue when saving or reading the audio files which
* I can't figure out. I checked the values immediately after encoding and they were correct, but after saving and reading
* the file back in they get slightly distorted. It's ok because they can be rounded to end up correct(almost all of the time),
* but rounding is an ugly solution. */
jTextArea1.append("\n Fichier audio avec données codées");
for (int z = 0; z < 100; z++) {
jTextArea1.append(audioEncoded[z] + " " );
} jTextArea1.append("\n\n");
jTextArea1.append("\nFichier audio originale");
for (int z = 0; z < 100; z++) {
jTextArea1.append(key[z] + " " );
}jTextArea1.append("\n\n");
jTextArea1.append("\n(données codés - clé) * 10000");
for (int z = 0; z < 100; z++) {
jTextArea1.append(10000 * ( audioEncoded[z] - key[z] ) + " " );
} jTextArea1.append("\n\n");
int contentCounter = 0; // Increments whenever a character is added to the contents array
for (int i = 0; i < key.length; i++) {
if (!((audioEncoded[i] - key[i] == 0))) { // If there's a value there, then put it in the char array. If not then nothing was encoded and it can be skipped.
contents[contentCounter] = (char) (Math.round((float) (10000 * ( audioEncoded[i] - key[i] )))); // For some reason the values end up being a tiny bit off and need to be rounded to the nearest int before being converted to characters
contentCounter++;
//DEBUGGING
jTextArea1.append(contents[contentCounter-1] + " " + (Math.round((float) (10000 * ( audioEncoded[i] - key[i] )))) + " " + ((10000 * ( audioEncoded[i] - key[i] ))) + "\n" ); //debug line for trying to figure out why the values were off
}
}
jTextArea1.append("Entrer le nom de fichier texte danslequel vous voulez écrire les données: ");
if(scan.hasNext()) {
outputName = scan.next();
}
if (!outputName.endsWith(".txt"))
outputName += ".txt";
// Lots of error catching here, but in short it writes the character array "contents" to the file "outputName"
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(outputName));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
for (int i = 0; i < contentCounter+1; i++) {
writer.write(contents[i]);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
StdAudio.close();
jTextArea1.append("Fin");
}
public void write(int b) throws IOException
{
write (new byte [] {(byte)b}, 0, 1);
}
public void write(byte[] buffer, int offset, int length) throws IOException
{
final String text = new String (buffer, offset, length);
SwingUtilities.invokeLater(new Runnable ()
{
@Override
public void run()
{
jTextArea1.append (text);
}
});
}
} |
Partager