java audiosystem_Java AudioSystem和TargetDataLine

我试图从我的电脑上接收来自线路输入的音频,为此我正在使用

AudioSystem类.静态AudioSystem.write方法有两种选择之一:写入文件或写入流.我可以让它写入文件就好了,但每当我尝试写入流时,我都会抛出java.io.IOException(未指定流长度).至于我的缓冲区,我使用的是ByteArrayOutputStream.是否有其他类型的流我应该使用或搞乱其他地方?

同样在相关主题中,可以通过调用read直接在(TargetDataLine)中对音频线进行采样.这是进行音频捕获还是使用AudioSystem的首选方式?

更新

请求的源代码:

final private TargetDataLine line;

final private AudioFormat format;

final private AudioFileFormat.Type fileType;

final private AudioInputStream audioInputStream;

final private ByteArrayOutputStream bos;

// Constructor, etc.

public void run()

{

System.out.println("AudioWorker Started");

try

{

line.open(format);

line.start();

// This commented part is regarding the second part

// of my question

// byte[] buff = new byte[512];

// int bytes = line.read(buff, 0, buff.length);

AudioSystem.write(audioInputStream, fileType, bos);

}

catch ( Exception e )

{

e.printStackTrace();

}

System.out.println("AudioWorker Finished");

}

// Stack trace in console

AudioWorker Started

java.io.IOException: stream length not specified

at com.sun.media.sound.WaveFileWriter.write(Unknown Source)

at javax.sound.sampled.AudioSystem.write(Unknown Source)

at AudioWorker.run(AudioWorker.java:41)

