package com.rfg.synth; import java.nio.*; import com.un4seen.bass.BASS; import com.un4seen.bass.BASS.*; import android.R.bool; import android.app.Activity; import android.os.Bundle; import android.util.FloatMath; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class RFGSynthActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Init Bass boolean inited = BASS.BASS_Init(-1, _sampleRate,BASS.BASS_DEVICE_MONO); // Events Button button = (Button) findViewById(R.id.btn_start); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Start-Button Geklickt StartLoopChan(); } }); // Events Button button_stop = (Button) findViewById(R.id.btn_stop); button_stop.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Stop-Button Geklickt BASS.BASS_ChannelStop(loopChannel); } }); } int loopChannel; float theta = 0; float angle = 0; int _sampleRate = 44100; public double frequency = 440.0; private void StartLoopChan() { // Init Theta theta = (float) (Math.PI * frequency * 2) / (float) (_sampleRate); // LoopChan STREAMPROC streamProc = new BASS.STREAMPROC() { @Override public int STREAMPROC(int handle, ByteBuffer buffer, int length, Object user) { // Set Byte Order to Little Endian ! buffer.order(null); // Sine Wave Test (16Bit = 2Bytes for 1Short Value) ShortBuffer sbuffer = buffer.asShortBuffer(); short[] b = new short[length / 2]; // allocate an "short" array for the sample data // Sine Init double amplitude = Short.MAX_VALUE; for (int a = 0; a < b.length; a++) { b[a] = (short) ((float)(amplitude * FloatMath.sin(angle))); angle += theta; } sbuffer.put(b); // put the array back into the buffer return length; } }; //Set Buffer Size (default 30ms) //BASS.BASS_SetConfig(BASS.BASS_CONFIG_DEV_BUFFER, 10); loopChannel = BASS.BASS_StreamCreate(_sampleRate, 1,BASS.BASS_SAMPLE_LOOP, streamProc, 0); // int err = BASS.BASS_ErrorGetCode(); BASS.BASS_ChannelPlay(loopChannel, false); } }