# Path: apicultor/sonification/Sonification.py
# def hfc_onsets(audio):
#     """
#     Find onsets in music based on High Frequency Content
#     :param audio: the input signal                                                                         
#     :returns:                                                                                                         
#       - hfcs = onsets locations in seconds
#     """
#     song = sonify(audio, 44100)
#     hfcs = []                          
#     for frame in song.FrameGenerator():
#         song.window()    
#         song.Spectrum()    
#         hfcs.append(song.hfc())   
#     hfcs /= max(hfcs) 
#     song.hfcs = hfcs
#     fir = firwin(11, 1.0 / 8, window = "hamming")
#     song.filtered = np.convolve(hfcs, fir, mode="same")
#     song.climb_hills() 
#     return np.array([i for i, x in enumerate(song.Filtered) if x > 0]) * song.N


import os
import sys
import json
import numpy as np
import logging
import random
from .utils.algorithms import *
from soundfile import read
from collections import defaultdict, OrderedDict
from .sonification.Sonification import hfc_onsets
        pool['rhythm.bpm_ticks'].append(retrieve.ticks / retrieve.M)

    #zero crossing rate
    namespace = 'lowlevel'
    desc_name = namespace + '.zero_crossing_rate'
    if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
        zcr = retrieve.zcr()
        pool[desc_name].append(zcr)

    #spectral flux
    namespace = 'lowlevel'
    desc_name = namespace + '.spectral_flux'
    if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
        try:
            retrieve.mel_dbs
            pool[desc_name].append(np.mean(retrieve.flux(retrieve.mel_dbs)))
        except Exception as e:
            retrieve.mel_bands_global()
            pool[desc_name].append(np.mean(retrieve.flux(retrieve.mel_dbs)))

    #onsets by polar distance
    namespace = 'rhythm'
    desc_name = namespace + '.onsets_by_polar_distance'
    if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
        pool[desc_name].append(onsets_indexes)
            
    #onsets by high frequency content
    namespace = 'rhythm'
    desc_name = namespace + '.onsets_hfc'
    if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):

# Path: apicultor/sonification/Sonification.py
# def hfc_onsets(audio):
#     """
#     Find onsets in music based on High Frequency Content
#     :param audio: the input signal                                                                         
#     :returns:                                                                                                         
#       - hfcs = onsets locations in seconds
#     """
#     song = sonify(audio, 44100)
#     hfcs = []                          
#     for frame in song.FrameGenerator():
#         song.window()    
#         song.Spectrum()    
#         hfcs.append(song.hfc())   
#     hfcs /= max(hfcs) 
#     song.hfcs = hfcs
#     fir = firwin(11, 1.0 / 8, window = "hamming")
#     song.filtered = np.convolve(hfcs, fir, mode="same")
#     song.climb_hills() 
#     return np.array([i for i, x in enumerate(song.Filtered) if x > 0]) * song.N


# Path: apicultor/run_mir_analysis.py
import os
import sys
import json
import numpy as np
import logging
import random
from .utils.algorithms import *
from soundfile import read
from collections import defaultdict, OrderedDict
from .sonification.Sonification import hfc_onsets

        pool['rhythm.bpm_ticks'].append(retrieve.ticks / retrieve.M)

    #zero crossing rate
    namespace = 'lowlevel'
    desc_name = namespace + '.zero_crossing_rate'
    if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
        zcr = retrieve.zcr()
        pool[desc_name].append(zcr)

    #spectral flux
    namespace = 'lowlevel'
    desc_name = namespace + '.spectral_flux'
    if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
        try:
            retrieve.mel_dbs
            pool[desc_name].append(np.mean(retrieve.flux(retrieve.mel_dbs)))
        except Exception as e:
            retrieve.mel_bands_global()
            pool[desc_name].append(np.mean(retrieve.flux(retrieve.mel_dbs)))

    #onsets by polar distance
    namespace = 'rhythm'
    desc_name = namespace + '.onsets_by_polar_distance'
    if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
        pool[desc_name].append(onsets_indexes)
            
    #onsets by high frequency content
    namespace = 'rhythm'
    desc_name = namespace + '.onsets_hfc'
    if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):

        pool[desc_name].append(np.mean(hfc_onsets(retrieve.signal) / retrieve.fs))            