0

我正在尝试学习使用 Java 从头开始​​创建合成器我尝试将一些教程和代码串在一起以尝试从基本的工作产品中学习,但我没有得到任何声音。我只想能够使用正弦波、锯齿波和方波产生声音。当我运行程序时,我没有声音。我认为我的问题出在 Main.java 类和 SamplePlayer.java 类中。我将在最后发布的 SamplePlayer.java 类中遇到错误。有谁知道我做错了什么或者需要改变什么才能创造出基本的声音?

我有 6 节课:

  • Envelope.java(设置 ADSR 包络生成器)

  • Main.java(包含主函数)

    public class Main
    {
        public static void main(String[] args) 
        {
        // Create an oscillator sample producer
        Oscillator osc = new Oscillator();
    
        // Set the frequency
        osc.setFrequency(500);
    
        // Set the waveashape
        //osc.setWaveshape(WAVESHAPE.SIN);
    
        // Create a sample player
        SamplePlayer player = new SamplePlayer();
    
        // Sets the player's sample provider
        player.setSampleProvider(osc);
    
        // Start the player
        player.startPlayer();
    
        // Delay so oscillator can be heard
        try
        {
            TimeUnit.SECONDS.sleep(4);  
        }
        catch(InterruptedException e)
        {
    
        }
    
        // Stop the player
        player.stopPlayer();
    }
    
    }
    
  • SamplePlayer.java(负责采样波形和播放声音)

    import java.nio.ByteBuffer;
    
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.SourceDataLine;
    
    public class SamplePlayer extends Thread 
    {
    
        // AudioFormat parameters
        public  static final int     SAMPLE_RATE = 22050;
        private static final int     SAMPLE_SIZE = 16;
        private static final int     CHANNELS = 1;
        private static final boolean SIGNED = true;
        private static final boolean BIG_ENDIAN = true;
    
        // Chunk of audio processed at one time
        public static final int BUFFER_SIZE = 1000;
        public static final int SAMPLES_PER_BUFFER = BUFFER_SIZE / 2;
    
        public SamplePlayer() 
        {
    
            // Create the audio format we wish to use
            format = new AudioFormat(SAMPLE_RATE, SAMPLE_SIZE, CHANNELS, SIGNED, BIG_ENDIAN);
    
            // Create dataline info object describing line format
            info = new DataLine.Info(SourceDataLine.class, format);
        }
    
        public void run() 
        {
    
            done = false;
            int nBytesRead = 0;
    
            try 
            {
                // Get line to write data to
                auline = (SourceDataLine) AudioSystem.getLine(info);
                auline.open(format);
                auline.start();
    
                while ((nBytesRead != -1) && (! done)) 
                {
                    nBytesRead = getSamples(sampleData);
                    if (nBytesRead > 0) {
                        auline.write(sampleData, 0, nBytesRead);
                    }
                }
    
            } catch(Exception e) 
            {
                e.printStackTrace();                
            } finally {
                auline.drain();
                auline.close();                         
            }
        }       
    
        public void startPlayer() 
        {
            if (provider != null) 
            {
                start();    
            }
        }
    
        public void stopPlayer() 
        {
            done = true;
        }
    
        public void setSampleProvider(SampleProviderIntfc provider) 
        {
            this.provider = provider;
        }
        public int getSamples(byte [] samples)
        {
            return ByteBuffer.wrap(samples).getInt();
        }
    
        // Instance data
        private AudioFormat format;
        private DataLine.Info info;
        private SourceDataLine auline;
        private boolean done;
        private byte [] sampleData = new byte[BUFFER_SIZE];
        private SampleProviderIntfc provider;
    }
    
  • SampleProviderIntfc.java(有一个 getSamples() 方法存根)

SamplePlayer.java 中产生的错误:

