日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 生成正弦波声音_Java错误生成声音正弦波

發布時間:2024/3/7 java 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 生成正弦波声音_Java错误生成声音正弦波 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我這里有這個代碼,我從如何創建一個可聽正弦波的教程有: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 (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: {type = mutable dict, count = 7,

entries =>

2 : {contents = "manufacturer"} = {contents = "CaNR"}

7 : {contents = "factoryFunction"} = {contents = "KickstartAUFactory"}

8 : {contents = "subtype"} = {contents = "CNKS"}

9 : {contents = "description"} = {contents = "Kickstart"}

10 : {contents = "type"} = {contents = "kAudioUnitType_Effect"}

11 : {contents = "name"} = {contents = "Nicky Romero: Kickstart"}

12 : {contents = "version"} = {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: {type = mutable dict, count = 7,

entries =>

2 : {contents = "manufacturer"} = {contents = "AudG"}

7 : {contents = "factoryFunction"} = {contents = "PrimerAUFactory"}

8 : {contents = "subtype"} = {contents = "agp"}

9 : {contents = "description"} = {contents = "Primer"}

10 : {contents = "type"} = {contents = "aumu"}

11 : {contents = "name"} = {contents = "Audible Genius: Primer"}

12 : {contents = "version"} = {value = +65793, type = kCFNumberSInt64Type}

}

我完全不明白這個錯誤。我唯一知道的是它正在訪問我的音頻插件。我也知道錯誤發生在:

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

line.open(format);

有沒有人知道這個錯誤的原因是什么?或者如何解決它?

總結

以上是生活随笔為你收集整理的java 生成正弦波声音_Java错误生成声音正弦波的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。