I want to create app to play Morse Code. To do this i find that you can generate sound by writing bytes array to AudoTrack object. However, no matter of configuration, i can't hear any sound, even tho my Logs are saying, that every function i called correctly
Main class
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Consts Cons = new Consts();
final int patternLenght = 10;
String pattern;
int[] soundArray = new int[patternLenght * 2];
MediaPlayer[] mp = new MediaPlayer[patternLenght * 2];
int userInputCorrectNumber;
private String GeneratePattern(int lenght) {
String patterngen = "";
Random rand = new Random();
for (int i = 0; i < lenght; i++) {
int n = rand.nextInt(2);
String temp = Cons.ALFABET[n];
patterngen = patterngen + temp;
}
pattern = patterngen;
return patterngen;
}
private void PlayPattern(){
BeepClassMain.SetUpEverything();
BeepClassMain.GenerateSoundWave();
String morsePat=BeepClassMain.ConvertPatternToMorsePattern(pattern);
BeepClassMain.PlayPattern(morsePat);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = findViewById(R.id.make_pattern);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pattern = GeneratePattern(patternLenght);
Log.i("Pattern", pattern);
}
});
Button playpattern = findViewById(R.id.playPattern);
playpattern.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PlayPattern();
}
});
}
}
Const Class
public class Consts {final String ALFABET[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; }
BeepClassMain
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.util.Log;
public class BeepClassMain {
private static AudioTrack audioTrack;
private static int SAMPLE_RATE_HZ = 48000;
private static int numofSamples;
private static short samples[];
private static short silenceTab[];
public static void SetUpEverything() {
int bufferSize = AudioTrack.getMinBufferSize(SAMPLE_RATE_HZ, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE_HZ, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
audioTrack.setStereoVolume(AudioTrack.getMaxVolume(), AudioTrack.getMaxVolume());
numofSamples = 250 * 8000 / 1000; // duration in ms * samplerate/1000
samples = new short[numofSamples];
silenceTab=new short[numofSamples];
}
public static void GenerateSoundWave() {
for (int i = 0; i < numofSamples; i++) {
samples[i] = 15000; //example numbers of generated sound
silenceTab[i]=0; //no value, silence
}
}
public static String ConvertPatternToMorsePattern(String pattern) {
String morsePattern = "";
GenerateSoundWave();
for (int i = 0; i < pattern.length(); i++) {
if (pattern.charAt(i) == 'A') morsePattern += ".- ";
if (pattern.charAt(i) == 'B') morsePattern += "-... ";
if (pattern.charAt(i) == 'C') morsePattern += "-.-. ";
if (pattern.charAt(i) == 'D') morsePattern += "-.. ";
if (pattern.charAt(i) == 'E') morsePattern += ". ";
if (pattern.charAt(i) == 'F') morsePattern += "..-. ";
if (pattern.charAt(i) == 'G') morsePattern += "--. ";
if (pattern.charAt(i) == 'H') morsePattern += ".... ";
if (pattern.charAt(i) == 'I') morsePattern += ".. ";
if (pattern.charAt(i) == 'J') morsePattern += ".--- ";
if (pattern.charAt(i) == 'K') morsePattern += "-.- ";
if (pattern.charAt(i) == 'L') morsePattern += ".-.. ";
if (pattern.charAt(i) == 'M') morsePattern += "-- ";
if (pattern.charAt(i) == 'N') morsePattern += "-. ";
if (pattern.charAt(i) == 'O') morsePattern += "--- ";
if (pattern.charAt(i) == 'P') morsePattern += ".--. ";
if (pattern.charAt(i) == 'Q') morsePattern += "--.- ";
if (pattern.charAt(i) == 'R') morsePattern += ".-. ";
if (pattern.charAt(i) == 'S') morsePattern += "... ";
if (pattern.charAt(i) == 'T') morsePattern += "- ";
if (pattern.charAt(i) == 'U') morsePattern += "..- ";
if (pattern.charAt(i) == 'V') morsePattern += "...- ";
if (pattern.charAt(i) == 'W') morsePattern += ".-- ";
if (pattern.charAt(i) == 'X') morsePattern += "-..- ";
if (pattern.charAt(i) == 'Y') morsePattern += "-.-- ";
if (pattern.charAt(i) == 'Z') morsePattern += "--.. ";
}
Log.i("patterntomorsebeep", morsePattern);
return morsePattern;
}
private static void PlayDot() {
audioTrack.write(samples, 0, 8000);
}
private static void PlaySilence(){
audioTrack.write(silenceTab,0,8000);
}
public static int isStillSameCharacter(String morsePattern, int i) {
if (morsePattern.charAt(i + 1) == '.' || morsePattern.charAt(i + 1) == '-') return 1;
else return 0;
}
public static void PlayPattern(String morsePattern) {
audioTrack.play();
for (int i = 0; i < morsePattern.length(); i++) {
if (morsePattern.charAt(i) == '.') {
PlayDot();
if (isStillSameCharacter(morsePattern, i) == 1) continue;
PlaySilence();
}
}
}
}
For now i only wrote dot function and silence, as other characters will base on those 2 functions, but onyl increase size of them by multiplying numer of samples ex audioTrack.write(samples, 0, 8000*3);
for playing line, not dot
I'm surprised you're not hearing a clicking sound (because of the transition from 15000 to 0). Anyway, you're not hearing any beeping because you didn't put an actual waveform into samples. 15000 is just a straight line. Try a sine wave instead:
samples[i] = (short) (Math.sin(i*1000.0f/*Hz*//SAMPLE_RATE_HZ*Math.PI*2)*15000/*Amp*/);
Whenever you write code to generate a wave, in memory, like this, it's a good idea to check the array in the debugger. That way you can make sure you did your math right. This one should show a wave, starting at 0, and undulating between roughly +15000 and -15000, with a period of about 44 samples.
Also, you want to make sure that your sound arrays are of a size of at least 8000, since that's how much you're trying to write()
. Make sure this line:
250 * 8000 / 1000
is using the correct sample rate:
250 * SAMPLE_RATE_HZ / 1000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.