public static List<MfiEvent> getAdpcmEvents(byte[] pcm, float time, int sampleRate, int bits, int channels, int adpcmVolume) throws InvalidMfiDataException {
int delta = getDelta(time);
Debug.println("delta: " + delta + ", time: " + time);
int velocity = (int) (adpcmVolume * maxVelocity / 100f);
AudioEngine audioEngine = NecSequencer.getAudioEngine();
int numberOfChunks = pcm.length / (PCM_MAX_BLOCK * channels);
int moduloOfChunks = pcm.length % (PCM_MAX_BLOCK * channels);
List<MfiEvent> events = new ArrayList<MfiEvent>();
// 1. data
int streamNumber = 0;
for (int i = 0; i < numberOfChunks; i++) {
byte[] temp = new byte[PCM_MAX_BLOCK * channels];
System.arraycopy(pcm, (PCM_MAX_BLOCK * channels) * i, temp, 0, PCM_MAX_BLOCK * channels);
byte[] chunk = audioEngine.encode(4, channels, temp);
if (channels == 1) {
Debug.println("wave chunk(" + i + "): " + chunk.length);
// adpcm data
events.add(getVoiceEvent(streamNumber++, channels, sampleRate, chunk));
} else {
byte[] chunkM = new byte[chunk.length / 2];
System.arraycopy(chunk, 0, chunkM, 0, chunkM.length);
Debug.println("wave l chunk(" + i + "): " + chunkM.length);
// adpcm data L
events.add(getVoiceEvent(streamNumber++, 1, sampleRate, chunkM)); // TODO channel is 1
System.arraycopy(chunk, chunkM.length, chunkM, 0, chunkM.length);
Debug.println("wave r chunk(" + i + "): " + chunkM.length);
// adpcm data R
events.add(getVoiceEvent(streamNumber++, 1, sampleRate, chunkM)); // TODO channel is 1
}
}
if (moduloOfChunks != 0) {
byte[] temp = new byte[moduloOfChunks];
System.arraycopy(pcm, (PCM_MAX_BLOCK * channels) * numberOfChunks, temp, 0, moduloOfChunks);
byte[] chunk = audioEngine.encode(4, channels, temp);
if (channels == 1) {
Debug.println("wave chunk(" + numberOfChunks + "): " + chunk.length);
// adpcm data
events.add(getVoiceEvent(streamNumber++, channels, sampleRate, chunk));