Skip to content

Commit 5c2a5a9

Browse files
committed
perf: 优化 autoToPcm 方法增加 silkToPcm 的支持
1 parent 5c72620 commit 5c2a5a9

5 files changed

Lines changed: 21 additions & 37 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ long duration = codec.getDuration("/sdcard/somefile");
6767
| -3 | PCM 转 Silk 需要额外参数 |
6868
| -4 | 输入已经是 PCM 格式 |
6969
| -5 | 输入已经是 Silk 格式 |
70-
| -6 | Silk 转 PCM 请使用 silkToMp3 |
7170
| -10 | 输出必须是 .silk 或 .slk |
7271
| -11 | 输出必须是 .mp3 |
7372
| -12 | 输出必须是 .pcm 或 .raw |

app/src/main/java/me/yun/silk/utils/Conversion.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private static String getFileTypeName(int type) {
7171
case 5: return "OGG";
7272
case 6: return "PCM";
7373
case 7: return "M4A";
74-
case 8: return "AAC";
74+
case 8: return "MP4";
7575
default: return "未知";
7676
}
7777
}
@@ -84,7 +84,6 @@ private static String getErrorMsg(int code) {
8484
case -3: return "错误码:-3 → PCM 转 Silk 需要额外参数";
8585
case -4: return "错误码:-4 → 输入已经是 PCM 格式";
8686
case -5: return "错误码:-5 → 输入已经是 Silk 格式";
87-
case -6: return "错误码:-6 → Silk 转 PCM 请使用 silkToMp3";
8887
case -10: return "错误码:-10 → 输出必须是 .silk 或 .slk";
8988
case -11: return "错误码:-11 → 输出必须是 .mp3";
9089
case -12: return "错误码:-12 → 输出必须是 .pcm 或 .raw";

silk-codec/src/main/java/me/yun/silk/AacCodec.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import android.media.MediaExtractor;
66
import android.media.MediaFormat;
77
import android.media.MediaMuxer;
8+
import android.media.MediaMetadataRetriever;
89

910
import java.io.File;
1011
import java.io.FileOutputStream;
1112
import java.io.RandomAccessFile;
13+
import java.nio.ByteBuffer;
1214

1315
public class AacCodec {
1416

@@ -98,8 +100,8 @@ public static int decodeAacFile(String aacPath, String pcmPath, AacCallback call
98100
codec.start();
99101

100102
FileOutputStream fos = new FileOutputStream(pcmPath);
101-
java.nio.ByteBuffer[] inputBuffers = codec.getInputBuffers();
102-
java.nio.ByteBuffer[] outputBuffers = codec.getOutputBuffers();
103+
ByteBuffer[] inputBuffers = codec.getInputBuffers();
104+
ByteBuffer[] outputBuffers = codec.getOutputBuffers();
103105

104106
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
105107
boolean sawInputEOS = false;
@@ -109,7 +111,7 @@ public static int decodeAacFile(String aacPath, String pcmPath, AacCallback call
109111
if (!sawInputEOS) {
110112
int inputBufferIndex = codec.dequeueInputBuffer(TIMEOUT_US);
111113
if (inputBufferIndex >= 0) {
112-
java.nio.ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
114+
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
113115
int sampleSize = extractor.readSampleData(inputBuffer, 0);
114116
if (sampleSize < 0) {
115117
codec.queueInputBuffer(inputBufferIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
@@ -125,7 +127,7 @@ public static int decodeAacFile(String aacPath, String pcmPath, AacCallback call
125127
if (outputBufferIndex >= 0) {
126128
if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) sawOutputEOS = true;
127129
if (bufferInfo.size > 0) {
128-
java.nio.ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
130+
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
129131
outputBuffer.position(bufferInfo.offset);
130132
outputBuffer.limit(bufferInfo.offset + bufferInfo.size);
131133

@@ -186,8 +188,8 @@ public static int encodePcmToAac(String pcmPath, String aacPath, int sampleRate,
186188

187189
RandomAccessFile raf = new RandomAccessFile(pcmPath, "r");
188190
FileOutputStream fos = new FileOutputStream(aacPath);
189-
java.nio.ByteBuffer[] inputBuffers = codec.getInputBuffers();
190-
java.nio.ByteBuffer[] outputBuffers = codec.getOutputBuffers();
191+
ByteBuffer[] inputBuffers = codec.getInputBuffers();
192+
ByteBuffer[] outputBuffers = codec.getOutputBuffers();
191193

192194
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
193195
boolean sawInputEOS = false;
@@ -199,7 +201,7 @@ public static int encodePcmToAac(String pcmPath, String aacPath, int sampleRate,
199201
if (!sawInputEOS) {
200202
int inputBufferIndex = codec.dequeueInputBuffer(TIMEOUT_US);
201203
if (inputBufferIndex >= 0) {
202-
java.nio.ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
204+
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
203205
inputBuffer.clear();
204206

205207
byte[] pcmChunk = new byte[Math.min(4096, (int)(fileSize - raf.getFilePointer()))];
@@ -227,7 +229,7 @@ public static int encodePcmToAac(String pcmPath, String aacPath, int sampleRate,
227229
if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) sawOutputEOS = true;
228230

229231
if (bufferInfo.size > 0) {
230-
java.nio.ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
232+
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
231233
byte[] aacFrame = new byte[bufferInfo.size];
232234
outputBuffer.get(aacFrame);
233235

@@ -283,8 +285,8 @@ public static int encodePcmToM4a(String pcmPath, String m4aPath, int sampleRate,
283285
boolean muxerStarted = false;
284286

285287
RandomAccessFile raf = new RandomAccessFile(pcmPath, "r");
286-
java.nio.ByteBuffer[] inputBuffers = codec.getInputBuffers();
287-
java.nio.ByteBuffer[] outputBuffers = codec.getOutputBuffers();
288+
ByteBuffer[] inputBuffers = codec.getInputBuffers();
289+
ByteBuffer[] outputBuffers = codec.getOutputBuffers();
288290

289291
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
290292
boolean sawInputEOS = false;
@@ -297,7 +299,7 @@ public static int encodePcmToM4a(String pcmPath, String m4aPath, int sampleRate,
297299
if (!sawInputEOS) {
298300
int inputBufferIndex = codec.dequeueInputBuffer(TIMEOUT_US);
299301
if (inputBufferIndex >= 0) {
300-
java.nio.ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
302+
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
301303
inputBuffer.clear();
302304

303305
byte[] pcmChunk = new byte[Math.min(frameSize, (int)(fileSize - raf.getFilePointer()))];
@@ -331,7 +333,7 @@ public static int encodePcmToM4a(String pcmPath, String m4aPath, int sampleRate,
331333
muxerStarted = true;
332334
}
333335

334-
java.nio.ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
336+
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
335337
muxer.writeSampleData(trackIndex, outputBuffer, bufferInfo);
336338
}
337339

@@ -597,12 +599,12 @@ public static int decodeM4aFile(String m4aPath, String pcmPath, AacCallback call
597599
}
598600

599601
public static long getDuration(String filePath) {
600-
android.media.MediaMetadataRetriever retriever = null;
602+
MediaMetadataRetriever retriever = null;
601603
try {
602-
retriever = new android.media.MediaMetadataRetriever();
604+
retriever = new MediaMetadataRetriever();
603605
retriever.setDataSource(filePath);
604606
String durationStr = retriever.extractMetadata(
605-
android.media.MediaMetadataRetriever.METADATA_KEY_DURATION);
607+
MediaMetadataRetriever.METADATA_KEY_DURATION);
606608
if (durationStr != null) {
607609
return Long.parseLong(durationStr);
608610
}

silk-codec/src/main/java/me/yun/silk/SilkCodec.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public class SilkCodec {
1717
*/
1818
public native int getFileType(String filePath);
1919

20-
// ==================== 转 Silk ====================
21-
2220
/**
2321
* MP3 转 Silk
2422
*
@@ -72,7 +70,7 @@ public class SilkCodec {
7270
public native int pcmToSilk(String pcmPath, String silkPath, int hz, int pcmHz, int channels);
7371

7472
/**
75-
* 自动识别音频格式并转 Silk 支持格式: MP3, WAV, FLAC, OGG, M4A, MP4
73+
* 自动识别音频格式并转 Silk
7674
*
7775
* @param audioPath 输入音频文件路径
7876
* @param silkPath 输出 Silk 文件路径
@@ -81,8 +79,6 @@ public class SilkCodec {
8179
*/
8280
public native int autoToSilk(String audioPath, String silkPath, int hz);
8381

84-
// ==================== Silk 转 MP3 ====================
85-
8682
/**
8783
* Silk 转 MP3
8884
*
@@ -93,7 +89,6 @@ public class SilkCodec {
9389
*/
9490
public native int silkToMp3(String silkPath, String mp3Path, int hz);
9591

96-
// ==================== 转 PCM ====================
9792

9893
/**
9994
* Silk 转 PCM
@@ -142,7 +137,7 @@ public class SilkCodec {
142137
public native int oggToPcm(String oggPath, String pcmPath);
143138

144139
/**
145-
* 自动识别音频格式并转 PCM 支持格式: MP3, WAV, FLAC, OGG, M4A, AAC, AMR
140+
* 自动识别音频格式并转 PCM
146141
*
147142
* @param audioPath 输入音频文件路径
148143
* @param pcmPath 输出 PCM 文件路径

silk-codec/src/main/jni/silk_codec.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Silk Codec JNI 接口
33
*
44
* 功能:多种音频格式与 Silk 格式互转
5-
* 支持:MP3/WAV/FLAC/OGG <-> Silk
65
*
76
* 注意:输出的 Silk 文件包含微信专属头 (0x02 + #!SILK_V3)
87
*/
@@ -251,16 +250,6 @@ static int isMp4Brand(const unsigned char *brand) {
251250
return 0;
252251
}
253252

254-
/**
255-
* detectMp4Type - 检测 MP4/M4A 容器类型
256-
*
257-
* 读取 MP4 文件的 ftyp box,检查品牌标识确定类型
258-
*
259-
* @param path 文件路径
260-
* @return 1=M4A, 2=MP4, 0=未知
261-
*/
262-
263-
264253
/**
265254
* detectFileType - 通过文件头检测文件实际类型
266255
*
@@ -1629,7 +1618,7 @@ JNIEXPORT jint JNICALL Java_me_yun_silk_SilkCodec_autoToPcm(
16291618

16301619
switch (inputType) {
16311620
case FILE_TYPE_SILK:
1632-
result = -6;
1621+
result = Java_me_yun_silk_SilkCodec_silkToPcm(env, thiz, audioPath, pcmPath, 24000);
16331622
break;
16341623
case FILE_TYPE_MP3:
16351624
result = Java_me_yun_silk_SilkCodec_mp3ToPcm(env, thiz, audioPath, pcmPath);

0 commit comments

Comments
 (0)