Normand Briere
2019-10-06 ce660a4b6ba367bd162dd2cff26d02c80a34c912
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//package GlobalUtilities;
 
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.*;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.sound.sampled.*;
 
/**
 * This class handles the reading, writing, and playing of wav files. It is
 * also capable of converting the file to its raw byte [] form.
 *
 * based on code by Evan Merz modified by Dan Vargo
 * @author dvargo
 */
public class Wav
{
    /*
    WAV File Specification
    FROM http://ccrma.stanford.edu/courses/422/projects/WaveFormat/
    The canonical WAVE format starts with the RIFF header:
    0         4   ChunkID          Contains the letters "RIFF" in ASCII form
    (0x52494646 big-endian form).
    4         4   ChunkSize        36 + SubChunk2Size, or more precisely:
    4 + (8 + SubChunk1Size) + (8 + SubChunk2Size)
    This is the size of the rest of the chunk
    following this number.  This is the size of the
    entire file in bytes minus 8 bytes for the
    two fields not included in this count:
    ChunkID and ChunkSize.
    8         4   Format           Contains the letters "WAVE"
    (0x57415645 big-endian form).
    
    The "WAVE" format consists of two subchunks: "fmt " and "data":
    The "fmt " subchunk describes the sound data's format:
    12        4   Subchunk1ID      Contains the letters "fmt "
    (0x666d7420 big-endian form).
    16        4   Subchunk1Size    16 for PCM.  This is the size of the
    rest of the Subchunk which follows this number.
    20        2   AudioFormat      PCM = 1 (i.e. Linear quantization)
    Values other than 1 indicate some
    form of compression.
    22        2   NumChannels      Mono = 1, Stereo = 2, etc.
    24        4   SampleRate       8000, 44100, etc.
    28        4   ByteRate         == SampleRate * NumChannels * BitsPerSample/8
    32        2   BlockAlign       == NumChannels * BitsPerSample/8
    The number of bytes for one sample including
    all channels. I wonder what happens when
    this number isn't an integer?
    34        2   BitsPerSample    8 bits = 8, 16 bits = 16, etc.
    
    The "data" subchunk contains the size of the data and the actual sound:
    36        4   Subchunk2ID      Contains the letters "data"
    (0x64617461 big-endian form).
    40        4   Subchunk2Size    == NumSamples * NumChannels * BitsPerSample/8
    This is the number of bytes in the data.
    You can also think of this as the size
    of the read of the subchunk following this
    number.
    44        *   Data             The actual sound data.
    
    
    The thing that makes reading wav files tricky is that java has no unsigned types.  This means that the
    binary data can't just be read and cast appropriately.  Also, we have to use larger types
    than are normally necessary.
    
    In many languages including java, an integer is represented by 4 bytes.  The issue here is
    that in most languages, integers can be signed or unsigned, and in wav files the  integers
    are unsigned.  So, to make sure that we can store the proper values, we have to use longs
    to hold integers, and integers to hold shorts.
    
    Then, we have to convert back when we want to save our wav data.
    
    It's complicated, but ultimately, it just results in a few extra functions at the bottom of
    this file.  Once you understand the issue, there is no reason to pay any more attention
    to it.
    
    ALSO:
    
    This code won't read ALL wav files.  This does not use to full specification.  It just uses
    a trimmed down version that most wav files adhere to.
    
     */
 
    ByteArrayOutputStream byteArrayOutputStream;
    AudioFormat audioFormat;
    TargetDataLine targetDataLine;
    AudioInputStream audioInputStream;
    SourceDataLine sourceDataLine;
//    float frequency = 8000.0F;  //8000,11025,16000,22050,44100
//    int samplesize = 16;
    private String myPath;
    private long myChunkSize;
    private long mySubChunk1Size;
    private int myFormat;
    private long myChannels;
    private long mySampleRate;
    private long myByteRate;
    private int myBlockAlign;
    private int myBitsPerSample;
    private long myDataSize;
    
    public byte[][] myData = new byte[4][];
    
    static int minutes = 10;
    
    static byte[] theStream = new byte[0]; // 44100*2 * 60 * minutes];
    
