
    3jW                        d dl mZ d dl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	  ej                  d      Z G d	 d
      Z G d dej                        Zedk(  rd dlZ ej                   e       yy)    )annotationsN)time)environment)scale)search)streamzaudioSearch.scoreFollowerc                  H    e Zd Zd
dZ	 	 	 	 ddZd Zd Zd Zd Zd Z	d	 Z
y)ScoreFollowerNc                   || _         |.|j                         j                  j                         | _        nd | _        t        t        j                         dz        | _        d| _	        d| _
        d| _        d| _	        d| _        d| _        d| _        d| _        d | _        d | _        d | _        d| _        d| _        d| _        d| _        d | _        d | _        d | _        d | _        d | _        d | _        y )NzscoreFollowerTemp.wavr   F   T)scoreStreamflattennotesAndRestsr   scoreNotesOnlystrenvironLocalgetRootTempDirwaveFilelastNotePositioncurrentSample	totalFilestartSearchAtSlotpredictedNotePosition	countdownEND_OF_SCOREqlefirstNotePagelastNotePage	firstSlotsilencePeriodCounternotesCounterbeginsuseScalesilencePeriodresultuseMicprocessing_timeseconds_recording)selfr   s     N/DATA/.local/lib/python3.12/site-packages/music21/audioSearch/scoreFollower.py__init__zScoreFollower.__init__   s    &""-"5"5"7"E"E"L"L"ND"&DL779<SST ! !!"%&"!! $%!!#!%    c                    |t        j                  d      }|| _        || _        || _        d| _        | j
                  du r$| j                         | _        | j
                  du r$t        j                  d       y)z
        The main program. It runs the 'repeatTranscription' until the
        performance ends.

        If `useScale` is none, then it uses a scale.ChromaticScale
        NC4Fz* END)	r   ChromaticScaler(   r&   r#   r%   repeatTranscriptionr   
printDebug)r)   plotr&   secondsr#   s        r*   runScoreFollowerzScoreFollower.runScoreFollower9   sq     ++D1H!( kkU"224DK kkU" 	(r,   c                   ddl m} t        j                  d       | j                  du r|j                  | j                  d      }nq|j                  } || j                  | j                  | j                        \  }| _        | _	        | j                  dk(  r| j                  j                         | _
        t        j                  d       t               }|j                  || j                        }|j                  |      }|j!                  || j                        \  }}|j#                  |      \  }}	| j%                  |       t        j                  d	       | j&                  | j(                  | j(                  t+        |      z    }
t-        j.                  |
      }|j1                  ||	|| j2                  
      \  }| _        | j5                  | j&                  || j6                  | j(                        \  }| _        }}t               |z
  | _        t        j                  d       |du rd}|S | j;                  |||      }| j                  du r>|j                  } || j                  | j8                  | j                        \  }}| _	        | j(                  t+        | j<                        kD  rd}n)| j                  du r| j                  | j                  k\  rd}t        j                  d| d       |S )ag  
        First, it records from the microphone (or from a file if is used for
        test). Later, it processes the signal to detect the pitches.
        It converts them into music21 objects and compares them with the score.
        It finds the best matching position of the recorded signal with the
        score, and decides, depending on matching accuracy, the last note
        predicted and some other parameters, in which position the recorded
        signal is.

        It returns a value that is False if the song has not finished, or true
        if there has been a problem like some consecutive bad matches or the
        score has finished.

        >>> from music21.audioSearch import scoreFollower
        >>> scoreNotes = ' '.join(['c4', 'd', 'e', 'f', 'g', 'a', 'b', "c'", 'c', 'e',
        ...     'g', "c'", 'a', 'f', 'd', 'c#', 'd#', 'f#', 'c', 'e', 'g', "c'",
        ...     'a', 'f', 'd', 'c#', 'd#', 'f#'])
        >>> scNotes = converter.parse('tinynotation: 4/4 ' + scoreNotes, makeNotation=False)
        >>> ScF = scoreFollower.ScoreFollower(scoreStream=scNotes)
        >>> ScF.useMic = False
        >>> import os #_DOCS_HIDE
        >>> ScF.waveFile = str(common.getSourceFilePath() #_DOCS_HIDE
        ...                 / 'audioSearch' / 'test_audio.wav') #_DOCS_HIDE
        >>> #_DOCS_SHOW ScF.waveFile = 'test_audio.wav'
        >>> ScF.seconds_recording = 10
        >>> ScF.useScale = scale.ChromaticScale('C4')
        >>> ScF.currentSample = 0
        >>> exitType = ScF.repeatTranscription()
        >>> print(exitType)
        False
        >>> print(ScF.lastNotePosition)
        10

        r   audioSearchzrepeat transcription startingTN)lengthstoreWaveFilename)r8   startSamplezgot Frequencies from Microphonezmade it to here.)scNotesr   zand even to here.
