
    3j*                       d Z ddlmZ ddlmZ ddlmZ ddlZddlm	Z	 ddl
mZ ddlmZ dd	lmZ  G d
 de      Z G d d      Z G d dej$                        Z G d dej$                        Zedk(  rddlZ ej,                  e       yy)a  
Objects for realtime playback of Music21 Streams as MIDI.

From an idea of Joe "Codeswell":

https://joecodeswell.wordpress.com/2012/06/13/how-to-produce-python-controlled-audio-output-from-music-made-with-music21

https://stackoverflow.com/questions/10983462/how-can-i-produce-real-time-audio-output-from-music-made-with-music21

Requires pygame 2 (or 1.9) install with: pip3 install pygame
    )annotations)	find_spec)BytesION)defaults)Music21Exception)stream)	translatec                      e Zd Zy)StreamPlayerExceptionN__name__
__module____qualname__     B/DATA/.local/lib/python3.12/site-packages/music21/midi/realtime.pyr   r   "       r   r   c                      e Zd ZdZdZ	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 d ed      dddZd	 Z	 	 d ed      ddd
Z	d Z
y)StreamPlayera)  
    Create a player for a stream that plays its midi version in realtime using pygame.

    Set up a detuned piano (where each key has a random but
    consistent detuning from 30 cents flat to sharp)
    and play a Bach Chorale on it in real time.

    >>> import random
    >>> keyDetune = []
    >>> for i in range(127):
    ...    keyDetune.append(random.randint(-30, 30))

    >>> #_DOCS_SHOW b = corpus.parse('bwv66.6')
    >>> #_DOCS_SHOW for n in b.flatten().notes:
    >>> class PitchMock: midi = 20  #_DOCS_HIDE
    >>> class Mock: pitch = PitchMock()  #_DOCS_HIDE
    >>> #_DOCS_HIDE -- should not play back in doctests, see TestExternal
    >>> n = Mock()  #_DOCS_HIDE
    >>> for i in [1]:  #_DOCS_HIDE
    ...    n.pitch.microtone = keyDetune[n.pitch.midi]
    >>> #_DOCS_SHOW sp = midi.realtime.StreamPlayer(b)
    >>> #_DOCS_SHOW sp.play()

    The stream is stored (unaltered) in `StreamPlayer.streamIn`, and can be changed any time the
    midi file is not playing.

    A number of mixer controls can be passed in with keywords:

    *  mixerFreq (default 44100 -- CD quality)
    *  mixerBitSize (default -16 (=unsigned 16bit) --
         really, are you going to do 24bit audio with Python?? :-)  )
    *  mixerChannels (default 2 = stereo)
    *  mixerBuffer (default 1024 = number of samples)
    Fc                    	 dd l }|| _         | j                  du s|r|j                  j                  ||||       || _        y # t        $ r t        d      w xY w)Nr   z,StreamPlayer requires pygame.  Install firstF)pygameImportErrorr   mixerInitializedmixerinitstreamIn)selfr   reinitMixer	mixerFreqmixerBitSizemixerChannelsmixerBufferr   s           r   __init__zStreamPlayer.__init__K   sb    	X DK   E)[LLi}kR   	X'(VWW	Xs   A ANinfT)playForMillisecondsblockedc          
     V    | j                         }| j                  ||||||||       y)a  
        busyFunction is a function that is called with busyArgs when the music is busy every
        busyWaitMilliseconds.

        endFunction is a function that is called with endArgs when the music finishes playing.

        playForMilliseconds is the amount of time in milliseconds after which
        the playback will be automatically stopped.

        If blocked is False, the method will finish before ending the stream, allowing
        you to completely control whether to stop it. Ignore every other arguments
        )busyFunctionbusyArgsendFunctionendArgsbusyWaitMillisecondsr%   r&   N)getStringOrBytesIOFileplayStringIOFile)	r   r(   r)   r*   r+   r,   r%   r&   streamStringIOFiles	            r   playzStreamPlayer.play_   s>    * "88:0+7'/*5&-3G2E&- 	 	/r   c                v    t        j                  | j                        }|j                         }t	        |      S N)midiTranslatestreamToMidiFiler   writestrr   )r   streamMidiFilestreamMidiWrittens      r   r-   z#StreamPlayer.getStringOrBytesIOFile~   s1    &77F*335())r   c                  | j                   j                  j                         }		 | j                   j                  j                  j                  |       | j                   j                  j                  j                          |syt        d|z        }| j                   j                  j                         }| j                   j                  j                  j                         r| ||       | j                   j                  j                         |z
  |kD  r/| j                   j                  j                  j                          n@|	j                  |       | j                   j                  j                  j                         r|	 ||       yy# | j                   j                  $ r}
t        d| d|
       |
d}
~
ww xY w)a'  
        busyFunction is a function that is called with busyArgs when the music is busy every
        busyWaitMilliseconds.

        endFunction is a function that is called with endArgs when the music finishes playing.

        playForMilliseconds is the amount of time in milliseconds after which the
        playback will be automatically stopped.

        If blocked is False, the method will finish before ending the stream, allowing you to
        completely control whether to stop it. Ignore every other arguments but for stringIOFile
        zCould not play music file z
 because: N  )r   timeClockr   musicloaderrorr   r0   int	get_ticksget_busystoptick)r   stringIOFiler(   r)   r*   r+   r,   r%   r&   pygameClockpge	framerate
