Skip to content

Commit c4e009a

Browse files
committed
Issue #1. Fixed bug with audio pitch
1 parent 2f3661e commit c4e009a

4 files changed

Lines changed: 45 additions & 16 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ dependencies {
2828

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

3233
}

app/src/main/java/linc/com/audiotool/MainActivity.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
import androidx.appcompat.app.AppCompatActivity;
88
import androidx.core.app.ActivityCompat;
99

10-
import java.io.File;
11-
import java.util.Arrays;
12-
import java.util.List;
13-
1410
import linc.com.library.AudioTool;
15-
import linc.com.library.callback.OnListComplete;
1611

1712
public class MainActivity extends AppCompatActivity {
1813

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

3025
try {
3126
AudioTool.getInstance(this)
32-
.withAudio(new File("/storage/emulated/0/Music/Linc - AudioTool.mp3"))
33-
/* calls */
27+
.withAudio("/storage/emulated/0/Music/level.mp3")
28+
// .changeAudioPitch(44100, -0.86883157f, -0.8699582800000001f, null)
29+
// .changeAudioPitch(44100, -2.86883157f, null)
30+
// .changeAudioPitch(44100, -2.86883157f, Pitch.DOWN, null)
31+
.saveCurrentTo("/storage/emulated/0/Music/level_pitch.mp3")
3432
.release();
3533
} catch (Exception e) {
3634
e.printStackTrace();

library/src/main/java/linc/com/library/AudioTool.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import linc.com.library.callback.OnListComplete;
2424
import linc.com.library.types.Duration;
2525
import linc.com.library.types.Echo;
26+
import linc.com.library.types.Pitch;
2627

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

207208
/**
208209
* @param sampleRate audio sample rate
209-
* @param pitch audio pitch value
210-
* @param tempo audio speed value
210+
* @param deltaRate audio custom rate value
211+
* @param deltaTempo audio custom tempo value
211212
* @param onCompleteCallback lambda with result audio
212213
*/
213-
public AudioTool changeAudioPitch(int sampleRate, float pitch, float tempo, @Nullable OnFileComplete onCompleteCallback) throws IOException {
214-
tempo = Limiter.limit(0.5f, 2, tempo);
214+
public AudioTool changeAudioPitch(int sampleRate, float deltaRate, float deltaTempo, @Nullable OnFileComplete onCompleteCallback) throws IOException {
215+
deltaTempo = Limiter.limit(-12f, 12f, deltaTempo);
216+
deltaRate = Limiter.limit(-12f, 12f, deltaRate);
217+
215218
String command = String.format(Locale.US,
216-
"-y -i %s -filter_complex asetrate=%d*%f^(-10/12),atempo=%f^(-10/12)",
217-
audio.getPath(), sampleRate, pitch, tempo
219+
"-y -i %s -filter_complex asetrate=%d*2^(%f/12),atempo=1/2^(%f/12)",
220+
audio.getPath(), sampleRate, deltaRate, deltaTempo
218221
);
219222
FFmpegExecutor.executeCommandWithBuffer(command, audio);
220223
onResultFile(onCompleteCallback);
221224
return this;
222225
}
223226

224227
/**
225-
* @param bass audio bass
226-
* @param width audio bass width
227-
* @param frequency audio frequency
228+
* @param sampleRate audio sample rate
229+
* @param pitch audio semitone value
230+
* @param onCompleteCallback lambda with result audio
231+
*/
232+
public AudioTool changeAudioPitch(int sampleRate, float pitch, @Nullable OnFileComplete onCompleteCallback) throws IOException {
233+
changeAudioPitch(sampleRate, pitch, pitch, onCompleteCallback);
234+
return this;
235+
}
236+
237+
/**
238+
* @param sampleRate audio sample rate
239+
* @param pitchValue audio semitone value
240+
* @param pitch pitch type: UP or DOWN
228241
* @param onCompleteCallback lambda with result audio
229242
*/
243+
public AudioTool changeAudioPitch(int sampleRate, float pitchValue, Pitch pitch, @Nullable OnFileComplete onCompleteCallback) throws IOException {
244+
pitchValue = pitch == Pitch.UP ? Math.abs(pitchValue) : -Math.abs(pitchValue);
245+
changeAudioPitch(sampleRate, pitchValue, pitchValue, onCompleteCallback);
246+
return this;
247+
}
248+
249+
/**
250+
* @param bass audio bass
251+
* @param width audio bass width
252+
* @param frequency audio frequency
253+
* @param onCompleteCallback lambda with result audio
254+
*/
230255
public AudioTool changeAudioBass(float bass, float width, int frequency, @Nullable OnFileComplete onCompleteCallback) throws IOException {
231256
bass = Limiter.limit(-20f, 20f, bass);
232257
width = Limiter.limit(0f, 1f, width);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package linc.com.library.types;
2+
3+
public enum Pitch {
4+
UP, DOWN
5+
}

0 commit comments

Comments
 (0)