cpsmidinn
Prev | Orchestra Opcodes and Operators | Next

## cpsmidinn

cpsmidinn — Converts a Midi note number value to cycles-per-second.

## Description

Converts a Midi note number value to cycles-per-second.

## Syntax

    cpsmidinn (MidiNoteNumber)  (init- or control-rate args only)

where the argument within the parentheses may be a further expression.

## Performance

cpsmidinn is a function that takes an i-rate or k-rate value representing a Midi note number and returns the equivalent frequency value in cycles-per-second (Hertz). This conversion assumes that Middle C is Midi note number 60 and that Middle A is tuned to 440 Hz. Midi note number values are typically integers in the range from 0 to 127 but fractional values or values outside of this range will be interpreted consistently.

| cpsmidinn vs. cpsmidi

The cpsmidinn opcode may be used in any Csound instrument instance whether it is activated from a Midi event, score event, line event, or from another instrument. The input value for cpsmidinn might for example come from a p-field in a textual score or it may have been retrieved from the real-time Midi event that activated the current note using the notnum opcode. You must specify an i-rate or k-rate expression for the Midi note number that is to be converted. On the other hand, the cpsmidi opcode only produces meaningful results in a Midi-activated note (either real-time or from a Midi score with the -F flag). With cpsmidi, the Midi note number value is taken from the Midi event associated with the instrument instance, and no location or expression for this value may be specified.

cpsmidinn and its related opcodes are really value converters with a special function of manipulating pitch data.

Data concerning pitch and frequency can exist in any of the following forms:

Table 6. Pitch and Frequency Values

Name | Abbreviation
--- | ---
octave point pitch-class (8ve.pc) | pch
octave point decimal | oct
cycles per second | cps
Midi note number (0-127) | midinn

The first two forms consist of a whole number, representing octave registration, followed by a specially interpreted fractional part. For pch, the fraction is read as two decimal digits representing the 12 equal-tempered pitch classes from .00 for C to .11 for B. For oct, the fraction is interpreted as a true decimal fractional part of an octave. The two fractional forms are thus related by the factor 100/12. In both forms, the fraction is preceded by a whole number octave index such that 8.00 represents Middle C, 9.00 the C above, etc. Midi note number values range between 0 and 127 (inclusively) with 60 representing Middle C, and are usually whole numbers. Thus A440 can be represented alternatively by 440 (cps), 69 (midinn), 8.09 (pch), or 8.75 (oct). Microtonal divisions of the pch semitone can be encoded by using more than two decimal places.

The mnemonics of the pitch conversion units are derived from morphemes of the forms involved, the second morpheme describing the source and the first morpheme the object (result). Thus cpspch(8.09) will convert the pitch argument 8.09 to its cps (or Hertz) equivalent, giving the value of 440. Since the argument is constant over the duration of the note, this conversion will take place at i-time, before any samples for the current note are produced.

By contrast, the conversion cpsoct(8.75 + k1) which gives the value of A440 transposed by the octave interval k1. The calculation will be repeated every k-period since that is the rate at which k1 varies.

| Note

The conversion from pch, oct, or midinn into cps is not a linear operation but involves an exponential process that could be time-consuming when executed repeatedly. Csound now uses a built-in table lookup to do this efficiently, even at audio rates. Because the table index is truncated without interpolation, pitch resolution when using one of these opcodes is limited to 8192 discrete and equal divisions of the octave, and some pitches of the standard 12-tone equally-tempered scale are very slightly mistuned (by at most 0.15 cents).

## Examples

Here is an example of the cpsmidinn opcode. It uses the file cpsmidinn.csd.

Example 177. Example of the cpsmidinn opcode.

See the sections Real-time Audio and Command Line Flags for more information on using command line flags.

    <CsoundSynthesizer>
    ; Prints a table showing the equivalents of all Midi
    ; note numbers from 0-127 in cycles-per-second,
    ; octave.decimal, and octave.pitchclass units.

    <CsOptions>
    ; Select audio/midi flags here according to platform.
    ; This example produces no audio, so we render in
    ; non-realtime and turn off sound to disk:
    -n
    </CsOptions>
    <CsInstruments>

    instr 1
      ; i-time loop to print conversion table
      imidiNN =   0
      loop1:
        icps  = cpsmidinn(imidiNN)
        ioct  = octmidinn(imidiNN)
        ipch  = pchmidinn(imidiNN)

        print   imidiNN, icps, ioct, ipch

        imidiNN = imidiNN + 1
      if (imidiNN < 128) igoto loop1
    endin

    instr 2
      ; test k-rate converters
      kMiddleC  =   60
      kcps  = cpsmidinn(kMiddleC)
      koct  = octmidinn(kMiddleC)
      kpch  = pchmidinn(kMiddleC)

      printks "%d %f %f %f\n", 1.0, kMiddleC, kcps, koct, kpch
    endin

    </CsInstruments>
    <CsScore>
    i1 0 0
    i2 0 0.1
    e

    </CsScore>
    </CsoundSynthesizer>

Here is another example of the cpsmidinn opcode. It uses the file cpsmidinn2.csd.

Example 178. Second example of the cpsmidinn opcode.

    <CsoundSynthesizer>
    <CsOptions>
    ; Select audio/midi flags here according to platform
    ;;;RT audio out, midi in, note=p4 and velocity=p5
    -odac -+rtmidi=virtual -M0d --midi-key=4 --midi-velocity-amp=5
    ;-iadc    ;;;uncomment -iadc if RT audio input is needed too
    ; For Non-realtime ouput leave only the line below:
    ; -o cpsmidinn.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>

    sr = 44100
    ksmps = 32
    nchnls = 2
    0dbfs = 1

    massign 0, 1	;assign all midi to instr. 1

    instr 1	;play virtual keyboard

    inote = p4
    icps  = cpsmidinn(inote)
    asig	oscil 0.6, icps, 1
    	print icps
    	outs  asig, asig

    endin

    </CsInstruments>
    <CsScore>
    f0 20
    ;sine wave.
    f 1 0 16384 10 1
    ;play note from score too
    i1 0 1 60
    e
    </CsScore>
    </CsoundSynthesizer>

## See also

Pitch Converters: Functions

Midi Converters

## Credits

Derived from original value converters by Barry Vercoe.

New in version 5.07

Prev | Up | Next
cpsmidib | Home | cpsoct