2013-08-27 5 views
2

주파수 톤을 생성 할 때 Playing an arbitrary tone with Android이 도움이된다는 것을 알았습니다. 이제 음색이 재생되는 동안 주파수가 변경되기를 바랍니다. 자바 (안드로이드)에서 Sine Sweep을 생성하는 방법

나는 다음과 유사 할 수 genTone 수정 :

private void genTone(double startFreq, double endFreq, int dur) { 
    int numSamples = dur * sampleRate; 
    sample = new double[numSamples]; 
    double currentFreq = 0, numerator; 
    for (int i = 0; i < numSamples; ++i) { 
     numerator = (double) i/(double) numSamples; 
     currentFreq = startFreq + (numerator * (endFreq - startFreq)); 
     if ((i % 1000) == 0) { 
      Log.e("Current Freq:", String.format("Freq is: %f at loop %d of %d", currentFreq, i, numSamples)); 
     } 
     sample[i] = Math.sin(2 * Math.PI * i/(sampleRate/currentFreq)); 
    } 
    convertToPCM(numSamples); 
} 

private void convertToPCM(int numSamples) { 
    // convert to 16 bit pcm sound array 
    // assumes the sample buffer is normalised. 
    int idx = 0; 
    generatedSnd = new byte[2 * numSamples]; 
    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 
     generatedSnd[idx++] = (byte) (val & 0x00ff); 
     generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8); 

    } 
} 

로그는 currentFreq에 대한 올바른 값으로 나타납니다 보여줍니다 톤을들을 때, 그러나 스윕이 너무, 너무 빨리 간다 . 예를 들어 400hz에서 800hz로 이동하면 오실로스코프는 실제로 400hz에서 1200z로 동시에 이동한다는 것을 보여줍니다.

내가 뭘 잘못하고 있는지 잘 모르겠지만 아무도 도와 줄 수 있습니까?

답변

2

샘플러를 변경하면 오실로스코프로 측정 한 주파수에서 어떤 효과가 있습니까? 가능한 한 샘플러를 더 높은 값으로 늘리려고합니다. 왜냐하면 샘플러가 높을수록 생성 된 신호가 정확해질 수 있기 때문입니다. 그 경우 does't 도움

어쨌든로부터 화학식 조정할주세요

currentFreq startFreq = + (분자 * (endFreq -를 startFreq));

행 :

currentFreq startFreq = + (분자 * (endFreq - startFreq)) /2;

이제 신호의 새로운 측정 간격을 알려주십시오.

행운을 빈다.

+0

이미 sampleRate가 44100입니다. 좋은 소식은 간단한/2가 트릭을 수행 한 것으로 보입니다. 그것은 내 귀에 좋게 들립니다. 검증을 위해 테스트 그룹에 보내 겠지만, 그렇게했다고 생각합니다! – Innova

+0

오실로스코프가이를 확인했습니다. 감사합니다! – Innova