endOfScoreFfinishedPerformingwaveFileEOFzabout to return -- exitType:  )music21r7   r   r1   r&   getFrequenciesFromMicrophoner(   "getFrequenciesFromPartialAudioFiler   r   r   
getnframesr   detectPitchFrequenciesr#   smoothFrequenciespitchFrequenciesToObjectsjoinConsecutiveIdenticalPitchessilencePeriodDetectionr   r   lenr   PartnotesAndDurationsToStreamr   matchingNotesr   r'   updatePositionr   )r)   r7   freqFromAQListgetFreqFunc
time_startdetectedPitchesFreqdetectedPitchObjectsunused_plot	notesListdurationListexcerptr;   transcribedScoretotalLengthPeriodprobr   exitTypejunks                     r*   r0   z!ScoreFollower.repeatTranscriptionZ   s   F 	( 	 ?@;;$(EE--"& F N
 &HHK@K-- ..A=NDM4+=
 ~~"!%!9!9!; ABV
)@@QUQ^Q^_);;<OP,7,Q,Q-0)k"-"M"M #"	<##I. 23""4#8#89N9NQTU^Q_9_`++g&%0%J%J	 &K &
"$( HLGYGY""!!	H
D40$  $v
2 344#HO &&t->
K;;%%HHK7B++ ..84ND$"4   3t':':#;;+H[[E!d&8&8DNN&J$H"?z KLr,   c                    d}|D ]  }|j                   dk7  sd} |du r$d| _        d| _        | xj                  dz  c_        yd| _        | xj                  dz  c_        d| _        y)a  
        Detection of consecutive periods of silence.
        Useful if the musician has some consecutive measures of silence.

        >>> from music21.audioSearch import scoreFollower
        >>> scNotes = corpus.parse('luca/gloria').parts[0].flatten().notes.stream()
        >>> ScF = scoreFollower.ScoreFollower(scoreStream=scNotes)
        >>> notesList = []
        >>> notesList.append(note.Rest())
        >>> ScF.notesCounter = 3
        >>> ScF.silencePeriodCounter = 0
        >>> ScF.silencePeriodDetection(notesList)
        >>> ScF.notesCounter
        0
        >>> ScF.silencePeriodCounter
        1

        >>> ScF = scoreFollower.ScoreFollower(scoreStream=scNotes)
        >>> notesList = []
        >>> notesList.append(note.Rest())
        >>> notesList.append(note.Note())
        >>> ScF.notesCounter = 1
        >>> ScF.silencePeriodCounter = 3
        >>> ScF.silencePeriodDetection(notesList)
        >>> ScF.notesCounter
        2
        >>> ScF.silencePeriodCounter
        0
        TrestFr   r   N)namer$   r!   r    )r)   rT   	onlyRestsis       r*   rH   z$ScoreFollower.silencePeriodDetection   sp    < 	Avv!	  !%D !D%%*%!&D"()D%r,   c                L   d}| j                   sS| j                  dk(  r7| j                  | _        t	               |z
  }| j                  ||      | _        |S | j                  dk(  r6dt	               |z
  z  | j                  z   }| j                  ||      | _        |S | j                  dk(  r6dt	               |z
  z  | j                  z   }| j                  ||      | _        |S | j                  dk(  r5| j                  | _        | j                  | _        | j                  | _        |S | j                  dk(  rd| _        d| _        d| _        |S t        j                  d       d}|S |d	k  r$d| _        d| _        t        j                  d
       nd| _         | j                  dk\  rd}|S )ah  
        It updates the position in which the scoreFollower starts to search at,
        and the predicted position in which the new fragment of the score
        should start.  It updates these positions taking into account the value
        of the "countdown", and if is the beginning of the song or not.

        It returns the exitType, which determines whether the scoreFollower has
        to stop (and why) or not.

        See example of a bad prediction at the beginning of the song:

        >>> from time import time
        >>> from music21.audioSearch import scoreFollower
        >>> scNotes = corpus.parse('luca/gloria').parts[0].flatten().notes.stream()
        >>> ScF = scoreFollower.ScoreFollower(scoreStream=scNotes)
        >>> ScF.begins = True
        >>> ScF.startSearchAtSlot = 15
        >>> ScF.countdown = 0
        >>> prob = 0.5  # bad prediction
        >>> totalLengthPeriod = 15
        >>> time_start = time()
        >>> exitType = ScF.updatePosition(prob, totalLengthPeriod, time_start)
        >>> print(ScF.startSearchAtSlot)
        0

        Different examples for different countdowns:

        Countdown = 0:

        The last matching was good, so it calculates the position in which it
        starts to search at, and the position in which the music should start.

        >>> ScF = scoreFollower.ScoreFollower(scoreStream=scNotes)
        >>> ScF.scoreNotesOnly = scNotes.flatten().notesAndRests
        >>> ScF.begins = False
        >>> ScF.countdown = 0
        >>> ScF.startSearchAtSlot = 15
        >>> ScF.lastNotePosition = 38
        >>> ScF.predictedNotePosition = 19
        >>> ScF.seconds_recording = 10
        >>> prob = 0.8
        >>> totalLengthPeriod = 25
        >>> time_start = time()
        >>> exitType = ScF.updatePosition(prob, totalLengthPeriod, time_start)
        >>> print(ScF.startSearchAtSlot)
        38

        >>> ScF.predictedNotePosition >=38
        True

        Countdown = 1:

        Now it doesn't change the slot in which it starts to search at.
        It also predicts the position in which the music should start.

        >>> ScF = scoreFollower.ScoreFollower(scoreStream=scNotes)
        >>> ScF.begins = False
        >>> ScF.countdown = 1
        >>> ScF.startSearchAtSlot = 15
        >>> ScF.lastNotePosition = 15
        >>> ScF.predictedNotePosition = 19
        >>> ScF.seconds_recording = 10
        >>> prob = 0.8
        >>> totalLengthPeriod = 25
        >>> time_start = time()
        >>> exitType = ScF.updatePosition(prob, totalLengthPeriod, time_start)
        >>> print(ScF.startSearchAtSlot)
        15

        >>> ScF.predictedNotePosition > 15
        True

        Countdown = 2:

        Now it starts searching at the beginning of the page of the screen.
        The note prediction is also the beginning of the page.

        >>> ScF = scoreFollower.ScoreFollower(scoreStream=scNotes)
        >>> ScF.begins = False
        >>> ScF.countdown = 2
        >>> ScF.startSearchAtSlot = 15
        >>> ScF.lastNotePosition = 15
        >>> ScF.predictedNotePosition = 19
        >>> ScF.seconds_recording = 10
        >>> prob = 0.8
        >>> totalLengthPeriod = 25
        >>> time_start = time()
        >>> exitType = ScF.updatePosition(prob, totalLengthPeriod, time_start)
        >>> print(ScF.startSearchAtSlot)
        15

        >>> print(ScF.predictedNotePosition)
        39

        Countdown = 4:

        Now it starts searching at the beginning of the page of the screen.
        The note prediction is also the beginning of the page.

        >>> ScF = scoreFollower.ScoreFollower(scoreStream=scNotes)
        >>> ScF.begins = False
        >>> ScF.countdown = 4
        >>> ScF.startSearchAtSlot = 15
        >>> ScF.lastNotePosition = 15
        >>> ScF.predictedNotePosition = 19
        >>> ScF.seconds_recording = 10
        >>> prob = 0.8
        >>> totalLengthPeriod = 25
        >>> time_start = time()
        >>> exitType = ScF.updatePosition(prob, totalLengthPeriod, time_start)
        >>> print(ScF.startSearchAtSlot)
        0

        >>> print(ScF.predictedNotePosition)
        0

        Countdown = 5:

        Now it stops the program.

        >>> ScF = scoreFollower.ScoreFollower(scoreStream=scNotes)
        >>> ScF.begins = False
        >>> ScF.countdown = 5
        >>> ScF.startSearchAtSlot = 15
        >>> ScF.lastNotePosition = 15
        >>> ScF.predictedNotePosition = 19
        >>> ScF.seconds_recording = 10
        >>> prob = 0.8
        >>> totalLengthPeriod = 25
        >>> time_start = time()
        >>> exitType = ScF.updatePosition(prob, totalLengthPeriod, time_start)
        >>> print(exitType)
        countdownExceeded

        Fr   r            zCOUNTDOWN = 5countdownExceededgffffff?z!Silence or noise at the beginning   5consecutiveCountdownsBeginning)r"   r   r   r   r   predictNextNotePositionr   r(   r   r   r1   )r)   rY   rX   rP   rZ   r'   totalSecondss          r*   rM   zScoreFollower.updatePosition   s   P {{~~")-)>)>&"&&:"5-1-I-I%.8*J G 1$ DFZ$784;Q;QQ-1-I-I%|.5*@ = 1$ DFZ$784;Q;QQ-1-I-I%|.5*6 3 1$(,%)-&-1^^** ) 1$ )*%)*&-.*  ''8.  cz()%)*&''(KL#~~"<r,   c                     y)z
        Returns the index of the first element on the screen right now.

        Doesn't work. (maybe it's not necessary)
        r    )r)   s    r*   getFirstSlotOnScreenz"ScoreFollower.getFirstSlotOnScreen  s     r,   c                    ||z  | j                   z  }d}d}||k  r4|| j                  | j                  |z      j                  z   }|dz   }||k  r4t	        || j                  z         }|S )aF  
        It predicts the position in which the first note of the following
        recording note should start, taking into account the processing time of
        the computer.  It has two inputs: totalLengthPeriod, that is the number
        of pulses or beats in the recorded audio, and totalSeconds, that is the
        length in seconds of the processing time.

        It returns a number with the position of the predicted note in the
        score.

        >>> from time import time
        >>> from music21.audioSearch import scoreFollower
        >>> scNotes = corpus.parse('luca/gloria').parts[0].flatten().notes.stream()
        >>> ScF = scoreFollower.ScoreFollower(scoreStream=scNotes)
        >>> ScF.scoreNotesOnly = ScF.scoreStream.flatten().notesAndRests
        >>> ScF.lastNotePosition = 14
        >>> ScF.seconds_recording = 10.0
        >>> totalLengthPeriod = 8
        >>> totalSeconds = 2.0
        >>> predictedStartPosition = ScF.predictNextNotePosition(
        ...     totalLengthPeriod, totalSeconds)
        >>> print(predictedStartPosition)
        18

        r   r   )r(   r   r   quarterLengthint)r)   rX   ri   extraLengthmiddleRhythmslotspredictedStartingNotePositions          r*   rh   z%ScoreFollower.predictNextNotePosition  s    4 (,69O9OO[('$*=*=%%-+//<}=LAIE [( ),ED4I4I,I(J%,,r,   c           	     :   ddl m} t        t        |j	                         j
                              }g }g }g }	d}
