Skip to content

Commit

Permalink
Issue #1. Fixed bug with audio pitch
Browse files Browse the repository at this point in the history
  • Loading branch information
lincollincol committed Oct 25, 2020
1 parent 2f3661e commit c4e009a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
// implementation 'com.github.lincollincol:AudioTool:1.0'

}
12 changes: 5 additions & 7 deletions app/src/main/java/linc/com/audiotool/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import linc.com.library.AudioTool;
import linc.com.library.callback.OnListComplete;

public class MainActivity extends AppCompatActivity {

Expand All @@ -29,8 +24,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

try {
AudioTool.getInstance(this)
.withAudio(new File("/storage/emulated/0/Music/Linc - AudioTool.mp3"))
/* calls */
.withAudio("/storage/emulated/0/Music/level.mp3")
// .changeAudioPitch(44100, -0.86883157f, -0.8699582800000001f, null)
// .changeAudioPitch(44100, -2.86883157f, null)
// .changeAudioPitch(44100, -2.86883157f, Pitch.DOWN, null)
.saveCurrentTo("/storage/emulated/0/Music/level_pitch.mp3")
.release();
} catch (Exception e) {
e.printStackTrace();
Expand Down
43 changes: 34 additions & 9 deletions library/src/main/java/linc/com/library/AudioTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import linc.com.library.callback.OnListComplete;
import linc.com.library.types.Duration;
import linc.com.library.types.Echo;
import linc.com.library.types.Pitch;

import static linc.com.library.Constant.AUDIO_TOOL_LOCAL_EFFECT_REVERB;
import static linc.com.library.Constant.AUDIO_TOOL_LOCAL_JOIN_FILE;
Expand Down Expand Up @@ -206,27 +207,51 @@ public AudioTool changeAudioSpeed(float xSpeed, @Nullable OnFileComplete onCompl

/**
* @param sampleRate audio sample rate
* @param pitch audio pitch value
* @param tempo audio speed value
* @param deltaRate audio custom rate value
* @param deltaTempo audio custom tempo value
* @param onCompleteCallback lambda with result audio
*/
public AudioTool changeAudioPitch(int sampleRate, float pitch, float tempo, @Nullable OnFileComplete onCompleteCallback) throws IOException {
tempo = Limiter.limit(0.5f, 2, tempo);
public AudioTool changeAudioPitch(int sampleRate, float deltaRate, float deltaTempo, @Nullable OnFileComplete onCompleteCallback) throws IOException {
deltaTempo = Limiter.limit(-12f, 12f, deltaTempo);
deltaRate = Limiter.limit(-12f, 12f, deltaRate);

String command = String.format(Locale.US,
"-y -i %s -filter_complex asetrate=%d*%f^(-10/12),atempo=%f^(-10/12)",
audio.getPath(), sampleRate, pitch, tempo
"-y -i %s -filter_complex asetrate=%d*2^(%f/12),atempo=1/2^(%f/12)",
audio.getPath(), sampleRate, deltaRate, deltaTempo
);
FFmpegExecutor.executeCommandWithBuffer(command, audio);
onResultFile(onCompleteCallback);
return this;
}

/**
* @param bass audio bass
* @param width audio bass width
* @param frequency audio frequency
* @param sampleRate audio sample rate
* @param pitch audio semitone value
* @param onCompleteCallback lambda with result audio
*/
public AudioTool changeAudioPitch(int sampleRate, float pitch, @Nullable OnFileComplete onCompleteCallback) throws IOException {
changeAudioPitch(sampleRate, pitch, pitch, onCompleteCallback);
return this;
}

/**
* @param sampleRate audio sample rate
* @param pitchValue audio semitone value
* @param pitch pitch type: UP or DOWN
* @param onCompleteCallback lambda with result audio
*/
public AudioTool changeAudioPitch(int sampleRate, float pitchValue, Pitch pitch, @Nullable OnFileComplete onCompleteCallback) throws IOException {
pitchValue = pitch == Pitch.UP ? Math.abs(pitchValue) : -Math.abs(pitchValue);
changeAudioPitch(sampleRate, pitchValue, pitchValue, onCompleteCallback);
return this;
}

/**
* @param bass audio bass
* @param width audio bass width
* @param frequency audio frequency
* @param onCompleteCallback lambda with result audio
*/
public AudioTool changeAudioBass(float bass, float width, int frequency, @Nullable OnFileComplete onCompleteCallback) throws IOException {
bass = Limiter.limit(-20f, 20f, bass);
width = Limiter.limit(0f, 1f, width);
Expand Down
5 changes: 5 additions & 0 deletions library/src/main/java/linc/com/library/types/Pitch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package linc.com.library.types;

public enum Pitch {
UP, DOWN
}

0 comments on commit c4e009a

Please sign in to comment.