2015-11-12 00:37:22.215 java[892:2167212] 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-12 00:37:22.215 java[892:2167212] Cannot find function pointer New_SHP_PlugIn for factory 834FC054-C1CC-11D6-BD01-00039315CD46 in CFBundle/CFPlugIn 0x7fd7abd29180 </Library/Audio/Plug-Ins/HAL/SeratoVirtualAudioPlugIn.plugin> (bundle, not loaded)
2015-11-12 00:37:22.253 java[892:2167212] 00:37:22.252 WARNING:  >compload> 472: Kickstart.component -- file://localhost/Library/Audio/Plug-Ins/Components/: trouble parsing Info.plist's AudioComponents, key (null); entry: <CFBasicHash 0x7fd7abc44cc0 [0x7fff73159390]>{type = mutable dict, count = 7,
entries =>
    2 : <CFString 0x7fff730e39f0 [0x7fff73159390]>{contents = "manufacturer"} = <CFString 0x7fd7abc4b9c0 [0x7fff73159390]>{contents = "CaNR"}
    7 : <CFString 0x7fff730e74b0 [0x7fff73159390]>{contents = "factoryFunction"} = <CFString 0x7fd7abc4b090 [0x7fff73159390]>{contents = "KickstartAUFactory"}
    8 : <CFString 0x7fd7abc4baa0 [0x7fff73159390]>{contents = "subtype"} = <CFString 0x7fd7abc4bae0 [0x7fff73159390]>{contents = "CNKS"}
    9 : <CFString 0x7fff7310b6f0 [0x7fff73159390]>{contents = "description"} = <CFString 0x7fd7abc4c180 [0x7fff73159390]>{contents = "Kickstart"}
    10 : <CFString 0x7fff7310a3d0 [0x7fff73159390]>{contents = "type"} = <CFString 0x7fd7abc4bbc0 [0x7fff73159390]>{contents = "kAudioUnitType_Effect"}
    11 : <CFString 0x7fff73095bb0 [0x7fff73159390]>{contents = "name"} = <CFString 0x7fd7abc4b990 [0x7fff73159390]>{contents = "Nicky Romero: Kickstart"}
    12 : <CFString 0x7fff73114370 [0x7fff73159390]>{contents = "version"} = <CFNumber 0x1000937 [0x7fff73159390]>{value = +65545, type = kCFNumberSInt64Type}
}
2015-11-12 00:37:22.256 java[892:2167212] 00:37:22.256 WARNING:  >compload> 472: Primer.component -- file://localhost/Library/Audio/Plug-Ins/Components/: trouble parsing Info.plist's AudioComponents, key (null); entry: <CFBasicHash 0x7fd7abc4b0f0 [0x7fff73159390]>{type = mutable dict, count = 7,
entries =>
    2 : <CFString 0x7fff730e39f0 [0x7fff73159390]>{contents = "manufacturer"} = <CFString 0x7fd7abc4b3e0 [0x7fff73159390]>{contents = "AudG"}
    7 : <CFString 0x7fff730e74b0 [0x7fff73159390]>{contents = "factoryFunction"} = <CFString 0x7fd7abc4b090 [0x7fff73159390]>{contents = "PrimerAUFactory"}
    8 : <CFString 0x7fd7abc4ac30 [0x7fff73159390]>{contents = "subtype"} = <CFString 0x7fd7abc4b990 [0x7fff73159390]>{contents = "agp"}
    9 : <CFString 0x7fff7310b6f0 [0x7fff73159390]>{contents = "description"} = <CFString 0x7fd7abc4af00 [0x7fff73159390]>{contents = "Primer"}
    10 : <CFString 0x7fff7310a3d0 [0x7fff73159390]>{contents = "type"} = <CFString 0x7fd7abc4c4c0 [0x7fff73159390]>{contents = "aumu"}
    11 : <CFString 0x7fff73095bb0 [0x7fff73159390]>{contents = "name"} = <CFString 0x7fd7abc4a1a0 [0x7fff73159390]>{contents = "Audible Genius: Primer"}
    12 : <CFString 0x7fff73114370 [0x7fff73159390]>{contents = "version"} = <CFNumber 0x1010137 [0x7fff73159390]>{value = +65793, type = kCFNumberSInt64Type}
}

这些错误在以下行中生成:

auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
4

0 回答 0