    static int cursor = 0; // +735 per frame = 44100 Hz / 120 Hz * 2 (for 16 bits)
 
//    public Wav()
//    {
//        myPath = "/Users/nbriere/0ut/wavs/Footstepwood";
//        //myPath = "/Users/nbriere/0ut/wavs/robot_walk";
//    }
 
    // constructor takes a wav path
    public Wav(String tmpPath)
    {
        myPath = tmpPath;
        
        read();
    }
 
    // get/set for the Path property
    public String getPath()
    {
        return myPath;
    }
 
    public void setPath(String newPath)
    {
        myPath = newPath;
    }
 
    // read a wav file into this class
    public Wav read()
    {
        DataInputStream inFile = null;
    //    myData = null;
        byte[] tmpLong = new byte[4];
        byte[] tmpInt = new byte[2];
 
        int i = 0;
        
        try
        {
            for (; i<4; i++)
            {
                inFile = new DataInputStream(new FileInputStream(myPath + "" + i + ".wav"));
 
                System.out.println("Reading wav file: " + myPath + "" + i + ".wav"); // for debugging only
 
                String chunkID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();
 
                inFile.read(tmpLong); // read the ChunkSize
                myChunkSize = byteArrayToInt(tmpLong);
 
                String format = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();
 
                // print what we've read so far
                //System.out.println("chunkID:" + chunkID + " chunk1Size:" + myChunkSize + " format:" + format); // for debugging only
 
                String subChunk1ID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();
 
                inFile.read(tmpLong); // read the SubChunk1Size
                mySubChunk1Size = byteArrayToInt(tmpLong);
 
                inFile.read(tmpInt); // read the audio format.  This should be 1 for PCM
                myFormat = byteArrayToShort(tmpInt);
 
                inFile.read(tmpInt); // read the # of channels (1 or 2)
                myChannels = byteArrayToShort(tmpInt);
 
                inFile.read(tmpLong); // read the samplerate
                mySampleRate = byteArrayToInt(tmpLong);
 
                inFile.read(tmpLong); // read the byterate
                myByteRate = byteArrayToInt(tmpLong);
 
                inFile.read(tmpInt); // read the blockalign
                myBlockAlign = byteArrayToShort(tmpInt);
 
                inFile.read(tmpInt); // read the bitspersample
                myBitsPerSample = byteArrayToShort(tmpInt);
 
                // print what we've read so far
                //System.out.println("SubChunk1ID:" + subChunk1ID + " SubChunk1Size:" + mySubChunk1Size + " AudioFormat:" + myFormat + " Channels:" + myChannels + " SampleRate:" + mySampleRate);
 
 
                // read the data chunk header - reading this IS necessary, because not all wav files will have the data chunk here - for now, we're just assuming that the data chunk is here
                String dataChunkID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();
 
                inFile.read(tmpLong); // read the size of the data
                myDataSize = bytebuffer.length; // ??? byteArrayToLong(tmpLong);
 
 
                // read the data chunk
                myData[i] = new byte[(int) myDataSize];
                inFile.read(myData[i]);
 
                // close the input stream
                inFile.close();
            }
        } catch (Exception e)
        {
            if (i == 0)
                e.printStackTrace();
            return this;
        }
 
        return this;
    }
 
