2015-11-17 103 views
2

我这里有这个代码,我从如何创建一个可听正弦波的教程有:Java错误生成声音正弦波

import java.nio.ByteBuffer; 
import javax.sound.sampled.*; 


public class FixedFreqSine { 

    //This is just an example - you would want to handle LineUnavailable properly... 
    public static void main(String[] args) throws InterruptedException, LineUnavailableException 
    { 
     final int SAMPLING_RATE = 44100;   // Audio sampling rate 
     final int SAMPLE_SIZE = 2;     // Audio sample size in bytes 

     SourceDataLine line; 
     double fFreq = 440;       // Frequency of sine wave in hz 

     //Position through the sine wave as a percentage (i.e. 0 to 1 is 0 to 2*PI) 
     double fCyclePosition = 0;   

     //Open up audio output, using 44100hz sampling rate, 16 bit samples, mono, and big 
     // endian byte ordering 
     AudioFormat format = new AudioFormat(SAMPLING_RATE, 16, 1, true, true); 
     DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); 

     if (!AudioSystem.isLineSupported(info)){ 
     System.out.println("Line matching " + info + " is not supported."); 
     throw new LineUnavailableException(); 
     } 

     line = (SourceDataLine)AudioSystem.getLine(info); 
     line.open(format); 
     line.start(); 

     // Make our buffer size match audio system's buffer 
     ByteBuffer cBuf = ByteBuffer.allocate(line.getBufferSize()); 

     int ctSamplesTotal = SAMPLING_RATE*5;   // Output for roughly 5 seconds 


     //On each pass main loop fills the available free space in the audio buffer 
     //Main loop creates audio samples for sine wave, runs until we tell the thread to exit 
     //Each sample is spaced 1/SAMPLING_RATE apart in time 
     while (ctSamplesTotal>0) { 
     double fCycleInc = fFreq/SAMPLING_RATE; // Fraction of cycle between samples 

     cBuf.clear();       // Discard samples from previous pass 

      // Figure out how many samples we can add 
     int ctSamplesThisPass = line.available()/SAMPLE_SIZE; 
     for (int i=0; i < ctSamplesThisPass; i++) { 
      cBuf.putShort((short)(Short.MAX_VALUE * Math.sin(2*Math.PI * fCyclePosition))); 

      fCyclePosition += fCycleInc; 
      if (fCyclePosition > 1) 
       fCyclePosition -= 1; 
     } 

     //Write sine samples to the line buffer. If the audio buffer is full, this will 
     // block until there is room (we never write more samples than buffer will hold) 
     line.write(cBuf.array(), 0, cBuf.position());    
     ctSamplesTotal -= ctSamplesThisPass;  // Update total number of samples written 

     //Wait until the buffer is at least half empty before we add more 
     while (line.getBufferSize()/2 < line.available()) 
      Thread.sleep(1);            
     } 


     //Done playing the whole waveform, now wait until the queued samples finish 
     //playing, then clean up and exit 
     line.drain();           
     line.close(); 
    } 
} 

它运行正常在Windows 10,和完美的产生声音。然而,在我的Macbook Pro中,我收到以下错误:

2015-11-17 11:20:16.549 java[5899:9037084] Error loading /Library/Audio/Plug-Ins/HAL/SeratoVirtualAudioPlugIn.plugin/Contents/MacOS/SeratoVirtualAudioPlugIn: dlopen(/Library/Audio/Plug-Ins/HAL/SeratoVirtualAudioPlugIn.plugin/Contents/MacOS/SeratoVirtualAudioPlugIn, 262): no suitable image found. Did find: 
    /Library/Audio/Plug-Ins/HAL/SeratoVirtualAudioPlugIn.plugin/Contents/MacOS/SeratoVirtualAudioPlugIn: mach-o, but wrong architecture 
