在我的Android应用程序中,我想更改音频文件的音高并将其保存到SD卡。
我正在使用音速ndk link来更改音频文件的音高,但无法保存更改后的音频文件。
有什么方法可以保存此更改的音调音频文件?提前致谢
以下是我用于音频修改和保存的代码。
public class SonicTest extends Activity implements View.OnClickListener {
private Button saveBtn;
private int bufferSize=0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
saveBtn = findViewById(R.id.button1);
saveBtn.setOnClickListener(this);
}
public void play(View view)
{
new Thread(new Runnable()
{
public void run()
{
final EditText speedEdit = findViewById(R.id.speed);
final EditText pitchEdit = findViewById(R.id.pitch);
final EditText rateEdit = findViewById(R.id.rate);
float speed = Float.parseFloat(speedEdit.getText().toString());
float pitch = Float.parseFloat(pitchEdit.getText().toString());
float rate = Float.parseFloat(rateEdit.getText().toString());
AndroidAudioDevice device = new AndroidAudioDevice(22050, 1);
Sonic sonic = new Sonic(22050, 1);
byte samples[] = new byte[4096];
byte modifiedSamples[] = new byte[2048];
InputStream soundFile = getResources().openRawResource(R.raw.talking);
// InputStream soundFile = getResources().openRawResource(R.raw.sample);
int bytesRead;
if(soundFile != null) {
sonic.setSpeed(speed);
sonic.setPitch(pitch);
sonic.setRate(rate);
do {
try {
bytesRead = soundFile.read(samples, 0, samples.length);
} catch (IOException e) {
e.printStackTrace();
return;
}
if(bytesRead > 0) {
sonic.putBytes(samples, bytesRead);
} else {
sonic.flush();
}
int available = sonic.availableBytes();
if(available > 0) {
if(modifiedSamples.length < available) {
modifiedSamples = new byte[available*2];
}
sonic.receiveBytes(modifiedSamples, available);
device.writeSamples(modifiedSamples, available);
}
} while(bytesRead > 0);
device.flush();
}
}
} ).start();
}
@Override
public void onClick(View v) {
LinkedList<byte[]>linkedList = AndroidAudioDevice.list;
for (int i=0; i<linkedList.size(); i++){
bufferSize = bufferSize+linkedList.get(i).length;
}
byte[]buffer = new byte[bufferSize];
int k=0;
for (int i=0 ; i< linkedList.size(); i++){
for(int j=0; j<linkedList.get(i).length; j++){
buffer[k]=linkedList.get(i)[j];
k++;
}
Log.v("Buffer array length",""+k);
}
File file = new File(Environment.getExternalStorageDirectory(),"demo.mp3");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(buffer);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
软件包org.vinuxproject.sonic;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Environment;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
public class AndroidAudioDevice
{
AudioTrack track;
static int bufferLength;
public static LinkedList<byte[]>list= new LinkedList<>();
private int findFormatFromChannels(int numChannels)
{
switch(numChannels) {
case 1: return AudioFormat.CHANNEL_OUT_MONO;
case 2: return AudioFormat.CHANNEL_OUT_STEREO;
default: return -1; // Error
}
}
public AndroidAudioDevice(int sampleRate, int numChannels)
{
int format = findFormatFromChannels(numChannels);
int minSize = AudioTrack.getMinBufferSize(sampleRate, format, AudioFormat.ENCODING_PCM_16BIT);
track = new AudioTrack( AudioManager.STREAM_MUSIC, sampleRate,
format, AudioFormat.ENCODING_PCM_16BIT,
minSize*4, AudioTrack.MODE_STREAM);
track.play();
}
public void flush()
{
track.flush();
}
public void writeSamples(byte[] samples, int length)
{
bufferLength = bufferLength+length;
track.write( samples, 0, length);
list.add(samples);
}
}