AudioWorker Finished

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Swing是一种基于Java语言的图形用户界面(GUI)工具包。使用Java Swing,可以轻松地创建各种GUI应用程序,包括钢琴程序。以下是一个简单的Java Swing钢琴程序示例,支持录音等功能。 ```java import java.awt.*; import java.awt.event.*; import java.io.*; import javax.sound.midi.*; import javax.swing.*; public class Piano extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JButton[] whiteKeys = new JButton[7]; private JButton[] blackKeys = new JButton[5]; private JButton recordButton; private JButton playButton; private JButton stopButton; private boolean isRecording = false; private boolean isPlaying = false; private Sequence sequence; private Sequencer sequencer; private Track track; private int currentTick; private ByteArrayOutputStream byteArrayOutputStream; private AudioFormat audioFormat; private TargetDataLine targetDataLine; private AudioInputStream audioInputStream; private byte[] audioData; public Piano() { setTitle("Java Piano"); setSize(800, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); JPanel keyboardPanel = new JPanel(); keyboardPanel.setLayout(new GridLayout(1, 0)); // Add white keys String[] whiteNotes = {"C", "D", "E", "F", "G", "A", "B"}; for (int i = 0; i < 7; i++) { whiteKeys[i] = new JButton(whiteNotes[i]); whiteKeys[i].setPreferredSize(new Dimension(80, 200)); whiteKeys[i].addActionListener(this); keyboardPanel.add(whiteKeys[i]); } // Add black keys String[] blackNotes = {"C#", "D#", "", "F#", "G#", "A#"}; for (int i = 0; i < 6; i++) { if (i != 2) { blackKeys[i] = new JButton(blackNotes[i]); blackKeys[i].setPreferredSize(new Dimension(50, 120)); blackKeys[i].setBackground(Color.BLACK); blackKeys[i].setOpaque(true); blackKeys[i].addActionListener(this); keyboardPanel.add(blackKeys[i]); } } // Add record, play, and stop buttons JPanel controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); recordButton = new JButton("Record"); recordButton.addActionListener(this); controlPanel.add(recordButton); playButton = new JButton("Play"); playButton.addActionListener(this); controlPanel.add(playButton); stopButton = new JButton("Stop"); stopButton.addActionListener(this); controlPanel.add(stopButton); add(keyboardPanel, BorderLayout.CENTER); add(controlPanel, BorderLayout.SOUTH); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == recordButton) { if (!isRecording) { isRecording = true; recordButton.setText("Stop Recording"); startRecording(); } else { isRecording = false; recordButton.setText("Record"); stopRecording(); } } else if (e.getSource() == playButton) { if (!isPlaying) { isPlaying = true; playButton.setText("Stop Playing"); startPlaying(); } else { isPlaying = false; playButton.setText("Play"); stopPlaying(); } } else { JButton button = (JButton) e.getSource(); String note = button.getText(); playSound(note); if (isRecording) { addNoteToSequence(note); } } } private void startRecording() { try { audioFormat = getAudioFormat(); DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat); targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo); targetDataLine.open(audioFormat); targetDataLine.start(); byteArrayOutputStream = new ByteArrayOutputStream(); audioData = new byte[1024]; new Thread(new Runnable() { public void run() { while (isRecording) { int numBytesRead = targetDataLine.read(audioData, 0, audioData.length); byteArrayOutputStream.write(audioData, 0, numBytesRead); } } }).start(); } catch (Exception ex) { ex.printStackTrace(); } } private void stopRecording() { try { targetDataLine.stop(); targetDataLine.close(); byteArrayOutputStream.close(); byte[] byteArray = byteArrayOutputStream.toByteArray(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); audioInputStream = new AudioInputStream(byteArrayInputStream, audioFormat, byteArray.length / audioFormat.getFrameSize()); // Convert audio data to MIDI sequence AudioToMidiConverter converter = new AudioToMidiConverter(); sequence = converter.convert(audioInputStream); // Create new track to add notes to sequencer = MidiSystem.getSequencer(); sequencer.open(); sequence.deleteTrack(sequence.getTracks()[0]); track = sequence.createTrack(); currentTick = 0; } catch (Exception ex) { ex.printStackTrace(); } } private void startPlaying() { try { sequencer.setSequence(sequence); sequencer.start(); } catch (Exception ex) { ex.printStackTrace(); } } private void stopPlaying() { sequencer.stop(); sequencer.close(); } private void addNoteToSequence(String note) { try { int velocity = 127; int duration = 100; int pitch = getMidiPitch(note); ShortMessage messageOn = new ShortMessage(ShortMessage.NOTE_ON, 0, pitch, velocity); track.add(new MidiEvent(messageOn, currentTick)); currentTick += duration; ShortMessage messageOff = new ShortMessage(ShortMessage.NOTE_OFF, 0, pitch, velocity); track.add(new MidiEvent(messageOff, currentTick)); } catch (Exception ex) { ex.printStackTrace(); } } private void playSound(String note) { try { Synthesizer synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); MidiChannel[] channels = synthesizer.getChannels(); int velocity = 127; int pitch = getMidiPitch(note); channels[0].noteOn(pitch, velocity); Thread.sleep(100); channels[0].noteOff(pitch, velocity); synthesizer.close(); } catch (Exception ex) { ex.printStackTrace(); } } private int getMidiPitch(String note) { String[] notes = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"}; int[] pitches = {60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71}; for (int i = 0; i < notes.length; i++) { if (note.equals(notes[i])) { return pitches[i]; } } return 0; } private AudioFormat getAudioFormat() { float sampleRate = 44100.0F; int sampleSizeInBits = 16; int channels = 1; boolean signed = true; boolean bigEndian = false; return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); } public static void main(String[] args) { new Piano(); } } ``` 这个程序使用Java Swing创建了一个钢琴界面,包括白键和黑键。它还包括三个按钮:录音,播放和停止。当用户按下一个键时,程序将播放相应的音符,并且如果正在录音,则将该音符添加到MIDI序列中。当用户按下录音按钮时,程序开始录制音频,并将其转换为MIDI序列。当用户按下播放按钮时,程序将播放MIDI序列中的音符。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值