t        t        j                  |dz              }t        t        j                  |dz              }|dk(  rd}nCt        t        j                  t        |      |z        t        j                  ||z        z
        }t        |      D ]v  }|||z  dz   ||z  |z   dz    }t        j                  |      }t        |      }|j                  ||z  dz          |	j                  |       ||_        |j                  |       x t        j                   |j	                         j
                  j                         |      }|t        |      |z
  |z
  dz
  kD  r+t        |      |z
  |z
  dz
  }d}
t"        j%                  d       |j'                  ||||| j(                  | j*                  | j,                        \  }| _        d}t        ||   j                        }| j.                  du r9| j0                  d	k  r*t"        j%                  d
       | xj(                  dz  c_        | j(                  dk7  rd}n||   j2                  }t        j4                  |j	                         j
                  j                         |      }t        j6                  |j	                         j
                  j                         |      }t        j8                  |j	                         j
                  j                         |      }t        t        ||               D ]  }|||   |   j:                  z   } | j(                  dk(  r| j0                  dk(  r||   |	|   z   }||||
fS )Nr   r6   Fg?rd   r   TzLAST PART OF THE SCORErf   zAll rest period)r@   r7   ro   rI   r   r   mathceilfloorranger   rJ   r   appendidr   approximateNoteSearchWeightedr   r1   decisionProcessr   r   r   r$   r    matchProbabilityapproximateNoteSearchapproximateNoteSearchNoRhythmapproximateNoteSearchOnlyRhythmrn   )r)   r   rW   notePredictionr   r7   tn_recording	totScoresbeginningData
lengthDatar   	tn_windowhop
iterationsr`   rV   r;   r^   listOfPartspositiontotalLengthnumberprobabilityHitunused_listOfParts2unused_listOfParts3unused_listOfParts4s                             r*   rL   zScoreFollower.matchingNotes  s`    	( 3/779GGHI	
		,"456	$))IM*+!8Jdjj[)9C)?@#yyS9: ;J z"A!!c'A+a#g.Dq.HIGkk'*Gq6D  S1-l+GJW% # ::$$&44;;=yJ C,|;cAAEE -<sBQFNL##$<= $/#>#>NN$
 $. [*--.%$*C*Ca*G ##$56NNaN>>QN(2CCN$::$$&44;;=yJ$BB$$&44;;=yJ$DD$$&44;;=yJ s9V,-.A%	&(9!(<(J(JJK / >>Q4#<#<#A,V4z&7II,nlJJr,   )N)FFg      .@N)__name__
__module____qualname__r+   r4   r0   rH   rM   rl   rh   rL   rk   r,   r*   r
   r
      sB    &@ )Bsj**Xtl#-JVKr,   r
   c                      e Zd Zd Zy)TestExternalc                    ddl m} |j                  d      j                  d   j	                         j
                  }t        |      }|j                  ddd       y )	Nr   )corpuszluca/gloria)r   FTg      $@)r2   r&   r3   )r@   r   parsepartsr   r   r
   r4   )r)   r   r;   ScFs       r*   xtestRunScoreFollowerz"TestExternal.xtestRunScoreFollower<  sL    ",,}-33A6>>@NN0%dCr,   N)r   r   r   r   rk   r,   r*   r   r   :  s    Dr,   r   __main__)
__future__r   ru   r   unittestr@   r   r   r   r   Environmentr   r
   TestCaser   r   mainTestrk   r,   r*   <module>r      sw    #       &{&&'BCZK ZK@D8$$ D zG\" r,   