Skip to content

Commit

Permalink
1.1-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mzdluo123 committed Feb 26, 2021
1 parent 4b4b37f commit 421fbba
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'io.github.mzdluo123'
version '1.0'
version '1.1-dev'

repositories {
mavenCentral()
Expand Down
32 changes: 23 additions & 9 deletions src/main/java/io/github/mzdluo123/silk4j/AudioUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/io/github/mzdluo123/silk4j/SilkCoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}

}

0 comments on commit 421fbba

Please sign in to comment.