Midiplayer 在 16 个音符后停止播放声音

发布于 2024-09-03 12:09:26 字数 1447 浏览 3 评论 0原文

我目前正在编写一个钢琴键盘编辑器,很像你可以在 Cubase、Logic、Reason 等中找到的编辑器。

我有一个大网格、双数组 new int [13][9],它有 13 行、9 列。 第一列 [0-12][0] 是键盘,顶部有“high C”(midi note 72),底部有“low C”(midi note 60)。该列是 JButton 数组,当您按下“low C”时,合成器将播放音符 60。

目前我已经让它工作得很好,但我遇到的一个问题是我只能连续演奏 16 个音符,然后就像合成器关闭或其他什么情况一样。

你们知道问题出在哪里吗?

一些代码:

 import java.awt.*;
        import javax.swing.*;
        import java.awt.event.*;
        import java.util.*;
        import javax.sound.midi.*;

    actionPerformed(ActionEvent ae){
    for(int i = 0; i<13; i++){
            if(o== instr[i]){//instr is the button array
                SpelaTangent(i);
            }
        }

    }

    public void SpelaTangent(int tangent){


            int [] klaviatur = new int[13];
            for(int i = 0; i<13; i++){

                klaviatur[i] = (72-i);
                }

                         try {
                       Synthesizer synth = MidiSystem.getSynthesizer();                

synth.open(); 
                       final MidiChannel[] mc    = synth.getChannels();               

 Instrument[]        instrument = synth.getDefaultSoundbank().getInstruments();     

               synth.loadInstrument(instrument[1]);  
                        mc[0].noteOn(klaviatur[tangent],350);
                        mc[0].noteOff(klaviatur[tangent],350);

                   } catch (MidiUnavailableException e) {}
    }

非常感谢帮助!

I am currently programming a Piano Keyboard editor, much like the one you can find in Cubase, Logic, Reason etc..

I have this big grid, double array new int [13][9], which makes it 13 rows, 9 columns.
The first column [0-12][0] is the Keyboard, at the top there's "high C" (midi note 72) and at the bottom there's "low C" (midi note 60). That column is an array of JButtons and when you press for example "low C", note 60 is being played by the Synthesizer.

I've gotten this to work pretty OK as for now, but one problem I have is that I can only play 16 notes in a row, then it's like the Synthesizer shuts down or something.

Do you guys have ANY idea of what the problem is?

A bit of the code:

 import java.awt.*;
        import javax.swing.*;
        import java.awt.event.*;
        import java.util.*;
        import javax.sound.midi.*;

    actionPerformed(ActionEvent ae){
    for(int i = 0; i<13; i++){
            if(o== instr[i]){//instr is the button array
                SpelaTangent(i);
            }
        }

    }

    public void SpelaTangent(int tangent){


            int [] klaviatur = new int[13];
            for(int i = 0; i<13; i++){

                klaviatur[i] = (72-i);
                }

                         try {
                       Synthesizer synth = MidiSystem.getSynthesizer();                

synth.open(); 
                       final MidiChannel[] mc    = synth.getChannels();               

 Instrument[]        instrument = synth.getDefaultSoundbank().getInstruments();     

               synth.loadInstrument(instrument[1]);  
                        mc[0].noteOn(klaviatur[tangent],350);
                        mc[0].noteOff(klaviatur[tangent],350);

                   } catch (MidiUnavailableException e) {}
    }

Help is very much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守护在此方 2024-09-10 12:09:26

您似乎为每个演奏的音符初始化了新的 MIDI 系统。尝试将初始化代码移动到仅在程序开始时执行一次的地方,并重用您在那里创建的 MidiChannel。

You seem to initialize the MIDI system new for every note that is played. Try to move the initializing code somewhere it is only executed once at the start of your program and reuse the MidiChannel you create there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文