└── EncodeAudio.pde /EncodeAudio.pde: -------------------------------------------------------------------------------- 1 | import java.awt.datatransfer.*; 2 | import java.awt.Toolkit; 3 | import javax.swing.JOptionPane; 4 | import ddf.minim.*; 5 | 6 | int SAMPLES = 30000; 7 | 8 | Minim minim; 9 | AudioSample sample; 10 | 11 | void setup() 12 | { 13 | size(512, 200); 14 | 15 | String file = selectInput("Select audio file to encode."); 16 | 17 | if (file == null) { 18 | exit(); 19 | return; 20 | } 21 | 22 | try { 23 | Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 24 | 25 | minim = new Minim(this); 26 | sample = minim.loadSample(file); 27 | 28 | float[] samples = sample.getChannel(BufferedAudio.LEFT); 29 | float maxval = 0; 30 | 31 | for (int i = 0; i < samples.length; i++) { 32 | if (abs(samples[i]) > maxval) maxval = samples[i]; 33 | } 34 | 35 | int start; 36 | 37 | for (start = 0; start < samples.length; start++) { 38 | if (abs(samples[start]) / maxval > 0.01) break; 39 | } 40 | 41 | String result = ""; 42 | for (int i = start; i < samples.length && i - start < SAMPLES; i++) { 43 | result += constrain(int(map(samples[i], -maxval, maxval, 0, 256)), 0, 255) + ", "; 44 | } 45 | 46 | clipboard.setContents(new StringSelection(result), null); 47 | 48 | JOptionPane.showMessageDialog(null, "Audio data copied to the clipboard.", "Success!", JOptionPane.INFORMATION_MESSAGE); 49 | } catch (Exception e) { 50 | JOptionPane.showMessageDialog(null, "Maybe you didn't pick a valid audio file?\n" + e, "Error!", JOptionPane.ERROR_MESSAGE); 51 | } 52 | 53 | exit(); 54 | } 55 | 56 | void stop() 57 | { 58 | sample.close(); 59 | minim.stop(); 60 | super.stop(); 61 | } 62 | --------------------------------------------------------------------------------