start_times                r   r.   zStreamPlayer.playStringIOFile   sr     kk&&,,.	KK##((6
 	$$&334	[[%%//1
kk%%..0'X&{{))+j8;NN!!'',,.Y' kk%%..0 "  ## {{   	',\N*SEJ	s   /F G.G  Gc                `    | j                   j                  j                  j                          y r2   )r   r   r<   rB   )r   s    r   rB   zStreamPlayer.stop   s    $$&r   )FiD  i   i   )r   zstream.Streamr   boolr   r?   r    r?   r!   r?   r"   r?   )NNNN2   )r   r   r   __doc__r   r#   floatr0   r-   r.   rB   r   r   r   r   r   &   s    !D 
 "!! ! 	!
 ! ! !* "$/ "'u/>*
 JNNP%! .35\4%!N'r   r   c                      e Zd Zy)TestNr   r   r   r   rP   rP      r   r   rP   c                  n    e Zd Z ed      ZedZndZ ej                  ed      d        Zd Z	d Z
d	 Zy)
TestExternalr   NTFzpygame is not installedc                d   ddl m} dd l}|j                  d      }g }t	        d      D ]#  }|j                  |j                  dd             % |j                         j                  D ]*  }||j                  j                     |j                  _        , t        |      }|j                          y )Nr   corpusbwv66.6      )music21rU   randomparserangeappendrandintrecursenotespitchmidi	microtoner   r0   )r   rU   r[   b	keyDetuneinsps           r   testBachDetunezTestExternal.testBachDetune   s    "LL#	sAV^^C45 ""A )!'',, 7AGG #!_
	r   c                   ddl m} ddl}d } G d d      } |       }d|_        |j	                  d      }g }t        d	      D ]#  }|j                  |j                  d
d             % |j                         j                  D ]*  }	||	j                  j                     |	j                  _        , t        |      }
|
j                  ||gd       y)zN
        tests to see if the busyCallback function is called properly
        r   rT   Nc                ~    | d   }|xj                   |j                  z  c_         t        d|j                    d       y )Nr   zhi! waited z milliseconds)times
updateTimeprint)timeListtimeCounter_inners     r   busyCounterz4TestExternal.x_testBusyCallback.<locals>.busyCounter   s=     (##'8'C'CC#K 1 7 78FGr   c                      e Zd ZdZy)-TestExternal.x_testBusyCallback.<locals>.Mockr   N)r   r   r   rm   r   r   r   Mockrt      s    Er   ru     zbach/bwv66.6rW   rX   rY   r(   r)   r,   )rZ   rU   r[   rn   r\   r]   r^   r_   r`   ra   rb   rc   rd   r   r0   )r   rU   r[   rr   ru   timeCounterre   rf   rg   rh   ri   s              r   x_testBusyCallbackzTestExternal.x_testBusyCallback   s     	#	H
	 	 f!$LL(	sAV^^C45 ""A )!'',, 7AGG #!_
[K=WZ[r   c                h   ddl m} dt        _        |j	                  d      }g }t        |j                  d   j                  t        j                              }t        |      D ]"  }|j                  |j                  |             $ t        |      }|D ]  }||_        |j                           y )Nr   rT   rV   )rZ   rU   r   ticksAtStartr\   lenpartsgetElementsByClassr   Measurer]   r^   measurer   r   r0   )r   rU   re   measures
maxMeasurerg   ri   r   s           r   x_testPlayOneMeasureAtATimez(TestExternal.x_testPlayOneMeasureAtATime   s    " !LL#66v~~FG
z"AOOAIIaL) #!_G!BKGGI  r   c                4   ddl m ddlfdfd} G d d      } |       }        }t        |      }|j	                         |_        |j                  dkD  r9d|_        |j                  |j
                  |||gd	
       |j                  dkD  r8yy)zf
        doesn't work -- no matter what there's always at least a small lag, even with queues
        r   )noteNc                    t        j                         } t        d      D ]:  }j                         }j	                  dd      |_        | j                  |       < j                         }| j                  |       | S )N   0   H   )r   Streamr]   Noter_   psr^   )srg   rh   lastNr   r[   s       r   getRandomStreamz8TestExternal.x_testPlayRealTime.<locals>.getRandomStream  sa    A1XIIK~~b"-  IIKEHHUOHr   c                   | d   }| d   }|j                   j                  j                  j                         }|dcxk  r|j                  k  rn n|xj
                  dz  c_        |j
                  dkD  rb        |_        |j                         |_        |j                   j                  j                  j                  |j                         ||_        y y ||_        y )Nr      rv   )
r   r   r<   get_poslastPosrm   r   r-   storedIOFilequeue)rp   rx   streamPlayer
currentPosr   s       r   restoreListz4TestExternal.x_testPlayRealTime.<locals>.restoreList  s    "1+K#A;L%,,2288@@BJC6;#6#66!!Q&!$$q(,;,=L)/;/R/R/TK, ''--3399+:R:RS*4K' ) '1#r   c                      e Zd ZdZdZdZy)3TestExternal.x_testPlayRealTime.<locals>.TimePlayerF   r9   N)r   r   r   readyrm   r   r   r   r   
TimePlayerr     s    EEGr   r   FrY   rw   )	rZ   r   r[   r   r-   r   rm   r   r.   )	r   r   r   rx   re   ri   r   r   r[   s	         @@@r   x_testPlayRealTimezTestExternal.x_testPlayRealTime   s    
 	!			1	 	
 !l!_#%#<#<#> !# %K 8 8-8*5r):57   9 !#r   )r   r   r   r   loaderpygame_installedunittest
skipUnlessrj   ry   r   r   r   r   r   rR   rR      sR    x F X)+DE
 F
,\629r   rR   __main__)rM   
__future__r   importlib.utilr   ior   r   rZ   r   music21.exceptions21r   r   music21.midir	   r3   r   r   TestCaserP   rR   r   mainTestr   r   r   <module>r      s   
 # $    1  3	, 	E' E'P	8 	y98$$ y9x zG\" r   