From 734b7a60b1b6fa1745a625f46056bb6c4b55eb2c Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Tue, 14 Feb 2017 15:32:37 +0300 Subject: [PATCH] move to coders --- .../activities/RecordingActivity.java | 19 +------ .../axet/audiorecorder/app/Storage.java | 32 +---------- .../axet/audiorecorder/encoders/Factory.java | 53 +++++++++++++++++++ 3 files changed, 57 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/com/github/axet/audiorecorder/encoders/Factory.java diff --git a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java index 78e83c4..cfbea02 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java +++ b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java @@ -10,7 +10,6 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.pm.PackageManager; -import android.graphics.drawable.ColorDrawable; import android.media.AudioFormat; import android.media.AudioRecord; import android.media.AudioTrack; @@ -44,11 +43,8 @@ import com.github.axet.audiorecorder.app.Sound; import com.github.axet.audiorecorder.app.Storage; import com.github.axet.audiorecorder.encoders.Encoder; import com.github.axet.audiorecorder.encoders.EncoderInfo; +import com.github.axet.audiorecorder.encoders.Factory; import com.github.axet.audiorecorder.encoders.FileEncoder; -import com.github.axet.audiorecorder.encoders.Format3GP; -import com.github.axet.audiorecorder.encoders.FormatM4A; -import com.github.axet.audiorecorder.encoders.FormatMKA; -import com.github.axet.audiorecorder.encoders.FormatWAV; import com.github.axet.audiorecorder.services.RecordingService; import com.github.axet.audiorecorder.widgets.PitchView; @@ -786,18 +782,7 @@ public class RecordingActivity extends AppCompatActivity { final SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(this); String ext = shared.getString(MainApplication.PREFERENCE_ENCODING, ""); - if (ext.equals("wav")) { - e = new FormatWAV(info, out); - } - if (ext.equals("m4a")) { - e = new FormatM4A(info, out); - } - if (ext.equals("3gp")) { - e = new Format3GP(info, out); - } - if (ext.equals("mka")) { - e = new FormatMKA(info, out); - } + e = Factory.getEncoder(ext, info, out); encoder = new FileEncoder(this, in, e); diff --git a/app/src/main/java/com/github/axet/audiorecorder/app/Storage.java b/app/src/main/java/com/github/axet/audiorecorder/app/Storage.java index 9c35bac..43a229d 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/app/Storage.java +++ b/app/src/main/java/com/github/axet/audiorecorder/app/Storage.java @@ -4,13 +4,13 @@ import android.Manifest; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageManager; -import android.media.AudioFormat; import android.os.Build; import android.os.StatFs; import android.preference.PreferenceManager; import android.support.v4.content.ContextCompat; import com.github.axet.audiorecorder.R; +import com.github.axet.audiorecorder.encoders.Factory; import java.io.File; import java.io.FileInputStream; @@ -197,36 +197,8 @@ public class Storage { int rate = Integer.parseInt(shared.getString(MainApplication.PREFERENCE_RATE, "")); String ext = shared.getString(MainApplication.PREFERENCE_ENCODING, ""); - if (ext.equals("m4a") || ext.equals("mka")) { - long y1 = 365723; // one minute sample 16000Hz - long x1 = 16000; // at 16000 - long y2 = 493743; // one minute sample - long x2 = 44000; // at 44000 - long x = rate; - long y = (x - x1) * (y2 - y1) / (x2 - x1) + y1; - - int m = MainApplication.getChannels(context); - long perSec = (y / 60) * m; - return free / perSec * 1000; - } - - if (ext.equals("mka")) { - long y1 = 365723; // one minute sample 16000Hz - long x1 = 16000; // at 16000 - long y2 = 493743; // one minute sample - long x2 = 44000; // at 44000 - long x = rate; - long y = (x - x1) * (y2 - y1) / (x2 - x1) + y1; - - int m = MainApplication.getChannels(context); - long perSec = (y / 60) * m; - return free / perSec * 1000; - } - - // default raw int m = MainApplication.getChannels(context); - int c = RawSamples.AUDIO_FORMAT == AudioFormat.ENCODING_PCM_16BIT ? 2 : 1; - long perSec = (c * m * rate); + long perSec = Factory.getEncoderRate(ext, rate) * m; return free / perSec * 1000; } diff --git a/app/src/main/java/com/github/axet/audiorecorder/encoders/Factory.java b/app/src/main/java/com/github/axet/audiorecorder/encoders/Factory.java new file mode 100644 index 0000000..9af66c0 --- /dev/null +++ b/app/src/main/java/com/github/axet/audiorecorder/encoders/Factory.java @@ -0,0 +1,53 @@ +package com.github.axet.audiorecorder.encoders; + +import android.media.AudioFormat; + +import com.github.axet.audiorecorder.app.RawSamples; + +import java.io.File; + +public class Factory { + + public static Encoder getEncoder(String ext, EncoderInfo info, File out) { + if (ext.equals("wav")) { + return new FormatWAV(info, out); + } + if (ext.equals("m4a")) { + return new FormatM4A(info, out); + } + if (ext.equals("3gp")) { + return new Format3GP(info, out); + } + if (ext.equals("mka")) { + return new FormatMKA(info, out); + } + return null; + } + + public static long getEncoderRate(String ext, int rate) { + if (ext.equals("m4a") || ext.equals("mka")) { + long y1 = 365723; // one minute sample 16000Hz + long x1 = 16000; // at 16000 + long y2 = 493743; // one minute sample + long x2 = 44000; // at 44000 + long x = rate; + long y = (x - x1) * (y2 - y1) / (x2 - x1) + y1; + + return y / 60; + } + + if (ext.equals("mka")) { + long y1 = 365723; // one minute sample 16000Hz + long x1 = 16000; // at 16000 + long y2 = 493743; // one minute sample + long x2 = 44000; // at 44000 + long x = rate; + long y = (x - x1) * (y2 - y1) / (x2 - x1) + y1; + return y / 60; + } + + // default raw + int c = RawSamples.AUDIO_FORMAT == AudioFormat.ENCODING_PCM_16BIT ? 2 : 1; + return c * rate; + } +}