    // write out the wav file
    public Wav save()
    {
        if (loop == 0 || !Globals.ANIMATION)
            return this;
        
Object[] options = {"Yes",
                    "No",
                    "Cancel"};
int n = javax.swing.JOptionPane.showOptionDialog(null,
    "Would you like to save the sound?",
    "A Silly Question",
    javax.swing.JOptionPane.YES_NO_CANCEL_OPTION,
    javax.swing.JOptionPane.QUESTION_MESSAGE,
    null,
    options,
    options[2]);
        
if (n == 2)
    return this;
 
        loop = 0;
        
if (n == 1)
    return this;
 
        try
        {
            //for (int i=theStream.length; --i>=0;)
            {
                System.out.println(myPath + ".wav");
                DataOutputStream outFile = new DataOutputStream(new FileOutputStream(myPath + ".wav"));
 
                // write the wav file per the wav file format
                outFile.writeBytes("RIFF");                 // 00 - RIFF
                outFile.write(intToByteArray((int) myChunkSize), 0, 4);     // 04 - how big is the rest of this file?
                outFile.writeBytes("WAVE");                 // 08 - WAVE
                outFile.writeBytes("fmt ");                 // 12 - fmt
                outFile.write(intToByteArray((int) mySubChunk1Size), 0, 4); // 16 - size of this chunk
                shortToByteArray((short) myFormat, buf2);
                outFile.write(buf2, 0, 2);        // 20 - what is the audio format? 1 for PCM = Pulse Code Modulation
                shortToByteArray((short) myChannels, buf2);
                outFile.write(buf2, 0, 2);  // 22 - mono or stereo? 1 or 2?  (or 5 or ???)
                outFile.write(intToByteArray((int) mySampleRate), 0, 4);        // 24 - samples per second (numbers per second)
                outFile.write(intToByteArray((int) myByteRate), 0, 4);      // 28 - bytes per second
                shortToByteArray((short) myBlockAlign, buf2);
                outFile.write(buf2, 0, 2);    // 32 - # of bytes in one sample, for all channels
                shortToByteArray((short) myBitsPerSample, buf2);
                outFile.write(buf2, 0, 2); // 34 - how many bits in a sample(number)?  usually 16 or 24
                outFile.writeBytes("data");                 // 36 - data
 
    //            outFile.write(intToByteArray((int) myDataSize), 0, 4);      // 40 - how big is this data chunk
    //            outFile.write(myData);                      // 44 - the actual data itself - just a long string of numbers
 
                outFile.write(intToByteArray((int) theStream.length), 0, 4); // up to cursor only
                outFile.write(theStream);
            }
        } catch (Exception e)
        {
            System.out.println(e.getMessage());
            return this;
        }
 
        System.out.println("done.");
   //     cursor = 0;
        
        return this;
    }
 
    // return a printable summary of the wav file
    public String getSummary()
    {
        //String newline = System.getProperty("line.separator");
        String newline = "\n";
        String summary = "Format: " + myFormat + newline + "Channels: " + myChannels + newline + "SampleRate: " + mySampleRate + newline + "ByteRate: " + myByteRate + newline + "BlockAlign: " + myBlockAlign + newline + "BitsPerSample: " + myBitsPerSample + newline + "DataSize: " + myDataSize + "";
        return summary;
    }
 
//    public byte[] getBytes()
//    {
//        read();
//        return myData;
//    }
 
    /**
     * Plays back audio stored in the byte array using an audio format given by
     * freq, sample rate, ect.
     * @param data The byte array to play
     */
    public void playAudio(byte[] data)
    {
        try
        {
            byte audioData[] = data;
            //Get an input stream on the byte array containing the data
            InputStream byteArrayInputStream = new ByteArrayInputStream(audioData);
            AudioFormat audioFormat = getAudioFormat();
            audioInputStream = new AudioInputStream(byteArrayInputStream, audioFormat, audioData.length / audioFormat.getFrameSize());
            DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
            sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
            sourceDataLine.open(audioFormat);
            sourceDataLine.start();
 
            //Create a thread to play back the data and start it running.  It will run \
            //until all the data has been played back.
            Thread playThread = new Thread(new PlayThread());
            playThread.start();
        } catch (Exception e)
        {
            System.out.println(e);
        }
    }
 
    boolean playing;
    
    static byte[] bytebuffer = new byte[100000];
    
    static byte[] buf2 = new byte[2];
    
    static int loop = 0;
    
    public void play(double volume) //, int wave)
    {
        byte[] mydata = myData[(loop++) % myData.length]; // (int)(Math.random()*4)];
        
        //loop %= myData.length;
        
        byte[] thestream = theStream; // [wave-1];
        
        int mycursor = cursor/2*2;
        
        if (mydata == null) // june 2014
            return;
        
        for (int i=mydata.length/2; --i>=0;)
        {
//            bytebuffer[i] = mydata[i];        //    bytebuffer[i] *= volume;
            buf2[0] = mydata[2*i];
            buf2[1] = mydata[2*i+1];
            int val = byteArrayToShort(buf2); // & 0xFFFF;
            val *= volume;
            
            buf2[0] = thestream[mycursor+2*i];
            buf2[1] = thestream[mycursor+2*i+1];
            int prevval = byteArrayToShort(buf2); // & 0xFFFF;
            
            if (prevval != 0)
                val += prevval;
            
            if (val > 32767)
                val = 32767;
            if (val < -32768)
                val = -32768;
            
            shortToByteArray((short)val, buf2);
            bytebuffer[2*i] = buf2[0];
            bytebuffer[2*i + 1] = buf2[1];
        }
        
        System.arraycopy(bytebuffer, 0, thestream, mycursor, mydata.length);
        
        if (playing)
            return;
        
        playing = true;
                
        playAudio(bytebuffer); // myData);
    }
    
