【发布时间】:2011-09-10 20:22:10
【问题描述】:
我想通过音频端口进行一些 FSK 调制。所以问题是我的窦波不是很好。它受到偶数部分的干扰。我使用了 http://marblemice.blogspot.com/2010/04/generate-and-play-tone-in-android.html 的原始代码以及 Playing an arbitrary tone with Android 和 https://market.android.com/details?id=re.serialout&feature=search_result 的进一步修改。 那么失败在哪里呢?我哪里错了?
private static int bitRate=300;
private static int sampleRate=48000;
private static int freq1=600;
public static void loopOnes(){
playque.add(UARTHigh);
athread.interrupt();
}
private static byte[] UARTHigh() {
int numSamples=sampleRate/bitRate;
double sample[]=new double[numSamples];
byte[] buffer=new byte[numSamples*2];
for(int i=0; i<numSamples;++i){
sample[i]=Math.sin(2*Math.PI*i*freq1/sampleRate);
}
int idx = 0;
for (final double dVal : sample) {
// scale to maximum amplitude
final short val = (short) ((dVal * 32767));
// in 16 bit wav PCM, first byte is the low order byte
buffer[idx++] = (byte) (val & 0x00ff);
buffer[idx++] = (byte) ((val & 0xff00) >>> 8);
}
return buffer;
}
private static void playSound(){
active = true;
while(active)
{
try {Thread.sleep(Long.MAX_VALUE);} catch (InterruptedException e) {
while (playque.isEmpty() == false)
{
if (atrk != null)
{
if (generatedSnd != null)
{
// Das letzte Sample erst fertig abspielen lassen
// systemClock.sleep(xx) xx könnte angepasst werden
while (atrk.getPlaybackHeadPosition() < (generatedSnd.length))
SystemClock.sleep(50); // let existing sample finish first: this can probably be set to a smarter number using the information above
}
atrk.release();
}
UpdateParameters(); // might as well do it at every iteration, it's cheap
generatedSnd = playque.poll();
length = generatedSnd.length;
if (minbufsize<length)
minbufsize=length;
atrk = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, minbufsize,
AudioTrack.MODE_STATIC);
atrk.setStereoVolume(1,1);
atrk.write(generatedSnd, 0, length);
atrk.play();
}
// Playque is Empty =>send StopBit!
// Set Loop Points
int setLoopError=atrk.setLoopPoints(0, length, -1);
atrk.play();
}
}
}
}
【问题讨论】:
-
我发现了失败。 setLoopPoints(0,length/2,-1) 但我不知道为什么这是正确的。?
-
尚未查看您的代码,但我认为当您说“正弦波”时,您的意思是“正弦波”。一开始我还以为是你鼻子的问题。 :-)
-
“被偶数部分干扰”是什么意思?
-
哦,是的,我的意思是正弦波。我已经找到了解决这个问题的方法。我的意思是“被偶数部分干扰”,如果我在一个范围内观察输出,就会有一个正弦波、一条线,然后是一个正弦波。问题是我使用了 MODE_STATIC 并且为了加载新数据,输出转到一行。
-
@Ingo 我不知道你是否可以更改标题。尝试问题底部的“编辑”链接。此外,最好用解决方案“回答”您自己的问题,然后接受答案。