
    Mg                        S SK Jr  S SKrS SKrS SKrS SKJrJrJr  S SK	J
r
  S SKJr  S SKrS SKrS SKrS/r S SKrS SKr\" S\S	9\" S
5      \" S5       " S S5      5       5       5       rg! \ a    \R+                  S5         NDf = f)    )BytesION)pluginrequirealias)Fore)Shazamffmpegz@voice_control_requirements (install portaudio + re-run setup.sh)T)networknativeshazamzmusic recognitionc                   T    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rSrg)MusicRecognition   z-A tool to recognize music through Shazam API.c                     [         R                  " 5       U l        S U l        [	        5       U l        [        R                  " 5       U l        SU l	        g )N   )
tempfileNamedTemporaryFilesound_recordedselected_microphoner   r   sr
Recognizer
recognizerduration)selfs    G/Users/admin/workspace/ai/Jarvis/jarviscli/plugins/music_recognition.py__init__MusicRecognition.__init__   s8    &99;#' h--/    c                 n    [         R                  " 5       nUR                  U R                  U5      5        g )N)asyncioget_event_looprun_until_completemusic_recognition)r   jarvissloops       r   __call__MusicRecognition.__call__"   s)    %%' 6 6v >?r   c                   #    UR                  S5        UR                  S5        UR                  S5         U R                  U5        UR                  S5      nUR                  5       nUS:X  d  US:X  d  US:X  a!  UR                  S[        R
                  5        gUS	:X  a  U R                  U5        O2US
:X  a  U R                  U5      I Sh  vN   OUR                  5         M  M   N7f)z]Main function.

This function is the main menu, that will run
until the user says otherwise.
 z(This tool will let you recognize a musicz]To achieve this, a 15 seconds recording of your microphone is done and sent to Shazam serversYour choice: qquit3zSee you next time :D12N)	sayavailable_optionsinputlowerr   CYANselect_microphonerecognize_musicincorrect_optionr   r$   
user_inputs      r   r#   "MusicRecognition.music_recognition&   s      	

2

=>

 5 	6 ""6*o6J#))+JS J&$8J#<M

1499= S &&v. s"**6222 '')+   3s   CC*C(C*c                     UR                  S5        UR                  S5        UR                  S5        UR                  S5        g)zR
Message displayed to prompt the user about actions of 
music recognition plugin.
%
Select one of the following options:z!1: Select input microphone to usez2: Record and recognize musicz3: QuitNr1   r   r$   s     r   r2   "MusicRecognition.available_optionsI   s7     	

;<

67

23

9r   c                     UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        g)zQ
Message displayed to prompt the user about actions 
after microphone recording.
r=   z1: Play recorded soundz/2: Use the recorded sound for music recognitionz3: Record againz4: QuitNr>   r?   s     r   after_recording_options(MusicRecognition.after_recording_optionsT   sD     	

;<

+,

DE

$%

9r   c                 x   [         R                  R                  5       nU(       d!  UR                  S[        R
                  5        gUR                  S5        [        U5       H  u  p4UR                  US-    SU 35        M      UR                  S[        S[        U5      S9nUS-
  U l
        X R                     $ )z+Select input device from the available onesz$There are no input devices availableNz1
Select one of following available input devices:   z: r+   )promptrtyperminrmax)r   
Microphonelist_microphone_namesr1   r   RED	enumerateinput_numberintlenr   )r   r$   input_devicesivalselected_inputs         r   r6   "MusicRecognition.select_microphone`   s     ;;=JJ=txxH

GH.FAJJ!A#b' /  ,,"]#	 - 
 $2!#3 5566r   c                   #    U R                   R                  U R                  R                  5      I Sh  vN nSU;  a!  UR	                  S[
        R                  5        gUR	                  S[
        R                  5        SUS   ;   at  SUS   S   ;   ah   [        R                  R                  US   S   S   5      n[        UR                  5       5      n[        R                  " USS9nUR	                  U5        UR	                  S	[#        US   S
   5       3[
        R                  5        UR	                  S[#        US   S   5       3[
        R                  5        g GN.! [        R                  R                    a     Nf = f7f)zU
Get music match from Shazam servers
If available, draw a pixel art of the cover art
NtrackzNo match foundzMatch found:imagescoverart2   )widthzSong Title: titlezSong Artist: subtitle)r   recognize_songr   namer1   r   rL   GREENurllibrequesturlopenr   readclimageconverterrorURLErrorstr)r   r$   outrb   downloaded_image	pixel_arts         r   get_shazam_info MusicRecognition.get_shazam_infoy   sC     KK..t/B/B/G/GHH#~JJ'2JJ~tzz23w<'J#g,x:P,P$nn44GX.z:<G'.w||~'>$ '0@ KIJJy) JJc#g,w*?&@%ABDJJOJJCL$< =>?M! I ||,, s6   3F
E#A F
A'E& >A&F
&FF
FF
c                     UR                  S[        R                  5        [        R                  R                  U R                  SS9n[        R                  R                  U5        g)zPlay the last recorded soundz)-----Now playing last recorded sound-----mp3formatN)	r1   r   BLUEpydubAudioSegment	from_filer   playbackplay)r   r$   mp3_files      r   play_last_recorded_sound)MusicRecognition.play_last_recorded_sound   sO     	

>		J%%// 0 /H%r   c                   #    U R                  U5      (       d  g U R                  U5        UR                  S5      nUR                  5       nUS:X  d  US:X  d  US:X  a  gUS:X  a  U R	                  U5        OJUS:X  a  U R                  U5      I Sh  vN   O*US:X  a  U R                  U5        OUR                  5         M  M   N17f)	z-Function that does the music recognition flowNr+   r,   r-   4r/   r0   r.   )record_microphonerB   r3   r4   rz   rm   r8   r9   s      r   r7    MusicRecognition.recognize_music   s      %%f--((0o6J#))+JS J&$8J#<M S --f5 s"**6222 s"&&v. '')1  3s   BCC2Cc                 p   U R                   c7  UR                  S[        R                  5        U R	                  U5      (       d  g[
        R                  " U R                   S9nU nU R                  R                  5         UR                  S[        R                  5        U R                  R                  X0R                  S9n[        R                  R                  [!        UR#                  5       5      SS9nUR%                  U R                  SS	9  U R                  sSSS5        $ ! , (       d  f       g= f)
z&Record microphone input for 15 secondsNzMicrophone not yet selected...)device_indexz&-----Now Recording for 15 seconds-----)sourcer   wav)filerr   rp   rq   )r   r1   r   rL   r6   r   rJ   r   flushrs   r   recordr   rt   ru   rv   r   get_wav_dataexport)r   r$   my_micr   audio_recordedwav_files         r   r~   "MusicRecognition.record_microphone   s     ##+JJ7B))&11D,D,DEv%%'JJ?K!__33 4 7N ))33^88:;E 4 KH OOD//O>&& VVs   %B8D''
D5)r   r   r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   r'   r#   r2   rB   r6   rm   rz   r7   r~   __static_attributes__ r   r   r   r      s=     8@!F	
72M2&@'r   r   )ior   r   urllib.errorra   urllib.requestr   r   r   coloramar   shazamior   rt   r    re   requirementspyaudiospeech_recognitionr   ImportErrorappendr   r   r   r   <module>r      s        ) )     zL# 	l+xz' z'   ,z'  LJLLs   A, ,BB