2015-11-17 11:20:16.549 java[5899:9037084] Cannot find function pointer New_SHP_PlugIn for factory 834FC054-C1CC-11D6-BD01-00039315CD46 in CFBundle/CFPlugIn 0x7fd672c25910 </Library/Audio/Plug-Ins/HAL/SeratoVirtualAudioPlugIn.plugin> (bundle, not loaded) 
2015-11-17 11:20:16.580 java[5899:9037084] 11:20:16.580 WARNING: >compload> 472: Kickstart.component -- file://localhost/Library/Audio/Plug-Ins/Components/: trouble parsing Info.plist's AudioComponents, key (null); entry: <CFBasicHash 0x7fd67521c3f0 [0x7fff73159390]>{type = mutable dict, count = 7, 
entries => 
    2 : <CFString 0x7fff730e39f0 [0x7fff73159390]>{contents = "manufacturer"} = <CFString 0x7fd675231660 [0x7fff73159390]>{contents = "CaNR"} 
    7 : <CFString 0x7fff730e74b0 [0x7fff73159390]>{contents = "factoryFunction"} = <CFString 0x7fd6752319f0 [0x7fff73159390]>{contents = "KickstartAUFactory"} 
    8 : <CFString 0x7fd675230f10 [0x7fff73159390]>{contents = "subtype"} = <CFString 0x7fd675230f50 [0x7fff73159390]>{contents = "CNKS"} 
    9 : <CFString 0x7fff7310b6f0 [0x7fff73159390]>{contents = "description"} = <CFString 0x7fd67520fa20 [0x7fff73159390]>{contents = "Kickstart"} 
    10 : <CFString 0x7fff7310a3d0 [0x7fff73159390]>{contents = "type"} = <CFString 0x7fd675231030 [0x7fff73159390]>{contents = "kAudioUnitType_Effect"} 
    11 : <CFString 0x7fff73095bb0 [0x7fff73159390]>{contents = "name"} = <CFString 0x7fd675231630 [0x7fff73159390]>{contents = "Nicky Romero: Kickstart"} 
    12 : <CFString 0x7fff73114370 [0x7fff73159390]>{contents = "version"} = <CFNumber 0x1000937 [0x7fff73159390]>{value = +65545, type = kCFNumberSInt64Type} 
} 
2015-11-17 11:20:16.586 java[5899:9037084] 11:20:16.586 WARNING: >compload> 472: Primer.component -- file://localhost/Library/Audio/Plug-Ins/Components/: trouble parsing Info.plist's AudioComponents, key (null); entry: <CFBasicHash 0x7fd672c2dc50 [0x7fff73159390]>{type = mutable dict, count = 7, 
entries => 
    2 : <CFString 0x7fff730e39f0 [0x7fff73159390]>{contents = "manufacturer"} = <CFString 0x7fd672c2e720 [0x7fff73159390]>{contents = "AudG"} 
    7 : <CFString 0x7fff730e74b0 [0x7fff73159390]>{contents = "factoryFunction"} = <CFString 0x7fd672c2e8a0 [0x7fff73159390]>{contents = "PrimerAUFactory"} 
    8 : <CFString 0x7fd672c2e9d0 [0x7fff73159390]>{contents = "subtype"} = <CFString 0x7fd672c2ea10 [0x7fff73159390]>{contents = "agp"} 
    9 : <CFString 0x7fff7310b6f0 [0x7fff73159390]>{contents = "description"} = <CFString 0x7fd672c2c4a0 [0x7fff73159390]>{contents = "Primer"} 
    10 : <CFString 0x7fff7310a3d0 [0x7fff73159390]>{contents = "type"} = <CFString 0x7fd672c2ec50 [0x7fff73159390]>{contents = "aumu"} 
    11 : <CFString 0x7fff73095bb0 [0x7fff73159390]>{contents = "name"} = <CFString 0x7fd672c2e900 [0x7fff73159390]>{contents = "Audible Genius: Primer"} 
    12 : <CFString 0x7fff73114370 [0x7fff73159390]>{contents = "version"} = <CFNumber 0x1010137 [0x7fff73159390]>{value = +65793, type = kCFNumberSInt64Type} 
} 

我完全不明白这个错误。我唯一知道的是它正在访问我的音频插件。我也知道错误发生在:

line = (SourceDataLine)AudioSystem.getLine(info); 
     line.open(format); 

有没有人知道这个错误的原因是什么?或者如何解决它?

回答

0

我有一个建议尝试:在上下文中打开默认设置的行并检查格式。例如,如果您硬编码的格式不受支持,则您编写的代码可能会反对。

要使用默认值打开,请省略参数line.open()方法。要获取格式,请使用方法line.getFormat()。您还可以通过在不同的操作系统上播放wav文件来测试该线路的工作情况。如果wavs播放没问题,请在教程代码中使用相同的格式。

我建议这样做的主要原因是,我很惊讶地看到BigEndian被用作格式的一部分。我一直使用LittleEndian

如果默认值是立体声,您应该可以制作和使用与单声道相同的格式,因此您不必修改其余的代码。

但是,我有点问题的其他代码有一些差异。我写的,并使用以下基本形式发送标准化的PCM音频数据到Java桌面:

float[] normalizedOut = float[2]; 
byte[] buffer = new byte[outBufferSize]; 
int i; 

while(playing) 
{ 
    i = 0; 
    while(i < outBufferSize) 
    { 
     // obtain normalized, stereo PCM output from synth 
     normalizedOut = synth.getNextFrame(); 

     // convert normalized to stereo bytes, "CD quality" 
     normalizedOut[0] *= 32767; 
     normalizedOut[1] *= 32767; 
     // this part converts to Little Endian 
     outBuffer[i++] = (byte) normalizedOut[0]; 
     outBuffer[i++] = (byte)((int)normalizedOut[0] >> 8);    
     outBuffer[i++] = (byte) normalizedOut[1]; 
     outBuffer[i++] = (byte)((int)normalizedOut[1] >> 8); 
    } 
    line.write(outBuffer, 0, outBufferSize); 
} 

我认为这是完全没问题,让Java来处理阻塞。除非你的“合成器”算法真的很重,否则系统最终会花费98%(疯狂猜测)阻止这个线程。

上述循环一次一帧。它可能会更好地获得更大的块中的正弦数据,但我喜欢上述的简单性。此外,它似乎表现“足够好”。例如,您可以下载并尝试我编写的一个实现,其中包括上述采用6个FM合成器(每个都采用多个正弦信号作为载波和调制器),并一次播放。

circlesounds.jar demo (使用Java 8.这提醒了我,有些旧版本的Mac OS不支持某些版本的Java,我记得有一台Mac笔记本电脑的朋友无法运行Java 7程序。他来试试看,我没有注意到这个历史。)