
    3j4#                        d dl mZ d dlZd dlmZ d dlmZ d dlmZ  G d dej                        Z G d d	      Z	 G d
 dej                        Ze	gZedk(  rd dlZ ej                  e       yy)    )annotationsN)pitch)exceptions21)
instrumentc                      e Zd Zy)MIDIPercussionExceptionN)__name__
__module____qualname__     D/DATA/.local/lib/python3.12/site-packages/music21/midi/percussion.pyr   r      s    r   r   c                     e Zd ZdZeZi dej                  dej                  dej                  dej                  dej                  dej                  dej                  d	ej                  d
ej                  dej                  dej                  dej                  dej                  dej                  dej                  dej                  dej                  i dej                  dej                  dej                  dej                  dej                  dej                  dej                  dej                   dej                   dej"                  dej"                  dej$                  dej&                  d ej&                  d!ej(                  d"ej(                  d#ej*                  d$ej*                  iZd% Zd& ZeegZy')(PercussionMapperaV  
    PercussionMapper provides tools to convert between 0-indexed MIDI pitches
    and music21 instruments, based on the official General MIDI Level 1 Percussion Key Map.
    This mapping is conventionally applied to MIDI channel 10;
    see https://midi.org/general-midi-level-1 for more info.

    Give me the instrument that corresponds to MIDI note 58!

    >>> pm = midi.percussion.PercussionMapper()
    >>> pm.reverseInstrumentMapping[58]
    <class 'music21.instrument.Vibraslap'>

    That's right, vibraslap.

    But you're better off using the midiPitchToInstrument() method below!
    #   $   %   &   (   )   *   +   ,   -   .   /   0   1   2   6   8   9   :   <   =   >   ?   @   A   B   C   D   F   G   H   L   M   P   Q   c                $   t        |t              r|}n|j                  }|| j                  vrt	        |d      | j                  |   } |       }|j
                  du r0t        |d      r$||j                  v r|j                  |   }||_        |S )a  
        Takes a pitch.Pitch object or int ranging from 0-127 and returns
        the corresponding instrument in the GM Percussion Map.

        >>> pm = midi.percussion.PercussionMapper()
        >>> cowPitch = pitch.Pitch(56)
        >>> cowbell = pm.midiPitchToInstrument(cowPitch)
        >>> cowbell
        <music21.instrument.Cowbell 'Cowbell'>

        Or it can just take an integer (representing MIDI note) for the pitch instead:

        >>> moreCowbell = pm.midiPitchToInstrument(56)
        >>> moreCowbell
        <music21.instrument.Cowbell 'Cowbell'>

        The standard GM Percussion list goes from 35 to 81;
        pitches outside this range raise an exception.

        >>> bassDrum1Pitch = pitch.Pitch('B-1')
        >>> pm.midiPitchToInstrument(bassDrum1Pitch)
        Traceback (most recent call last):
        music21.midi.percussion.MIDIPercussionException: 34 does not map to a valid instrument!

        Also, certain GM instruments do not have corresponding music21 instruments,
        so at present they also raise an exception.

        >>> cabasaPitch = 69
        >>> pm.midiPitchToInstrument(cabasaPitch)
        Traceback (most recent call last):
        music21.midi.percussion.MIDIPercussionException: 69 does not map to a valid instrument!

        Some music21 Instruments have more than one MidiPitch.  In this case you'll
        get the same Instrument object but with a different modifier

        >>> acousticBassDrumPitch = pitch.Pitch(35)
        >>> acousticBDInstrument = pm.midiPitchToInstrument(acousticBassDrumPitch)
        >>> acousticBDInstrument
        <music21.instrument.BassDrum 'Bass Drum'>
        >>> acousticBDInstrument.modifier
        'acoustic'

        >>> oneBassDrumPitch = pitch.Pitch(36)
        >>> oneBDInstrument = pm.midiPitchToInstrument(oneBassDrumPitch)
        >>> oneBDInstrument
        <music21.instrument.BassDrum 'Bass Drum'>
        >>> oneBDInstrument.modifier
        '1'

        z$ does not map to a valid instrument!T_percMapPitchToModifier)	
isinstanceintmidireverseInstrumentMappingr   inGMPercMaphasattrr5   modifier)self	midiPitch
midiNumbermidiInstrumentmidiInstrumentObjectr<   s         r   midiPitchToInstrumentz&PercussionMapper.midiPitchToInstrumenta   s    h i%"J"JT:::)ZN:^*_``66zB-/ ,,402KL1III/GG
S08$-##r   c                    t        |d      r|j                  du rt        |d      |j                  }t	        j
                         }||_        |S )a  
        Takes an instrument.Instrument object and returns a pitch object
        with the corresponding 1-indexed MIDI note, according to the GM Percussion Map.

        >>> pm = midi.percussion.PercussionMapper()
        >>> myCow = instrument.Cowbell()
        >>> cowPitch = pm.midiInstrumentToPitch(myCow)
        >>> cowPitch.midi
        56

        Note that cowPitch is an actual pitch.Pitch object
        even though it's meaningless!

        >>> cowPitch
        <music21.pitch.Pitch G#3>

        If the instrument does not have an equivalent in the GM Percussion Map,
        return an Exception:

        >>> myBagpipes = instrument.Bagpipes()
        >>> pipePitch = pm.midiInstrumentToPitch(myBagpipes)
        Traceback (most recent call last):
        music21.midi.percussion.MIDIPercussionException: <music21.instrument.Bagpipes 'Bagpipes'>
            is not in the GM Percussion Map!
        r:   Fz! is not in the GM Percussion Map!)r;   r:   r   percMapPitchr   Pitchr8   )r=   r@   r>   pitchObjects       r   midiInstrumentToPitchz&PercussionMapper.midiInstrumentToPitch   sT    4 ~}59S9SW\9\)^,>>_*`aa"//	kkm$r   N)r	   r
   r   __doc__r   iBassDrum	SnareDrumTomTomHiHatCymbalCrashCymbals
TambourineCowbell	Vibraslap
BongoDrums	CongaDrumTimbalesAgogoMaracasWhistle	WoodblockTriangler9   rB   rG   
_DOC_ORDERr   r   r   r   r      sl   " 	A/ "AJJ / " "AJJ/ " "AKK/ " !#AKK/ "
 !#AKK/ " !#AHH/ " !#AMM/ " !#AHH/ " !#AMM/ " !#AHH/ " !#AMM/ " !#AHH/ " !#AHH/ " !#ANN/ " !#AHH/ "& !#ALL'/ "* !#AII+/ ", !#ANN-/ ". !#AKK// "2 !#ALL3/ "4 !#ALL5/ "6 !#AKK7/ "8 !#AKK9/ ": !#AKK;/ "< !#AJJ=/ "> !#AJJ?/ "@ !#AGGA/ "B !#AGGC/ "F !#AIIG/ "H !#AIII/ "J !#AIIK/ "R !#AKKS/ "T !#AKKU/ "Z !#AJJ[/ "\ !#AJJ]/ "lC$JB ()>?Jr   r   c                      e Zd Zd Zy)Testc                2    ddl m}  || t                      y )Nr   )testCopyAll)music21.test.commonTestr^   globals)r=   r^   s     r   testCopyAndDeepcopyzTest.testCopyAndDeepcopy   s    7D')$r   N)r	   r
   r   ra   r   r   r   r\   r\      s    %r   r\   __main__)
__future__r   unittestmusic21r   r   r   Music21Exceptionr   r   TestCaser\   rZ   r	   mainTestr   r   r   <module>ri      su    #     	l;; 	o@ o@d%8 % 
 zGT r   