
    rh+                       S r SSKJr  SSKJr  SSKJr  SSKrSSKJ	r	  SSK
Jr  SSKJr  SS	KJr   " S
 S\5      r " S S5      r " S S\R$                  5      r " S S\R$                  5      r\S:X  a  SSKr\R,                  " \5        gg)aq  
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: pip3 install pygame
    )annotations)	find_spec)BytesION)defaults)Music21Exception)stream)	translatec                      \ rS rSrSrg)StreamPlayerException#    N__name__
__module____qualname____firstlineno____static_attributes__r       O/home/james-whalen/.local/lib/python3.13/site-packages/music21/midi/realtime.pyr   r   #       r   r   c                      \ rS rSrSrSr     S           SS jjr     S\" S5      SS.S	 jjrS
 r	  S\" S5      SS.S jjr
S rSrg)StreamPlayer'   a  
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                     SS K nXpl         SS KnUR                  U l        U R                  SL d  U(       a  UR                  R                  X4XV5        Xl	        g ! [         a    [        S5      ef = f)Nr   z,StreamPlayer requires pygame.  Install firstF)
pygamepygame.exceptions
exceptionspygame_exceptionsImportErrorr   mixerInitializedmixerinitstreamIn)selfr#   reinitMixer	mixerFreqmixerBitSizemixerChannelsmixerBufferr   s           r   __init__StreamPlayer.__init__M   sk    	X K$%+%6%6D"   E)[LLi}R   	X'(VWW	Xs   A A0NinfT)playForMillisecondsblockedc               P    U R                  5       nU R                  UUUUUUUUS9  g)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$   r0   r1   r2   r3   r4   r-   r.   streamStringIOFiles	            r   playStreamPlayer.playd   s>    * "88:0+7'/*5&-3G2E&- 	 	/r   c                x    [         R                  " U R                  5      nUR                  5       n[	        U5      $ N)midiTranslatestreamToMidiFiler#   writestrr   )r$   streamMidiFilestreamMidiWrittens      r   r5   #StreamPlayer.getStringOrBytesIOFile   s1    &77F*335())r   c                  U R                   R                  R                  5       n	 U R                   R                  R                  R                  U5        U R                   R                  R                  R                  5         U(       d  g[        SU-  5      nU R                   R                  R                  5       nU R                   R                  R                  R                  5       (       a  Ub  U" U5        U R                   R                  R                  5       U-
  U:  a/  U R                   R                  R                  R                  5         OFU	R                  U5        U R                   R                  R                  R                  5       (       a  M  Ub	  U" U5        gg! U R                  R                   a1  n
[        SU S3U R                  R                  5        -   5      U
eSn
A
ff = f)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loadr   PygameErrorr   	get_errorr8   int	get_ticksget_busystoptick)r$   stringIOFiler0   r1   r2   r3   r4   r-   r.   pygameClockpge	framerate
start_times                r   r6   StreamPlayer.playStringIOFile   s     kk&&,,.	KK##((6 	$$&334	[[%%//1
kk%%..00'X&{{))+j8;NN!!'',,.Y' kk%%..00 "  #% %%11 	',\N*E++5578: 	s   /F( (G3,G..G3c                `    U R                   R                  R                  R                  5         g r;   )r   r!   rF   rM   )r$   s    r   rM   StreamPlayer.stop   s    $$&r   )r   r   r#   )FiD  i   i   )r#   zstream.Streamr%   boolr&   rJ   r'   rJ   r(   rJ   r)   rJ   )NNNN2   )r   r   r   r   __doc__r    r*   floatr8   r5   r6   rM   r   r   r   r   r   r   '   s    "F 
 "!! ! 	!
 ! ! !0 "$/ "'u/>*
 JNNP&! .35\4&!P'r   r   c                      \ rS rSrSrg)Test   r   Nr   r   r   r   r]   r]      r   r   r]   c                  x    \ rS rSr\" S5      r\b  SrOSr\R                  " \S5      S 5       r	S r
