aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/jsyn/util/JavaSoundSampleLoader.java
blob: 56a654e3ad24d2640a40ad1299cc67175ed8771c (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
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
/*
 * Copyright 2011 Phil Burk, Mobileer Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jsyn.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

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

import com.jsyn.data.FloatSample;

/**
 * Internal class for loading audio samples. Use SampleLoader instead.
 * 
 * @author Phil Burk (C) 2011 Mobileer Inc
 */
class JavaSoundSampleLoader implements AudioSampleLoader {
    /**
     * Load a FloatSample from a File object.
     */
    @Override
    public FloatSample loadFloatSample(File fileIn) throws IOException {
        try {
            return loadFloatSample(AudioSystem.getAudioInputStream(fileIn));
        } catch (UnsupportedAudioFileException e) {
            throw new IOException(e);
        }
    }

    /**
     * Load a FloatSample from an InputStream. This is handy when loading Resources from a JAR file.
     */
    @Override
    public FloatSample loadFloatSample(InputStream inputStream) throws IOException {
        try {
            return loadFloatSample(AudioSystem.getAudioInputStream(inputStream));
        } catch (UnsupportedAudioFileException e) {
            throw new IOException(e);
        }
    }

    /**
     * Load a FloatSample from a URL.. This is handy when loading Resources from a website.
     */
    @Override
    public FloatSample loadFloatSample(URL url) throws IOException {
        try {
            return loadFloatSample(AudioSystem.getAudioInputStream(url));
        } catch (UnsupportedAudioFileException e) {
            throw new IOException(e);
        }
    }

    private FloatSample loadFloatSample(javax.sound.sampled.AudioInputStream audioInputStream)
            throws IOException, UnsupportedAudioFileException {
        float[] floatData = null;
        FloatSample sample = null;
        int bytesPerFrame = audioInputStream.getFormat().getFrameSize();
        if (bytesPerFrame == AudioSystem.NOT_SPECIFIED) {
            // some audio formats may have unspecified frame size
            // in that case we may read any amount of bytes
            bytesPerFrame = 1;
        }
        AudioFormat format = audioInputStream.getFormat();
        if (format.getEncoding() == AudioFormat.Encoding.PCM_SIGNED) {
            floatData = loadSignedPCM(audioInputStream);
        }
        sample = new FloatSample(floatData, format.getChannels());
        sample.setFrameRate(format.getFrameRate());
        return sample;
    }

    private float[] loadSignedPCM(AudioInputStream audioInputStream) throws IOException,
            UnsupportedAudioFileException {
        int totalSamplesRead = 0;
        AudioFormat format = audioInputStream.getFormat();
        int numFrames = (int) audioInputStream.getFrameLength();
        int numSamples = format.getChannels() * numFrames;
        float[] data = new float[numSamples];
        final int bytesPerFrame = format.getFrameSize();
        // Set an arbitrary buffer size of 1024 frames.
        int numBytes = 1024 * bytesPerFrame;
        byte[] audioBytes = new byte[numBytes];
        int numBytesRead = 0;
        int numFramesRead = 0;
        // Try to read numBytes bytes from the file.
        while ((numBytesRead = audioInputStream.read(audioBytes)) != -1) {
            int bytesRemainder = numBytesRead % bytesPerFrame;
            if (bytesRemainder != 0) {
                // TODO Read until you get enough data.
                throw new IOException("Read partial block of sample data!");
            }

            if (audioInputStream.getFormat().getSampleSizeInBits() == 16) {
                if (format.isBigEndian()) {
                    SampleLoader.decodeBigI16ToF32(audioBytes, 0, numBytesRead, data,
                            totalSamplesRead);
                } else {
                    SampleLoader.decodeLittleI16ToF32(audioBytes, 0, numBytesRead, data,
                            totalSamplesRead);
                }
            } else if (audioInputStream.getFormat().getSampleSizeInBits() == 24) {
                if (format.isBigEndian()) {
                    SampleLoader.decodeBigI24ToF32(audioBytes, 0, numBytesRead, data,
                            totalSamplesRead);
                } else {
                    SampleLoader.decodeLittleI24ToF32(audioBytes, 0, numBytesRead, data,
                            totalSamplesRead);
                }
            } else if (audioInputStream.getFormat().getSampleSizeInBits() == 32) {
                if (format.isBigEndian()) {
                    SampleLoader.decodeBigI32ToF32(audioBytes, 0, numBytesRead, data,
                            totalSamplesRead);
                } else {
                    SampleLoader.decodeLittleI32ToF32(audioBytes, 0, numBytesRead, data,
                            totalSamplesRead);
                }
            } else {
                throw new UnsupportedAudioFileException(
                        "Only 16, 24 or 32 bit PCM samples supported.");
            }

            // Calculate the number of frames actually read.
            numFramesRead = numBytesRead / bytesPerFrame;
            totalSamplesRead += numFramesRead * format.getChannels();
        }
        return data;
    }

}