Skip to content

Commit

Permalink
added option to change audio format
Browse files Browse the repository at this point in the history
  • Loading branch information
xdsopl committed Aug 14, 2024
1 parent 6566936 commit a98c945
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 6 deletions.
51 changes: 45 additions & 6 deletions app/src/main/java/xdsopl/robot36/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class MainActivity extends AppCompatActivity {
private PixelBuffer peakMeterBuffer;
private ImageView peakMeterView;
private PixelBuffer imageBuffer;
private short[] shortBuffer;
private float[] recordBuffer;
private AudioRecord audioRecord;
private Decoder decoder;
Expand All @@ -80,6 +81,7 @@ public class MainActivity extends AppCompatActivity {
private int recordRate;
private int recordChannel;
private int audioSource;
private int audioFormat;
private int fgColor;
private int thinColor;
private int tintColor;
Expand Down Expand Up @@ -126,7 +128,13 @@ public void onMarkerReached(AudioRecord ignore) {

@Override
public void onPeriodicNotification(AudioRecord audioRecord) {
audioRecord.read(recordBuffer, 0, recordBuffer.length, AudioRecord.READ_BLOCKING);
if (shortBuffer == null) {
audioRecord.read(recordBuffer, 0, recordBuffer.length, AudioRecord.READ_BLOCKING);
} else {
audioRecord.read(shortBuffer, 0, shortBuffer.length, AudioRecord.READ_BLOCKING);
for (int i = 0; i < shortBuffer.length; ++i)
recordBuffer[i] = .000030517578125f * shortBuffer[i];
}
processPeakMeter();
boolean newLines = decoder.process(recordBuffer, recordChannel);
processFreqPlot();
Expand Down Expand Up @@ -199,7 +207,8 @@ private void initAudioRecord() {
rateChanged = audioRecord.getSampleRate() != recordRate;
boolean channelChanged = audioRecord.getChannelCount() != (recordChannel == 0 ? 1 : 2);
boolean sourceChanged = audioRecord.getAudioSource() != audioSource;
if (!rateChanged && !channelChanged && !sourceChanged)
boolean formatChanged = audioRecord.getAudioFormat() != audioFormat;
if (!rateChanged && !channelChanged && !sourceChanged && !formatChanged)
return;
stopListening();
audioRecord.release();
Expand All @@ -211,13 +220,14 @@ private void initAudioRecord() {
channelCount = 2;
channelConfig = AudioFormat.CHANNEL_IN_STEREO;
}
int sampleSize = 4;
int sampleSize = audioFormat == AudioFormat.ENCODING_PCM_FLOAT ? 4 : 2;
int frameSize = sampleSize * channelCount;
int audioFormat = AudioFormat.ENCODING_PCM_FLOAT;
int readsPerSecond = 50;
int bufferSize = Integer.highestOneBit(recordRate) * frameSize;
int frameCount = recordRate / readsPerSecond;
recordBuffer = new float[frameCount * channelCount];
int bufferCount = frameCount * channelCount;
recordBuffer = new float[bufferCount];
shortBuffer = audioFormat == AudioFormat.ENCODING_PCM_FLOAT ? null : new short[bufferCount];
try {
audioRecord = new AudioRecord(audioSource, recordRate, channelConfig, audioFormat, bufferSize);
if (audioRecord.getState() == AudioRecord.STATE_INITIALIZED) {
Expand All @@ -244,7 +254,10 @@ private void startListening() {
if (audioRecord != null) {
audioRecord.startRecording();
if (audioRecord.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
audioRecord.read(recordBuffer, 0, recordBuffer.length, AudioRecord.READ_BLOCKING);
if (shortBuffer == null)
audioRecord.read(recordBuffer, 0, recordBuffer.length, AudioRecord.READ_BLOCKING);
else
audioRecord.read(shortBuffer, 0, recordBuffer.length, AudioRecord.READ_BLOCKING);
setStatus(R.string.listening);
} else {
setStatus(R.string.audio_recording_error);
Expand Down Expand Up @@ -281,6 +294,14 @@ private void setAudioSource(int newAudioSource) {
initAudioRecord();
}

private void setAudioFormat(int newAudioFormat) {
if (audioFormat == newAudioFormat)
return;
audioFormat = newAudioFormat;
updateAudioFormatMenu();
initAudioRecord();
}

private void updateRecordRateMenu() {
switch (recordRate) {
case 8000:
Expand Down Expand Up @@ -341,6 +362,10 @@ private void updateAudioSourceMenu() {
}
}

private void updateAudioFormatMenu() {
menu.findItem(audioFormat == AudioFormat.ENCODING_PCM_FLOAT ? R.id.action_set_floating_point : R.id.action_set_fixed_point).setChecked(true);
}

private final int permissionID = 1;

@Override
Expand All @@ -359,6 +384,7 @@ protected void onSaveInstanceState(@NonNull Bundle state) {
state.putInt("recordRate", recordRate);
state.putInt("recordChannel", recordChannel);
state.putInt("audioSource", audioSource);
state.putInt("audioFormat", audioFormat);
state.putString("language", language);
super.onSaveInstanceState(state);
}
Expand All @@ -370,6 +396,7 @@ private void storeSettings() {
edit.putInt("recordRate", recordRate);
edit.putInt("recordChannel", recordChannel);
edit.putInt("audioSource", audioSource);
edit.putInt("audioFormat", audioFormat);
edit.putString("language", language);
edit.apply();
}
Expand All @@ -379,19 +406,22 @@ protected void onCreate(Bundle state) {
final int defaultSampleRate = 44100;
final int defaultChannelSelect = 0;
final int defaultAudioSource = MediaRecorder.AudioSource.MIC;
final int defaultAudioFormat = AudioFormat.ENCODING_PCM_FLOAT;
final String defaultLanguage = "system";
if (state == null) {
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
AppCompatDelegate.setDefaultNightMode(pref.getInt("nightMode", AppCompatDelegate.getDefaultNightMode()));
recordRate = pref.getInt("recordRate", defaultSampleRate);
recordChannel = pref.getInt("recordChannel", defaultChannelSelect);
audioSource = pref.getInt("audioSource", defaultAudioSource);
audioFormat = pref.getInt("audioFormat", defaultAudioFormat);
language = pref.getString("language", defaultLanguage);
} else {
AppCompatDelegate.setDefaultNightMode(state.getInt("nightMode", AppCompatDelegate.getDefaultNightMode()));
recordRate = state.getInt("recordRate", defaultSampleRate);
recordChannel = state.getInt("recordChannel", defaultChannelSelect);
audioSource = state.getInt("audioSource", defaultAudioSource);
audioFormat = state.getInt("audioFormat", defaultAudioFormat);
language = state.getString("language", defaultLanguage);
}
super.onCreate(state);
Expand Down Expand Up @@ -438,6 +468,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
updateRecordRateMenu();
updateRecordChannelMenu();
updateAudioSourceMenu();
updateAudioFormatMenu();
return true;
}

Expand Down Expand Up @@ -576,6 +607,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
setAudioSource(MediaRecorder.AudioSource.UNPROCESSED);
return true;
}
if (id == R.id.action_set_floating_point) {
setAudioFormat(AudioFormat.ENCODING_PCM_FLOAT);
return true;
}
if (id == R.id.action_set_fixed_point) {
setAudioFormat(AudioFormat.ENCODING_PCM_16BIT);
return true;
}
if (id == R.id.action_enable_night_mode) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
return true;
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@
</group>
</menu>
</item>
<item android:title="@string/audio_format">
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/action_set_floating_point"
android:title="@string/floating_point" />
<item
android:id="@+id/action_set_fixed_point"
android:title="@string/fixed_point" />
</group>
</menu>
</item>
</menu>
</item>
<item
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<string name="source_camcorder">Videokamera</string>
<string name="source_voice_recognition">Spracherkennung</string>
<string name="source_unprocessed">Unbearbeitet</string>
<string name="audio_format">Audioformat</string>
<string name="fixed_point">Festkomma</string>
<string name="floating_point">Gleitkomma</string>
<string name="audio_init_failed">Audioinitialisierung fehlgeschlagen</string>
<string name="audio_setup_failed">Audioeinrichtung fehlgeschlagen</string>
<string name="audio_permission_denied">Audioberechtigung verweigert</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<string name="source_camcorder">Filmadora</string>
<string name="source_voice_recognition">Reconhecimento de voz</string>
<string name="source_unprocessed">Não processado</string>
<string name="audio_format">Formato de Áudio</string>
<string name="fixed_point">Ponto Fixo</string>
<string name="floating_point">Ponto Flutuante</string>
<string name="audio_init_failed">Falha ao iniciar o áudio</string>
<string name="audio_setup_failed">Falha na configuração de áudio</string>
<string name="audio_permission_denied">Permissão de áudio negada</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<string name="source_camcorder">Камера</string>
<string name="source_voice_recognition">Распознавание голоса</string>
<string name="source_unprocessed">Необработанный</string>
<string name="audio_format">Аудио формат</string>
<string name="fixed_point">Фиксированная точка</string>
<string name="floating_point">Плавающая точка</string>
<string name="audio_init_failed">Ошибка инициализации аудио</string>
<string name="audio_setup_failed">Ошибка настройки аудио</string>
<string name="audio_permission_denied">Аудио - отказано в доступе</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<string name="source_camcorder">摄录机</string>
<string name="source_voice_recognition">音频识别</string>
<string name="source_unprocessed">不处理</string>
<string name="audio_format">音频格式</string>
<string name="fixed_point">定点数</string>
<string name="floating_point">浮点数</string>
<string name="audio_init_failed">初始化音频失败</string>
<string name="audio_setup_failed">音频设定失败</string>
<string name="audio_permission_denied">音频权限被拒绝</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<string name="source_camcorder">Camcorder</string>
<string name="source_voice_recognition">Voice Recognition</string>
<string name="source_unprocessed">Unprocessed</string>
<string name="audio_format">Audio Format</string>
<string name="fixed_point">Fixed Point</string>
<string name="floating_point">Floating Point</string>
<string name="audio_init_failed">Audio init failed</string>
<string name="audio_setup_failed">Audio setup failed</string>
<string name="audio_permission_denied">Audio permission denied</string>
Expand Down

0 comments on commit a98c945

Please sign in to comment.