aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/cycling74/msp/AudioFileBuffer.java
blob: b2e76f3bc18df807b5a625bc93215a5658f319f7 (plain)
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
package com.cycling74.msp;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;

import com.cycling74.max.MessageReceiver;

/**
 * Work in progress, target: 0.8.4
 */
class AudioFileBuffer {
    
    /** file buffer value */
    public float buf[][];
    
    /** Value of the message that will be sended to MessageReceiver when the file will be loaded */
    public static final int FINISHED_READING = 1;
   
    private MessageReceiver callback = null;
    private String file;
    private AudioFormat audioFormat;
    private AudioFormat targetAudioFormat;
    
    // getters values
    private float sampleRate;
    private int sampleBitFormat;
    private boolean sampleBigEndian;
    private int sampleChannels;
    private long sampleFrames;
    
    public AudioFileBuffer(String filename) throws FileNotFoundException, IOException, UnsupportedAudioFileException {
        open(filename);
    }
    
    public AudioFileBuffer(String filename, MessageReceiver callback) throws FileNotFoundException, IOException, UnsupportedAudioFileException {
        this.callback = callback;
        open(filename);
    }
    
    public void open(String filename) throws FileNotFoundException, IOException, UnsupportedAudioFileException {
        // file format 
        sampleRate = 0;
        sampleBitFormat = 0;
        sampleChannels = 0;
        sampleFrames = 0;
        
        AudioInputStream sourceAudioInputStream = AudioSystem.getAudioInputStream( new FileInputStream(file) );
        AudioFormat sourceAudioFormat = sourceAudioInputStream.getFormat();
        targetAudioFormat = new AudioFormat( 
                AudioFormat.Encoding.PCM_UNSIGNED,      // Encoding 
                getSystemSampleRate(),                  // Sample rate
                16,                                     // bit
                sourceAudioFormat.getChannels(),        // channel
                sourceAudioFormat.getFrameSize(),       // frame size
                sourceAudioFormat.getFrameRate(),       // frame rate 
                false );                                // big Endian
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFormat, sourceAudioInputStream );
        
        FileLoader thread = new FileLoader(audioInputStream);
        new Thread(thread).start();
    }
    
    class FileLoader implements Runnable {
        AudioInputStream ais;
        
        FileLoader(AudioInputStream input) {
            ais = input;
        }
        
        public void run() {
            try {
                buf = new float[audioFormat.getChannels()][];
                
                for (int i=0;ais.available() != 0;i++) {
                    //doc.content[i] = audioInputStream.read() / 65535;
                }       
                
                if ( callback != null ) 
                    callback.messageReceived(this, FINISHED_READING, null);
            } catch ( IOException e ) {
            }
        }
    }
    
    public float getSampleRate() {
        return sampleRate;
    }
    
    public int getSampleSizeInBits() {
        return sampleBitFormat;
    }
    
    public boolean isBigEndian() {
        return sampleBigEndian;
    }
    
    public long getFrameLength() {
        return sampleFrames;
    }
    
    public int getChannels() {
        return sampleChannels;
    }
    
    public float getLengthMs() {
        return buf[0].length / (sampleRate / 1000);
    }
    
    private native int getSystemSampleRate();
}