繁体   English   中英

在处理中,如何在一定时间后停止素描弹奏音符?

[英]In processing, how can I stop sketch playing notes after a certain duration ?

我如何更改上面显示的setup()方法,以使它不仅演奏一个音符,还执行以下操作:-随机生成一个介于0到10之间的浮点值,然后演奏少于或等于的尽可能多的音符到由随机数表示的总持续时间。

例如,如果生成了随机值5.2,则应该演奏前三个音符,因为它们的总持续时间为3.8,小于5.2; 如果生成了随机值8.7,则应该演奏前六个音符,因为它们的总持续时间等于8.7。

import arb.soundcipher.*;
SoundCipher midi;

String[] note = {"C", "C#", "D", "D#", "E", "F", "F#",
                 "G", "G#", "A", "A#", "B"};
float[] volume = {80,  100,  75,   43,  40,  81,  100,
                  60,   90,  30,   75,  52};
float[] duration = {1.3,   2, 0.5,    3, 0.9,   1, 0.25,
                    0.6, 1.5,   3, 1.25,   2};

void setup() {
  size(200,200);
  midi = new SoundCipher(this);
  int i = (int) random(note.length);
  midi.playNote(MIDIValue(note[i]),volume[i],duration[i]);
}

float MIDIValue(String aNote) {
  float noteMIDIValue = 0.0;
  int i;
  for (i =0; i < note.length && !note[i].equals(aNote) ; i++); 
  if(i < note.length) {
     noteMIDIValue = i+60;
  }
  return noteMIDIValue;
}

为了获得随机数,您需要使用Math.random() ,它会生成一个介于0和1之间的随机数。要使其生成,它会生成一个介于0和10之间的随机双精度数,您需要这样做10,所以Math.random() * 10

要使其播放所需的音符,您可以使用此随机数从duration列表中“削掉”一个元素,直到这样做会使其保持在下一个持续时间以下。

一个代码示例可能如下所示:

    float randomNumber = (float)Math.random() * 10f;
    int currentNote = 0;
    while(randomNumber > duration[currentNote]){
        randomNumber -= duration[currentNote]; // "Chip away" duration
        midi.playNote(MIDIValue(note[currentNote]),volume[currentNote],duration[currentNote]); // Play the note
        currentNote++; // Try out the next note
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM