
    3jW                        d dl mZ d dlZd dlmZ d dlmZ  ej                  d      Z	 	 ddZddZ	 G d d	ej                        Zed
k(  rd dlZ ej                  e       yy)    )annotationsN)environment)scalezaudioSearch.transcriberc                   ddl m} |t        j                  d      }|du rt        j                         dz  }n	|du rd}n|}|du r|j                  |t        |            }n|j                  t        |      	      }|j                  ||      }	|j                  |	      }	|j                  |	|      \  }
}|j                  |
      \  }}|j                  ||d
      \  }}| r|j                          |r:	 ddl}|j$                  j'                  |       |j$                  j                          t        j)                  d       |S # t         $ r |j#                  d      w xY w)a  
    runs all the methods to record from audio for `seconds` length (default 10.0)
    and transcribe the resulting melody returning a music21.Score object

    if `show` is True, show the stream.

    if `plot` is True, then a Tk graph of the frequencies will be displayed.

    if `useMic` is True then use the microphone.  If False it will load the file of `saveFile`
    or the default temp file to run transcriptions from.

    a different scale than the chromatic scale can be specified by setting `useScale`.
    See :ref:`moduleScale` for a list of allowable scales. (or a custom one can be given).
    Microtonal scales are totally accepted, as are retuned scales where A != 440hz.

    if `saveFile` is False, then the recorded audio is saved to disk.  If
    set to `True` then `environLocal.getRootTempDir() / 'ex.wav'` is
    used as the filename.  If set to anything else then it will use that as the
    filename.
    r   audioSearchNC4Tzex.wavF)lengthstoreWaveFilenamewaveFilenameremoveRestsAtBeginningz)Cannot plot without matplotlib installed.z* END)music21r   r   ChromaticScaleenvironLocalgetRootTempDirgetFrequenciesFromMicrophonestrgetFrequenciesFromAudioFiledetectPitchFrequenciessmoothFrequenciespitchFrequenciesToObjectsjoinConsecutiveIdenticalPitchesnotesAndDurationsToStreamshowmatplotlib.pyplotImportErrorAudioSearchExceptionpyplotplot
printDebug)r   r!   useMicsecondsuseScalesaveFileaudioSearchBaser   freqFromAQListdetectedPitchesFreqdetectedPitchObjectslistplot	notesListdurationListmyScoreunused_length_part
matplotlibs                    L/DATA/.local/lib/python3.12/site-packages/music21/audioSearch/transcriber.pyrunTranscriber2      s   , 7''-4#224x?	U	 ~(EE!,/ F 1 )DD\* E , *@@QYZ);;<OP#==>QS[\'GGH\]Y"1"K"K# #L #%G
 	=$ 	x( G$N  	=!66;= =	=s   7E E"c                ,   ddl m} |j                  |       }|j                  ||      }|j	                  |      }|j                  ||      \  }}|j                  |      \  }}|j                  ||d      \  }	}
|	j                  j                         S )a|  
    Reads in a .wav file and returns a stream representing the transcribed, monophonic audio.

    `fileName` should be the complete path to a file on the disk.

    A different scale than the chromatic scale can be specified by setting `useScale`.
    See :ref:`moduleScale` for a list of allowable scales. (or a custom one can be given).
    Microtonal scales are totally accepted, as are retuned scales where A != 440hz.

    We demonstrate with an audio file beginning with an ascending scale.

    >>> import os #_DOCS_HIDE
    >>> taw = 'test_audio.wav' #_DOCS_HIDE
    >>> waveFile = str(common.getSourceFilePath() / 'audioSearch' / taw) #_DOCS_HIDE
    >>> #_DOCS_SHOW waveFile = 'test_audio.wav'
    >>> p = audioSearch.transcriber.monophonicStreamFromFile(waveFile)
    >>> p
    <music21.stream.Part ...>
    >>> p.show('text')
    {0.0} <music21.note.Note C>
    {0.25} <music21.note.Note C>
    {0.75} <music21.note.Note D>
    {1.75} <music21.note.Note E>
    {2.75} <music21.note.Note F>
    {4.25} <music21.note.Note G>
    {5.25} <music21.note.Note A>
    {6.25} <music21.note.Note B>
    {7.25} <music21.note.Note C>
    ...
    r   r   r   Tr   )
r   r   r   r   r   r   r   r   partsfirst)fileNamer%   r'   r(   r)   r*   unused_listplotr,   r-   r.   r/   s              r1   monophonicStreamFromFiler8   ]   s    > 7$@@h@WN)@@QYZ);;<OP*DDEXZbc'GGH\]Y"1"K"K< #L #>G==      c                      e Zd Zd Zd Zy)TestExternalc                P    t         j                         dz  }t        dd|d       y )Nznew_song.wavFg      $@)r   r!   r&   r$   r   r   r2   selfr&   s     r1   x_testRunTranscribez TestExternal.x_testRunTranscribe   s#    ..0>A5uxNr9   c                P    t         j                         dz  }t        d|dd       y )Nzpachelbel.wavF)r#   r&   r!   r   r=   r>   s     r1   x_testTranscribePachelbelz&TestExternal.x_testTranscribePachelbel   s#    ..0?BUXENr9   N)__name__
__module____qualname__r@   rB    r9   r1   r;   r;      s    OOr9   r;   __main__)TTTg      4@NT)N)
__future__r   unittestr   r   r   Environmentr   r2   r8   TestCaser;   rC   mainTestrF   r9   r1   <module>rM      sq    #   &{&&'@A 048<DN+!\O8$$ O zG\" r9   