
    3j>                    ^   U d Z ddlmZ ddlmZ ddlZddlZddlmZ ddl	m
Z
 ddl	mZ  e
j                  d      Zd	Zd
ZdZdddeeefdZ G d dej$                        Z G d dej(                        Z G d dej(                        Zg Zded<   edk(  rddl	Z	 e	j4                  e       yy)a  
modules for audio searching that directly record from the microphone.

Requires PyAudio and portaudio to be installed (https://portaudio.com/download.html)

Windows users will get pyaudio and portaudio with `pip install pyaudio`

macOS users should have Homebrew installed and run `brew install portaudio`
before running `pip install pyaudio`

There is no official support for Linux/BSD etc. in music21, but package managers like `apt`
tend to have libraries like `portaudio19` and `python3-pyaudio`.
    )annotations)	find_specN)DocOrder)environment)exceptions21zaudioSearch.recording   iD  i   g      $@Tc                J   ddl }|j                  }||}||j                  k(  rt        d      |j	                         }|j                  |||d|      }	t        |t        |       z  |z        }
g }t        |
      D ]$  }|	j                  |      }|j                  |       & |	j                          |j                          |durt        |t              r|}n t        t        j!                         dz        }dj#                  |      }	 t%        j
                  |d	      }|j'                  |       |j)                  |j+                  |             |j-                  |       |j/                  |       |j                          |S |S # t0        $ r t        d
| d      w xY w)z
    records `seconds` length of sound in the given format (default Wave)
    and optionally stores it to disk using the filename of `storeFile`

    Returns a list of samples.
    r   Nz4cannot perform samplesFromRecording on 8-bit samplesT)formatchannelsrateinputframes_per_bufferFzrecordingTemp.wav    wbzCannot open z for writing.)pyaudiopaInt16paInt8RecordingExceptionPyAudioopenintfloatrangereadappendclose	terminate
isinstancestrenvironLocalgetRootTempDirjoinwavesetnchannelssetsampwidthget_sample_sizesetframeratewriteframesIOError)seconds	storeFilerecordFormatrecordChannelsrecordSampleRaterecordChunkLengthr   recordFormatDefaultp_audiostrecordingLengthstoredWaveSampleListidatawaveFilenamewfs                   J/DATA/.local/lib/python3.12/site-packages/music21/audioSearch/recording.pysamplesFromRecordingr:   )   s    !//*w~~% !WXXooG	\-+ (9	 
 
;B *U7^;>OOPO ?#ww()##D) $ HHJi%$L|::<?RRSLxx,-		Q<.BOON+OOG33LABOO,-NN4 HHJ    	Q$|L>%OPP	Qs   A9F	 	F"c                      e Zd Zy)r   N__name__
__module____qualname__ r   r9   r   r   d       r   r   c                      e Zd Zy)TestNr<   r@   r   r9   rC   rC   i   rA   r   rC   c                  \    e Zd Z ed      ZedZndZ ej                  ed      d        Zy)TestExternalr   NTFzpyaudio must be installedc                :    t        dd      }t        |dd        y)z@
        record one second of data and print 10 records
        r   F)r*   r+      (   N)r:   print)self
sampleLists     r9   testRecordingzTestExternal.testRecordings   s    
 *!uE
jB r   )	r=   r>   r?   r   loaderpyaudio_installedunittest
skipUnlessrL   r@   r   r9   rE   rE   l   sB    y!F !X*,GH! I!r   rE   r   
_DOC_ORDER__main__)__doc__
__future__r   importlib.utilr   rO   r#   music21.common.typesr   music21r   r   Environmentr    default_recordChannelsdefault_recordSampleRatedefault_recordChunkLengthr:   Music21Exceptionr   TestCaserC   rE   rQ   __annotations__r=   mainTestr@   r   r9   <module>r`      s    # $   )   &{&&'>?      "&&*(>*B+D	8 v	66 	
	8 	!8$$ !$ 
H  zGT r   