S	 rS
 rSrg)TestExternal   r   NTFzpygame is not installedc                j   SSK Jn  SS KnUR                  S5      n/ n[	        S5       H$  nUR                  UR                  SS5      5        M&     UR                  5       R                   H*  nXFR                  R                     UR                  l        M,     [        U5      nUR                  5         g )Nr   corpusbwv66.6      )music21rd   randomparserangeappendrandintrecursenotespitchmidi	microtoner   r8   )r$   rd   rj   b	keyDetuneinsps           r   testBachDetuneTestExternal.testBachDetune   s    "LL#	sAV^^C45 ""A )'',, 7AGG #!_
	r   c                   SSK Jn  SSKnS n " S S5      nU" 5       nSUl        UR	                  S5      n/ n[        S	5       H$  nUR                  UR                  S
S5      5        M&     UR                  5       R                   H*  n	XyR                  R                     U	R                  l        M,     [        U5      n
U
R                  X5/SS9  g)z>
tests to see if the busyCallback function is called properly
r   rc   Nc                ~    U S   nU=R                   UR                  -  sl         [        SUR                    S35        g )Nr   zhi! waited z milliseconds)times
updateTimeprint)timeListtimeCounter_inners     r   busyCounter4TestExternal.x_testBusyCallback.<locals>.busyCounter   s=     (##'8'C'CC#K 1 7 78FGr   c                      \ rS rSrSrSrg)-TestExternal.x_testBusyCallback.<locals>.Mock   r   r   N)r   r   r   r   r}   r   r   r   r   Mockr      s    Er   r     zbach/bwv66.6rf   rg   rh   r0   r1   r4   )ri   rd   rj   r~   rk   rl   rm   rn   ro   rp   rq   rr   rs   r   r8   )r$   rd   rj   r   r   timeCounterrt   ru   rv   rw   rx   s              r   x_testBusyCallbackTestExternal.x_testBusyCallback   s    
 	#	H
	 	 f!$LL(	sAV^^C45 ""A )'',, 7AGG #!_
[=WZ[r   c                n   SSK Jn  S[        l        UR	                  S5      n/ n[        UR                  S   R                  [        R                  5      5      n[        U5       H#  nUR                  UR                  U5      5        M%     [        U5      nU H  nXvl        UR                  5         M     g )Nr   rc   re   )ri   rd   r   ticksAtStartrk   lenpartsgetElementsByClassr   Measurerl   rm   measurer   r#   r8   )r$   rd   rt   measures
maxMeasurerv   rx   r   s           r   x_testPlayOneMeasureAtATime(TestExternal.x_testPlayOneMeasureAtATime   s    " !LL#66v~~FG
z"AOOAIIaL) #!_G!KGGI  r   c                6  ^^^ SSK Jm  SSKmUU4S jmU4S jn " S S5      nU" 5       nT" 5       n[        U5      nUR	                  5       Ul        UR                  S:  a7  SUl        UR                  UR
                  UX5/S	S
9  UR                  S:  a  M6  gg)zV
doesn't work -- no matter what there's always at least a small lag, even with queues
r   )noteNc                   > [         R                  " 5       n [        S5       H;  nTR                  5       nTR	                  SS5      Ul        U R                  U5        M=     TR                  5       nU R                  U5        U $ )N   0   H   )r   Streamrl   Notern   psrm   )srv   rw   lastNr   rj   s       r   getRandomStream8TestExternal.x_testPlayRealTime.<locals>.getRandomStream  sa    A1XIIK~~b"-  IIKEHHUOHr   c                  > U S   nU S   nUR                   R                  R                  R                  5       nUSs=:  a  UR                  ::  a  O  OU=R
                  S-  sl        UR
                  S:  aa  T" 5       Ul        UR                  5       Ul        UR                   R                  R                  R                  UR                  5        X1l        g g X1l        g )Nr      r   )
r   r!   rF   get_poslastPosr}   r#   r5   storedIOFilequeue)r   r   streamPlayer
currentPosr   s       r   restoreList4TestExternal.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*4' ) '1#r   c                       \ rS rSrSrSrSrSrg)3TestExternal.x_testPlayRealTime.<locals>.TimePlayeri#  F   rC   r   N)r   r   r   r   readyr}   r   r   r   r   r   
TimePlayerr   #  s    EEGr   r   Frh   r   )	ri   r   rj   r   r5   r   r}   r   r6   )	r$   r   r   r   rt   rx   r   r   rj   s	         @@@r   x_testPlayRealTimeTestExternal.x_testPlayRealTime   s    
 	!			1	 	
 !l!_#%#<#<#> !# %K 8 8-8*5):57   9 !#r   r   )r   r   r   r   r   loaderpygame_installedunittest
skipUnlessry   r   r   r   r   r   r   r   r`   r`      sP    x F )+DE
 F
,\829r   r`   __main__)rZ   
__future__r   importlib.utilr   ior   r   ri   r   music21.exceptions21r   r   music21.midir	   r<   r   r   TestCaser]   r`   r   mainTestr   r   r   <module>r      s   
 # $    1  3	, 	J' J'Z	8 	z98$$ z9z z\" r   