简体   繁体   English

JavaScript 音序器 MIDI 文件时序分辨率 (PPQN)

[英]JavaScript Sequencer MIDI file timing resolution (PPQN)

I read a lot about MIDI resolution and studied some codes like Tone.js and heartbeat.我阅读了很多关于 MIDI 分辨率的内容,并研究了一些代码,如 Tone.js 和 heartbeat。 But I don't understand why there are different Pulses Per Quarter Note (PPQN) values and what the effect it has on playing notes.但我不明白为什么会有不同的每季度音符 (PPQN)值以及它对演奏音符的影响。 When I have 960 PPQN so this means 1 quarter note has 960 ticks, 1 eight note 480 ticks, ect.当我有 960 PPQN 时,这意味着 1 个四分音符有 960 个刻度,1 个八分音符有 480 个刻度,等等。 And if I understand it correctly, the delta time is just a relative value.如果我理解正确,增量时间只是一个相对值。

What I don't understand right is, what should the PPQN when I play notes in JavScript, and when I set the PPQN why it should have this value?我不明白的是,当我在 JavScript 中演奏音符时,PPQN 应该是什么,以及当我设置 PPQN 时为什么它应该有这个值? For example I use the WebAudio API for playing notes:例如,我使用 WebAudio API 来播放音符:

function nextNote() {
  var quarterBeat = 60.0/tempo;
  nextNoteDuration = nextNoteDuration + (quarterBeat/32);
  currentNote++;
}

This way I can play different notes durations.这样我就可以演奏不同的音符时长。 Now when I read the MIDI file, should I just compare the delta time and convert it to my sequencers current playback?现在,当我读取 MIDI 文件时,是否应该只比较增量时间并将其转换为我的音序器当前播放? For example when I read a MIDI file with this values:例如,当我读取具有以下值的 MIDI 文件时:

Tempo = 120
PQN = 960
4 Quarter Notes

I read the MIDI file, save the notes in an array (assume the delta time is for each note a 1/4)我读取了 MIDI 文件,将音符保存在一个数组中(假设每个音符的增量时间为 1/4)

duration = [quarterNote, quarterNote, quarterNote, quarterNote]

And play the notes:并演奏音符:

while (nextNoteDuration < audioContext.currentTime) {
  if (duration[i] %32 == 0) playNote(currentNote, nextNoteDuration);
  nextNote();
  i++;
}

Should I use PPQN only when exporting a MIDI file?我应该只在导出 MIDI 文件时使用 PPQN 吗? If so, in relation to what should I set the PPQN?如果是这样,我应该如何设置 PPQN? I hope someone can explain this to me in more detail.我希望有人能更详细地向我解释这一点。

PPQ is about resolution. PPQ 是关于分辨率的。 More specifically about time resolution.更具体地说是关于时间分辨率。

what should (be?) the PPQN when I play notes in JavScript, and when I set the PPQN why it should have this value?当我在 JavScript 中演奏音符时,PPQN 应该(是什么?),以及当我设置 PPQN 时为什么它应该具有这个值?

When your program plays notes, it may use whatever time units you want or need, for instance milliseconds, nanoseconds, movie frames, ticks.当您的程序播放音符时,它可以使用您想要或需要的任何时间单位,例如毫秒、纳秒、电影帧、滴答声。 Absolute wall clock or relative times.绝对挂钟或相对时间。 It depends on your sequencer capabilities and your software features.这取决于您的音序器功能和软件功能。 It is only required to choose a PPQ value when storing MIDI sequences as MIDI files.它只需要存储MIDI序列的MIDI文件时,选择一个PPQ值。 Of course you need to be able to convert time units when reading/storing MIDI files.当然,您需要能够在读取/存储 MIDI 文件时转换时间单位。

Now when I read the MIDI file, should I just compare the delta time and convert it to my sequencers current playback?现在,当我读取 MIDI 文件时,是否应该只比较增量时间并将其转换为我的音序器当前播放?

Of course, if internally your music events use a different time representation, then you need to be able to translate the delta times from the MIDI file to your internal representation.当然,如果您的音乐事件在内部使用不同的时间表示,那么您需要能够将 MIDI 文件中的增量时间转换为您的内部表示。

You are only asking about PPQ, but this value is found only once, at the MIDI file header.您只是询问 PPQ,但此值仅在 MIDI 文件头中出现一次。 The tempo events in contrast may occur multiple times along the file, and it affects to the translation of the next delta times into wall clock times until the next tempo event.相反,速度事件可能会在文件中多次出现,它会影响下一个增量时间到挂钟时间的转换,直到下一个速度事件。 If your sequencer/player allows the user to change/add tempo events, it would be a good idea to use relative units instead of wall clock units for internal time representation (or both).如果您的音序器/播放器允许用户更改/添加速度事件,最好使用相对单位而不是挂钟单位来表示内部时间(或两者)。

Should I use PPQN only when exporting a MIDI file?我应该只在导出 MIDI 文件时使用 PPQN 吗? If so, in relation to what should I set the PPQN?如果是这样,我应该如何设置 PPQN?

