Back to project page android-wav-mixer.
The source code is released under:
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2014 Nathan Sizemore <nathanrsizemore@gmail.com> Everyone is permitted to copy and distribute verbatim or modified ...
If you think the Android project android-wav-mixer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.nathansizemore.beatmaker; //from w w w. j ava2 s. c om import java.io.IOException; import java.io.PipedInputStream; import android.app.Activity; import android.media.AudioFormat; import android.media.AudioManager; import android.media.AudioTrack; import android.media.AudioTrack.OnPlaybackPositionUpdateListener; import android.os.Bundle; import android.os.Message; import com.example.beatmaker.R; import com.nathansizemore.beatmaker.audiomixer.AudioChannel; import com.nathansizemore.beatmaker.audiomixer.Mixer; import com.nathansizemore.beatmaker.audiomixer.Sample; import com.nathansizemore.beatmaker.audiomixer.extras.MixerStatus; import com.nathansizemore.beatmaker.audiomixer.extras.SampleChange; import com.nathansizemore.beatmaker.audiomixer.extras.WavStream; import com.nathansizemore.beatmaker.fragments.MainClipSelect; import com.nathansizemore.beatmaker.fragments.interfaces.SampleSelect; public class ClipSelectActivity extends Activity implements SampleSelect, MixerStatus { // Logging support private final String TAG = "ClipSelect"; private PipedInputStream stream; private AudioTrack audioTrack; private int bufferSize; private int currentDownBeat; private OnPlaybackPositionUpdateListener downBeatListener; private Mixer mixerThread; private int currentMeasure; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_clip_select); if (savedInstanceState == null) { getFragmentManager().beginTransaction() .add(R.id.container, new MainClipSelect()).commit(); } } @Override public void onStart() { super.onStart(); mixerThread = new Mixer(this); mixerThread.start(); } @Override public void onPause() { super.onPause(); Message msg = Message.obtain(); msg.what = SampleChange.KILL; mixerThread.pipeLine.sendMessage(msg); } @Override public void onStop() { super.onStop(); } @Override public void onSampleSelect(Sample sample) { Message msg; AudioChannel newClip = new AudioChannel(); switch (sample) { case Drums: msg = Message.obtain(); msg.what = SampleChange.ADD_CLIP; newClip.id = R.raw.drums; newClip.downBeat = currentDownBeat; newClip.measure = currentMeasure; msg.obj = newClip; mixerThread.pipeLine.sendMessage(msg); break; case Bass: msg = Message.obtain(); msg.what = SampleChange.ADD_CLIP; newClip.id = R.raw.bass; newClip.downBeat = currentDownBeat; newClip.measure = currentMeasure; msg.obj = newClip; mixerThread.pipeLine.sendMessage(msg); break; case BassSynth: msg = Message.obtain(); msg.what = SampleChange.ADD_CLIP; newClip.id = R.raw.bass_synth; newClip.downBeat = currentDownBeat; newClip.measure = currentMeasure; msg.obj = newClip; mixerThread.pipeLine.sendMessage(msg); break; case Piano: msg = Message.obtain(); msg.what = SampleChange.ADD_CLIP; newClip.id = R.raw.piano; newClip.downBeat = currentDownBeat; newClip.measure = currentMeasure; msg.obj = newClip; mixerThread.pipeLine.sendMessage(msg); break; default: break; } } @Override public void onPipeReady(PipedInputStream stream) { this.stream = stream; } @Override public void onBufferStart(int bufferSize) { this.bufferSize = bufferSize; byte[] buffer = new byte[bufferSize]; try { stream.read(buffer); } catch (IOException e) { e.printStackTrace(); } startAudioTrack(buffer); } @Override public void onMixerError() { // TODO - Handle errors and shit } @Override public void onNextBuffer(int bufferLength) { byte[] nextBuffer = new byte[bufferLength]; try { stream.read(nextBuffer, 0, bufferLength); } catch (IOException e) { e.printStackTrace(); } audioTrack.write(nextBuffer, 0, bufferLength); } private void startAudioTrack(byte[] buffer) { currentMeasure = 0; currentDownBeat = 0; audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, 2, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM); audioTrack.setPositionNotificationPeriod(WavStream.SAMPLE_INTERVAL); downBeatListener = new OnPlaybackPositionUpdateListener() { @Override public void onMarkerReached(AudioTrack track) { // Do nothing. // Abstract classes are stupid and every method must be overloaded } @Override public void onPeriodicNotification(AudioTrack track) { currentDownBeat++; // New measure? if (currentDownBeat > 3) { currentDownBeat = 0; currentMeasure++; if (currentMeasure > 1) { currentMeasure = 0; } } requestNextBuffer(currentMeasure, currentDownBeat); } }; audioTrack.setPlaybackPositionUpdateListener(downBeatListener); audioTrack.write(buffer, 0, buffer.length); audioTrack.play(); } private void requestNextBuffer(int forMeasure, int forDownBeat) { Message msg = Message.obtain(); msg.what = SampleChange.BUILD_NEXT; msg.arg1 = forMeasure; msg.arg2 = forDownBeat; mixerThread.pipeLine.sendMessage(msg); } }