Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use phone speaker to produce sound #1969

Merged
merged 1 commit into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions app/src/main/java/io/pslab/activity/WaveGeneratorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -182,6 +186,8 @@ public class WaveGeneratorActivity extends AppCompatActivity {
private RelativeLayout pwmModeControls;
private RelativeLayout squareModeControls;
private LineChart previewChart;
private boolean isPlayingSound = false;
private ProduceSoundTask produceSoundTask;

@SuppressLint("ClickableViewAccessibility")
@Override
Expand Down Expand Up @@ -451,6 +457,23 @@ public void onStopTrackingTouch(IndicatorSeekBar seekBar) {
setReceivedData();
}
chartInit();

btnProduceSound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPlayingSound) {
btnProduceSound.setText(getResources().getString(R.string.produce_sound_text));
produceSoundTask.cancel(true);
produceSoundTask = null;
isPlayingSound = false;
} else {
btnProduceSound.setText(getResources().getString(R.string.stop_sound_text));
produceSoundTask = new ProduceSoundTask();
produceSoundTask.execute();
isPlayingSound = true;
}
}
});
}

public void saveWaveConfig() {
Expand Down Expand Up @@ -1252,4 +1275,50 @@ public final int getValue() {
return value;
}
}

private class ProduceSoundTask extends AsyncTask<Void, Void, Void> {

private AudioTrack track;

@Override
protected Void doInBackground(Void... voids) {
short[] buffer = new short[1024];
track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, buffer.length, AudioTrack.MODE_STREAM);
float angle = 0;
float samples[] = new float[1024];

track.play();
double frequency;
while (isPlayingSound) {
if (WaveGeneratorCommon.mode_selected == WaveConst.SQUARE) {
frequency = WaveGeneratorCommon.wave.get(waveBtnActive).get(WaveConst.FREQUENCY);
} else {
frequency = WaveGeneratorCommon.wave.get(WaveConst.SQR1).get(WaveConst.FREQUENCY);
}
float increment = (float) ((2 * Math.PI) * frequency / 44100);
for (int i = 0; i < samples.length; i++) {
samples[i] = (float) Math.sin(angle);
if (WaveGeneratorCommon.mode_selected == WaveConst.PWM) {
samples[i] = (samples[i] >= 0.0) ? 1 : -1;
} else {
if (WaveGeneratorCommon.wave.get(waveBtnActive).get(WaveConst.WAVETYPE) == 2) {
samples[i] = (float) ((2 / Math.PI) * Math.asin(samples[i]));
}
}
buffer[i] = (short) (samples[i] * Short.MAX_VALUE);
angle += increment;
}
track.write(buffer, 0, buffer.length);
}
return null;
}

@Override
protected void onCancelled() {
super.onCancelled();
track.flush();
track.stop();
track.release();
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/wave_generator_main_controls.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
android:layout_marginBottom="@dimen/margin_btn"
android:layout_weight="1"
android:background="@drawable/btn_back_rounded"
android:stateListAnimator="@animator/selector_animator"
android:minWidth="@dimen/btn_min_width"
android:text="@string/produce_sound_text"
android:textAllCaps="false"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@
<string name="text_pwm">PWM</string>
<string name="view_text">View</string>
<string name="produce_sound_text">Produce Sound</string>
<string name="stop_sound_text">Stop Sound</string>
<string name="save_text">Save</string>
<string name="phase_holder_text">45 deg</string>
<string name="duty_holder_text">50 %</string>
Expand Down