    /**
     * This method creates and returns an AudioFormat object for a given set
     * of format parameters.  If these parameters don't work well for
     * you, try some of the other allowable parameter values, which
     * are shown in comments following the declarations.
     * @return
     */
    private AudioFormat getAudioFormat()
    {
        float sampleRate = mySampleRate; // frequency;
        
        //8000,11025,16000,22050,44100
        int sampleSizeInBits = myBitsPerSample; // samplesize;
        //8,16
        int channels = 1;
        //1,2
        boolean signed = true;
        //true,false
        boolean bigEndian = false;
        //true,false
        //return new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, 8000.0f, 8, 1, 1,
        //8000.0f, false );
 
        return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
    }
 
    public void playWav(String filePath)
    {
        try
        {
            AudioClip clip = (AudioClip) Applet.newAudioClip(new File(filePath).toURI().toURL());
            clip.play();
        } catch (Exception e)
        {
            Logger.getLogger(Wav.class.getName()).log(Level.SEVERE, null, e);
        }
 
    }
 
// ===========================
// CONVERT BYTES TO JAVA TYPES
// ===========================
    // these two routines convert a byte array to a unsigned short
    public static short byteArrayToShort(byte[] b)
    {
//        int start = 0;
//        int low = b[start] & 0xff;
//        int high = b[start + 1] & 0xff;
//        return (int) (high << 8) | low;
        //
//        return (int) (high > 8) & 0x000000FF);
//        b[2] = (byte) ((i >> 16) & 0x000000FF);
//        b[3] = (byte) ((i >> 24) & 0x000000FF);
//        return b;
        short ret = 0;
        for (int i = 2; --i >= 0;)
        {
            ret <<= 8;
            ret |= b[i] & 0xFF;
        }
 
        return ret;
    }
 
    public static int byteArrayToInt(byte[] b)
    {
        int ret = 0;
        for (int i = 4; --i >= 0;)
        {
            ret <<= 8;
            ret |= b[i] & 0xFF;
        }
 
        return ret;
    }
 
    // convert a short to a byte array
    public static void shortToByteArray(short data, byte[] buf2)
    {
//        return new byte[]
//                {
//                    (byte) (data & 0xff), (byte) ((data >>> 8) & 0xff)
//                };
        buf2[0] = (byte) (data & 0xff);
        buf2[1] = (byte) ((data >>> 8) & 0xff);
    }
 
    // convert a short to a byte array
    public static byte[] intToByteArray(int data)
    {
        return new byte[]
                {
                    (byte) (data & 0xff), (byte) ((data >>> 8) & 0xff), (byte) ((data >>> 16) & 0xff), (byte) ((data >>> 24) & 0xff)
                };
    }
 
    /**
     * Inner class to play back the data that was saved
     */
    class PlayThread extends Thread
    {
 
        byte tempBuffer[] = new byte[10000];
 
        public void run()
        {
            try
            {
                int cnt;
                //Keep looping until the input
                // read method returns -1 for
                // empty stream.
                while ((cnt = audioInputStream.read(tempBuffer, 0, tempBuffer.length)) != -1)
                {
                    if (cnt > 0)
                    {
                        //Write data to the internal
                        // buffer of the data line
                        // where it will be delivered
                        // to the speaker.
                        sourceDataLine.write(tempBuffer, 0, cnt);
                    }
                }
                //Block and wait for internal
                // buffer of the data line to
                // empty.
                sourceDataLine.drain();
                sourceDataLine.close();
            } catch (Exception e)
            {
                System.out.println(e);
                System.exit(0);
            }
            
            playing = false;
        }
    }
}