Yes, you need to choose a suitable value for PPQ when you export a MIDI file.是的,您在导出 MIDI 文件时需要为 PPQ 选择一个合适的值。 If your internal time units are relative (as most sequencers do) then use your internal unit resolution for PPQ.如果您的内部时间单位是相对的(正如大多数音序器所做的那样),则使用您的内部单位分辨率进行 PPQ。 If you convert from wall clock units to delta times, then you need to choose a resolution which your translation loses less details (by quantization).如果您从挂钟单位转换为增量时间,那么您需要选择一个分辨率,使您的翻译丢失更少的细节(通过量化)。 Higher values are better in this sense.从这个意义上说,更高的值更好。 Rosegarden stores MIDI files always with 960 PPQ. Rosegarden始终以 960 PPQ 存储 MIDI 文件。 In contrast, Steinberg's Cubase used 480. I remember using Cakewalk a long time ago having only 120 PPQ, but later versions allowed this value to be changed as a configuration setting.相比之下,Steinberg 的Cubase使用的是 480。我记得很久以前使用 Cakewalk 时只有 120 PPQ,但后来的版本允许将此值更改为配置设置。 In general you need to accommodate not only all music figures shorter than quarter, including subdivisions like triplets, etc. but you also need to take into account effects like swing that are based on fine time adjustments.通常,您不仅需要容纳所有短于四分之一的音乐数字,包括三连音等细分,而且还需要考虑基于精细时间调整的摇摆效果。

In a standard MIDI file, there's information in the file header about “ticks per quarter note”, aka “parts per quarter” (or “PPQ”).在标准的 MIDI 文件中,文件头中有关于“每四分音符的刻度”的信息,也就是“每季度的部分”(或“PPQ”)。 For the purpose of this discussion, we'll consider “beat” and “quarter note” to be synonymous, so you can think of a “tick” as a fraction of a beat.出于本次讨论的目的,我们将“节拍”和“四分音符”视为同义词,因此您可以将“滴答声”视为节拍的一小部分。 The PPQ is stated in the last word of information (the last two bytes) of the header chunk that appears at the beginning of the file. PPQ 在出现在文件开头的头块的信息的最后一个字(最后两个字节)中声明。 The PPQ could be a low number such as 24 or 96, which is often sufficient resolution for simple music, or it could be a larger number such as 480 for higher resolution, or even something like 500 or 1000 if one prefers to refer to time in milliseconds. PPQ 可以是一个较低的数字,例如 24 或 96,这对于简单的音乐来说通常是足够的分辨率,或者它可以是更大的数字,例如 480 以获得更高分辨率,甚至可以是 500 或 1000(如果人们喜欢参考时间)以毫秒为单位。

What the PPQ means in terms of absolute time depends on the designated tempo. PPQ 在绝对时间方面的含义取决于指定的速度。 By default, the time signature is 4/4 and the tempo is 120 beats per minute.默认情况下,拍号为 4/4,速度为每分钟 120 拍。 That can be changed, however, by a “meta event” that specifies a different tempo.然而,这可以通过指定不同速度的“元事件”来改变。 (You can read about the Set Tempo meta event message in the file format description document.) The tempo is expressed as a 24-bit number that designates microseconds per quarter-note. (您可以在文件格式描述文档中阅读有关 Set Tempo 元事件消息的信息。)速度表示为 24 位数字,表示每四分音符的微秒数。 That's kind of upside-down from the way we normally express tempo, but it has some advantages.这与我们通常表达速度的方式有点颠倒,但它有一些优点。 So, for example, a tempo of 100 bpm would be 600000 microseconds per quarter note, so the MIDI meta event for expressing that would be FF 51 03 09 27 C0 (the last three bytes are the Hex for 600000).因此,例如,100 bpm 的速度将是每四分音符 600000 微秒,因此用于表达的 MIDI 元事件将是 FF 51 03 09 27 C0(最后三个字节是 600000 的十六进制)。 The meta event would be preceded by a delta time, just like any other MIDI message in the file, so a change of tempo can occur anywhere in the music.元事件之前会有一个增量时间,就像文件中的任何其他 MIDI 消息一样,因此速度的变化可以发生在音乐的任何地方。

Delta times are always expressed as a variable-length quantity, the format of which is explained in the document. Delta 时间总是表示为一个可变长度的数量,其格式在文档中进行了解释。 For example, if the PPQ is 480 (standard in most MIDI sequencing software), a delta time of a dotted quarter note (720 ticks) would be expressed by the two bytes 85 50 (hexadecimal).例如,如果 PPQ 为 480(大多数 MIDI 音序软件中的标准),则带点四分音符(720 滴答声)的增量时间将由两个字节 85 50(十六进制)表示。

So, bearing all that in mind, there is a correspondence between delta times expressed in terms of ticks and note values as we think of them in human terms.因此,考虑到所有这些,以刻度和音符值表示的增量时间之间存在对应关系,正如我们在人类术语中所认为的那样。 The relationship depends on the PPQ specified in the header chunk.这种关系取决于头块中指定的 PPQ。 For example, if the PPQ is 96 (hex 60), then a note middle C on MIDI channel 10 with a velocity of 127 lasting a dotted quarter note (1.5 beats) would be expressed as 00 99 3C 7F // delta time 0 ticks, 153 60 127 90 99 3C 00 // delta time 144 ticks, 153 60 0例如,如果 PPQ 为 96(十六进制 60),则 MIDI 通道 10 上的音符中间 C 的力度为 127,持续一个带点四分音符(1.5 拍)将表示为 00 99 3C 7F // delta time 0 tick , 153 60 127 90 99 3C 00 // delta time 144 ticks, 153 60 0

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

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