diff --git a/build.gradle b/build.gradle index b9cd0e5..1307a0a 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'io.github.mzdluo123' -version '1.0' +version '1.1-dev' repositories { mavenCentral() diff --git a/src/main/java/io/github/mzdluo123/silk4j/AudioUtils.java b/src/main/java/io/github/mzdluo123/silk4j/AudioUtils.java index 3ae290c..fed2e66 100644 --- a/src/main/java/io/github/mzdluo123/silk4j/AudioUtils.java +++ b/src/main/java/io/github/mzdluo123/silk4j/AudioUtils.java @@ -20,43 +20,57 @@ public static void init(File tmpDir) throws IOException { NativeLibLoader.load(); } - public static File mp3ToSilk(File mp3File) throws IOException { + public static File mp3ToSilk(File mp3File, int bitRate) throws IOException { if (!mp3File.exists() || mp3File.length() == 0) { throw new IOException("文件不存在或为空"); } File pcmFile = getTempFile("pcm"); File silkFile = getTempFile("silk"); - int bitrate = LameCoder.decode(mp3File.getAbsolutePath(), pcmFile.getAbsolutePath()); - SilkCoder.encode(pcmFile.getAbsolutePath(), silkFile.getAbsolutePath(), bitrate); + int sampleRate = LameCoder.decode(mp3File.getAbsolutePath(), pcmFile.getAbsolutePath()); + SilkCoder.encode(pcmFile.getAbsolutePath(), silkFile.getAbsolutePath(), sampleRate, bitRate); pcmFile.delete(); return silkFile; } - public static File mp3ToSilk(InputStream mp3FileStream) throws IOException { + public static File mp3ToSilk(File mp3File) throws IOException { + return mp3ToSilk(mp3File,24000); + } + public static File mp3ToSilk(InputStream mp3FileStream, int bitRate) throws IOException { File mp3File = getTempFile("mp3"); streamToTempFile(mp3FileStream, mp3File); - return mp3ToSilk(mp3File); + return mp3ToSilk(mp3File, bitRate); } - public static File silkToMp3(File silkFile) throws IOException { + public static File mp3ToSilk(InputStream mp3FileStream) throws IOException { + return mp3ToSilk(mp3FileStream,24000); + } + + public static File silkToMp3(File silkFile, int bitrate) throws IOException { if (!silkFile.exists() || silkFile.length() == 0) { throw new IOException("文件不存在或为空"); } File pcmFile = getTempFile("pcm"); File mp3File = getTempFile("mp3"); SilkCoder.decode(silkFile.getAbsolutePath(), pcmFile.getAbsolutePath()); - LameCoder.encode(pcmFile.getAbsolutePath(), mp3File.getAbsolutePath(), 24000); + LameCoder.encode(pcmFile.getAbsolutePath(), mp3File.getAbsolutePath(), bitrate); pcmFile.delete(); return mp3File; } - public static File silkToMp3(InputStream silkFileStream) throws IOException { + public static File silkToMp3(File silkFile) throws IOException { + return silkToMp3(silkFile, 24000); + } + + public static File silkToMp3(InputStream silkFileStream, int bitrate) throws IOException { File mp3File = getTempFile("silk"); streamToTempFile(silkFileStream, mp3File); - return silkToMp3(mp3File); + return silkToMp3(mp3File, bitrate); } + public static File silkToMp3(InputStream silkFileStream) throws IOException { + return silkToMp3(silkFileStream, 24000); + } static void streamToTempFile(InputStream inputStream, File tmpFile) throws IOException { FileOutputStream fileOutputStream = new FileOutputStream(tmpFile); diff --git a/src/main/java/io/github/mzdluo123/silk4j/SilkCoder.java b/src/main/java/io/github/mzdluo123/silk4j/SilkCoder.java index 0d1916a..f6899bf 100644 --- a/src/main/java/io/github/mzdluo123/silk4j/SilkCoder.java +++ b/src/main/java/io/github/mzdluo123/silk4j/SilkCoder.java @@ -23,7 +23,9 @@ public static native void encode(String source, int bitRate); public static void encode(String source, - String dest, int sampleRate + String dest, + int sampleRate, + int bitRate ) { encode(source, dest, true, @@ -34,7 +36,14 @@ public static void encode(String source, 0, 0, 2, - 16000); + bitRate); + } + + public static void encode(String source, + String dest, + int sampleRate + ){ + encode(source, dest, sampleRate, 24000); } }