mirror of
https://github.com/xdsopl/robot36.git
synced 2025-12-06 07:12:07 +01:00
added option to change audio format
This commit is contained in:
parent
656693609d
commit
a98c945f37
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
|
|
@ -126,7 +128,13 @@ public class MainActivity extends AppCompatActivity {
|
|||
|
||||
@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();
|
||||
|
|
@ -199,7 +207,8 @@ public class MainActivity extends AppCompatActivity {
|
|||
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();
|
||||
|
|
@ -211,13 +220,14 @@ public class MainActivity extends AppCompatActivity {
|
|||
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) {
|
||||
|
|
@ -244,7 +254,10 @@ public class MainActivity extends AppCompatActivity {
|
|||
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);
|
||||
|
|
@ -281,6 +294,14 @@ public class MainActivity extends AppCompatActivity {
|
|||
initAudioRecord();
|
||||
}
|
||||
|
||||
private void setAudioFormat(int newAudioFormat) {
|
||||
if (audioFormat == newAudioFormat)
|
||||
return;
|
||||
audioFormat = newAudioFormat;
|
||||
updateAudioFormatMenu();
|
||||
initAudioRecord();
|
||||
}
|
||||
|
||||
private void updateRecordRateMenu() {
|
||||
switch (recordRate) {
|
||||
case 8000:
|
||||
|
|
@ -341,6 +362,10 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
|
@ -359,6 +384,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
state.putInt("recordRate", recordRate);
|
||||
state.putInt("recordChannel", recordChannel);
|
||||
state.putInt("audioSource", audioSource);
|
||||
state.putInt("audioFormat", audioFormat);
|
||||
state.putString("language", language);
|
||||
super.onSaveInstanceState(state);
|
||||
}
|
||||
|
|
@ -370,6 +396,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
edit.putInt("recordRate", recordRate);
|
||||
edit.putInt("recordChannel", recordChannel);
|
||||
edit.putInt("audioSource", audioSource);
|
||||
edit.putInt("audioFormat", audioFormat);
|
||||
edit.putString("language", language);
|
||||
edit.apply();
|
||||
}
|
||||
|
|
@ -379,6 +406,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
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);
|
||||
|
|
@ -386,12 +414,14 @@ public class MainActivity extends AppCompatActivity {
|
|||
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);
|
||||
|
|
@ -438,6 +468,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
updateRecordRateMenu();
|
||||
updateRecordChannelMenu();
|
||||
updateAudioSourceMenu();
|
||||
updateAudioFormatMenu();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -576,6 +607,14 @@ public class MainActivity extends AppCompatActivity {
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Reference in a new issue