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

Add state of the art voice bitrates #90

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ private AppConstants() {}
public static final int RECORD_SAMPLE_RATE_32000 = 32000;
public static final int RECORD_SAMPLE_RATE_48000 = 48000;

public static final int RECORD_ENCODING_BITRATE_12000 = 12000; //Bitrate for 3gp format
public static final int RECORD_ENCODING_BITRATE_8000 = 8000;
public static final int RECORD_ENCODING_BITRATE_16000 = 16000;
public static final int RECORD_ENCODING_BITRATE_24000 = 24000;
public static final int RECORD_ENCODING_BITRATE_48000 = 48000;
public static final int RECORD_ENCODING_BITRATE_96000 = 96000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,20 @@ protected void onCreate(Bundle savedInstanceState) {
sampleRateSetting.setOnInfoClickListener(v -> AndroidUtils.showInfoDialog(SettingsActivity.this, R.string.info_frequency));

bitrateSetting = findViewById(R.id.setting_bitrate);


rates = getResources().getStringArray(R.array.bit_rates2);
rateKeys = new String[] {
// SettingsMapper.BITRATE_24000,
SettingsMapper.BITRATE_8000,
SettingsMapper.BITRATE_16000,
SettingsMapper.BITRATE_48000,
SettingsMapper.BITRATE_96000,
SettingsMapper.BITRATE_128000,
SettingsMapper.BITRATE_192000,
SettingsMapper.BITRATE_256000,
};
bitrateSetting.setData(rates, rateKeys);

bitrateSetting.setOnChipCheckListener((key, name, checked) -> presenter.setSettingRecordingBitrate(SettingsMapper.keyToBitrate(key)));
bitrateSetting.setTitle(R.string.bitrate);
bitrateSetting.setOnInfoClickListener(v -> AndroidUtils.showInfoDialog(SettingsActivity.this, R.string.info_bitrate));
Expand Down Expand Up @@ -528,8 +532,14 @@ public void updateRecordingInfo(String format) {
if (format.equals(AppConstants.FORMAT_3GP)) {
channelsSetting.removeChip(new String[] {SettingsMapper.CHANNEL_COUNT_STEREO});
channelsSetting.setSelected(SettingsMapper.CHANNEL_COUNT_MONO);
final String[] rates = getResources().getStringArray(R.array.bit_rates);
bitrateSetting.setData(rates, rateKeys);
bitrateSetting.setSelected(SettingsMapper.BITRATE_16000);
} else {
channelsSetting.addChip(new String[] {SettingsMapper.CHANNEL_COUNT_STEREO}, new String[] {getString(R.string.stereo)});
final String[] rates = getResources().getStringArray(R.array.bit_rates2);
bitrateSetting.setData(rates, rateKeys);
bitrateSetting.setSelected(SettingsMapper.BITRATE_48000);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public SettingsMapper(Context context) {
public final static String SAMPLE_RATE_44100 = "44100";
public final static String SAMPLE_RATE_48000 = "48000";

public final static String BITRATE_12000 = "12000";
// public final static String BITRATE_24000 = "24000";
public final static String BITRATE_8000 = "8000";
public final static String BITRATE_16000 = "16000";
public final static String BITRATE_48000 = "48000";
public final static String BITRATE_96000 = "96000";
public final static String BITRATE_128000 = "128000";
Expand Down Expand Up @@ -233,8 +233,10 @@ public static String sampleRateToKey(int sampleRate) {

public static int keyToBitrate(String bitrateKey) {
switch (bitrateKey) {
// case BITRATE_24000:
// return AppConstants.RECORD_ENCODING_BITRATE_24000;
case BITRATE_8000:
return AppConstants.RECORD_ENCODING_BITRATE_8000;
case BITRATE_16000:
return AppConstants.RECORD_ENCODING_BITRATE_16000;
case BITRATE_48000:
return AppConstants.RECORD_ENCODING_BITRATE_48000;
case BITRATE_96000:
Expand All @@ -252,10 +254,10 @@ public static int keyToBitrate(String bitrateKey) {

public static String bitrateToKey(int bitrate) {
switch (bitrate) {
case AppConstants.RECORD_ENCODING_BITRATE_12000:
return BITRATE_12000;
// case AppConstants.RECORD_ENCODING_BITRATE_24000:
// return BITRATE_24000;
case AppConstants.RECORD_ENCODING_BITRATE_8000:
return BITRATE_8000;
case AppConstants.RECORD_ENCODING_BITRATE_16000:
return BITRATE_16000;
case AppConstants.RECORD_ENCODING_BITRATE_48000:
return BITRATE_48000;
case AppConstants.RECORD_ENCODING_BITRATE_96000:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,46 +266,32 @@ private void updateAvailableSpace() {
final long space = FileUtil.getFree(fileRepository.getRecordingDir());
String format = prefs.getSettingRecordingFormat();
int sampleRate = prefs.getSettingSampleRate();

int channelsCount = prefs.getSettingChannelCount();
if (format.equals(AppConstants.FORMAT_3GP)) {
if (view != null) {
view.showAvailableSpace(
TimeUtils.formatTimeIntervalHourMinSec(
spaceToTimeSecs(space, format, sampleRate,
AppConstants.RECORD_ENCODING_BITRATE_12000, AppConstants.RECORD_AUDIO_MONO)
)
);
view.showSizePerMin(
decimalFormat.format(
sizePerMin(format, sampleRate, AppConstants.RECORD_ENCODING_BITRATE_12000,
AppConstants.RECORD_AUDIO_MONO) / 1000000f
)
);
view.showInformation(settingsMapper.convertFormatsToString(format) + AppConstants.SEPARATOR
+ settingsMapper.convertSampleRateToString(sampleRate) + AppConstants.SEPARATOR
+ settingsMapper.formatBitrate(AppConstants.RECORD_ENCODING_BITRATE_12000 / 1000) + AppConstants.SEPARATOR
+ settingsMapper.convertChannelsToString(AppConstants.RECORD_AUDIO_MONO));
}
} else {
int bitrate = prefs.getSettingBitrate();
int channelsCount = prefs.getSettingChannelCount();
final long time = spaceToTimeSecs(space, format, sampleRate, bitrate, channelsCount);
if (view != null) {
view.showAvailableSpace(TimeUtils.formatTimeIntervalHourMinSec(time));
view.showSizePerMin(decimalFormat.format(sizePerMin(format, sampleRate, bitrate, channelsCount) / 1000000f));
switch (format) {
default:
case AppConstants.FORMAT_M4A:
view.showInformation(settingsMapper.convertFormatsToString(format) + AppConstants.SEPARATOR
+ settingsMapper.convertSampleRateToString(sampleRate) + AppConstants.SEPARATOR
+ settingsMapper.convertBitratesToString(bitrate) + AppConstants.SEPARATOR
+ settingsMapper.convertChannelsToString(channelsCount));
break;
case AppConstants.FORMAT_WAV:
view.showInformation(settingsMapper.convertFormatsToString(format) + AppConstants.SEPARATOR
+ settingsMapper.convertSampleRateToString(sampleRate) + AppConstants.SEPARATOR
+ settingsMapper.convertChannelsToString(channelsCount));
break;
}
channelsCount = 1;
}
int bitrate = prefs.getSettingBitrate();
if (format.equals(AppConstants.FORMAT_WAV)) {
bitrate = 1411 * channelsCount;
}
final long time = spaceToTimeSecs(space, format, sampleRate, bitrate, channelsCount);
if (view != null) {
view.showAvailableSpace(TimeUtils.formatTimeIntervalHourMinSec(time));
view.showSizePerMin(new DecimalFormat("#.##").format(sizePerMin(format, sampleRate, bitrate, channelsCount) / 1000000f));
switch (format) {
default:
case AppConstants.FORMAT_M4A:
view.showInformation(settingsMapper.convertFormatsToString(format) + AppConstants.SEPARATOR
+ settingsMapper.convertSampleRateToString(sampleRate) + AppConstants.SEPARATOR
+ settingsMapper.convertBitratesToString(bitrate) + AppConstants.SEPARATOR
+ settingsMapper.convertChannelsToString(channelsCount));
break;
case AppConstants.FORMAT_WAV:
view.showInformation(settingsMapper.convertFormatsToString(format) + AppConstants.SEPARATOR
+ settingsMapper.convertSampleRateToString(sampleRate) + AppConstants.SEPARATOR
+ settingsMapper.convertChannelsToString(channelsCount));
break;
}
}
}
Expand Down Expand Up @@ -337,9 +323,9 @@ private long sizePerMin(String recordingFormat, int sampleRate, int bitrate, int
private void updateRecordingFormat(String formatKey) {
switch (formatKey) {
case AppConstants.FORMAT_WAV:
case AppConstants.FORMAT_3GP:
view.hideBitrateSelector();
break;
case AppConstants.FORMAT_3GP:
case AppConstants.FORMAT_M4A:
default:
view.showBitrateSelector();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private void updateSizePerMin() {
if (format.equals(AppConstants.FORMAT_3GP)) {
view.showSizePerMin(
decimalFormat.format(
sizePerMin(format, sampleRate, AppConstants.RECORD_ENCODING_BITRATE_12000,
sizePerMin(format, sampleRate, AppConstants.RECORD_ENCODING_BITRATE_16000,
AppConstants.RECORD_AUDIO_MONO) / 1000000f
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ public void startRecording(String outputFile, int channelCount, int sampleRate,
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
if (sampleRate > 8000) {
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB); //AMR_WR records with 16000 Hz frequency, ~23 kbps bitrate
} else {
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //AMR_NB records with 8000 Hz frequency, ~12 kbps bitrate
}
recorder.setAudioEncoder(sampleRate == 8000 ? MediaRecorder.AudioEncoder.AMR_NB : MediaRecorder.AudioEncoder.AMR_WB);
recorder.setAudioEncodingBitRate(bitrate);
recorder.setMaxDuration(-1); //Duration unlimited or use RECORD_MAX_DURATION
recorder.setOutputFile(recordFile.getAbsolutePath());
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,11 @@ public boolean hasAvailableSpace(Context context) throws IllegalArgumentExceptio
space = FileUtil.getAvailableInternalMemorySize(context);
}

final long time = spaceToTimeSecs(space, prefs.getSettingRecordingFormat(),
prefs.getSettingSampleRate(), prefs.getSettingBitrate(), prefs.getSettingChannelCount());
final long time = spaceToTimeSecs(space, prefs.getSettingRecordingFormat(), prefs.getSettingBitrate(), prefs.getSettingChannelCount());
return time > AppConstants.MIN_REMAIN_RECORDING_TIME;
}

private long spaceToTimeSecs(long spaceBytes, String recordingFormat, int sampleRate, int bitrate, int channels) {
switch (recordingFormat) {
case AppConstants.FORMAT_3GP:
return 1000 * (spaceBytes/(AppConstants.RECORD_ENCODING_BITRATE_12000/8));
case AppConstants.FORMAT_M4A:
return 1000 * (spaceBytes/(bitrate/8));
case AppConstants.FORMAT_WAV:
return 1000 * (spaceBytes/(sampleRate * channels * 2));
default:
return 0;
}
private long spaceToTimeSecs(long spaceBytes, String recordingFormat, int bitrate, int channels) {
return 1000 * (spaceBytes/(bitrate/8));
}
}
8 changes: 7 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,14 @@
<item>3gp</item>
</string-array>

<string-array name="bit_rates">
<item>8 kbps</item>
<item>16 kbps</item>
</string-array>

<string-array name="bit_rates2">
<!-- <item>24 kbps</item>-->
<item>8 kbps</item>
<item>16 kbps</item>
<item>48 kbps</item>
<item>96 kbps</item>
<item>128 kbps</item>
Expand Down