77import android .text .TextUtils ;
88import android .util .Log ;
99
10+ import com .iot .soundtouch .interfaces .SoundTouch ;
1011import com .tencent .iot .thirdparty .flv .FLVListener ;
1112import com .tencent .iot .thirdparty .flv .FLVPacker ;
1213import com .tencent .xnet .XP2P ;
@@ -35,12 +36,15 @@ public class AudioRecordUtil implements EncoderListener, FLVListener {
3536 private int channel ;
3637 private int bitDepth ;
3738 private int channelCount ; //声道数
39+ private int pitch ; //变调【-12~12】
3840
3941 private boolean isRecord = false ;
4042 private final ExecutorService executor = Executors .newSingleThreadExecutor ();
4143 private String speakFlvFilePath = "/storage/emulated/0/speak.flv" ;
4244 private FileOutputStream fos ;
4345
46+ private SoundTouch st ;
47+
4448 public AudioRecordUtil (Context ctx , String id , int sampleRate ) {
4549 context = ctx ;
4650 deviceId = id ;
@@ -51,6 +55,16 @@ public AudioRecordUtil(Context ctx, String id, int sampleRate, int channel, int
5155 deviceId = id ;
5256 init (sampleRate , channel , bitDepth );
5357 }
58+ public AudioRecordUtil (Context ctx , String id , int sampleRate , int channel , int bitDepth , int pitch ) {
59+ context = ctx ;
60+ deviceId = id ;
61+ this .pitch = pitch ;
62+ init (sampleRate , channel , bitDepth );
63+
64+ if (st == null ) {
65+ st = new SoundTouch (0 ,channelCount ,sampleRate ,bitDepth ,1.0f , pitch );
66+ }
67+ }
5468
5569 private void init (int sampleRate , int channel , int bitDepth ) {
5670 recordMinBufferSize = AudioRecord .getMinBufferSize (sampleRate , channel , bitDepth );
@@ -111,6 +125,13 @@ public void stop() {
111125 if (audioRecord != null ) {
112126 audioRecord .stop ();
113127 }
128+
129+ if (st != null ) {
130+ st .finish ();
131+ st .clearBuffer (0 );
132+ st = null ;
133+ }
134+
114135 executor .shutdown ();
115136 audioRecord = null ;
116137 pcmEncoder = null ;
@@ -152,6 +173,10 @@ private class RecordThread extends Thread {
152173 public void run () {
153174 while (recorderState ) {
154175 int read = audioRecord .read (buffer , 0 , buffer .length );
176+ if (st != null ) {
177+ st .putBytes (buffer );
178+ int bytesReceived = st .getBytes (buffer );
179+ }
155180 if (AudioRecord .ERROR_INVALID_OPERATION != read ) {
156181 //获取到的pcm数据就是buffer了
157182 pcmEncoder .encodeData (buffer );
0 commit comments