Exemplo n.º 1
0
def getAcousticIndices(audiofile):
    if (DEBUG_FLAG):
        print(
            "[WORKING] Attempting to run acoustic indices calculator - acousticIndices.py"
        )

    # loop through the files in the directory
    try:
        data, fs = librosa.load(audiofile, sr=None, offset=0, duration=60)
        # mono channel
        data = AudioProcessing.convert_to_mono(data)

        # changing sampling rate
        new_fs = 17640
        data_chunk = AudioProcessing.resample(data, fs, new_fs)

        # extracting indices
        acousticIndices = AcousticIndices(data_chunk, new_fs)
        acoustic_indices = acousticIndices.get_acoustic_indices()

        acoustic_indices = list(map(lambda x: round(x, 4), acoustic_indices))
        if (PREDICTION_VERBOSE):
            print(acoustic_indices)

        acoustic_headers = acousticIndices.get_acoustic_indices_headers()
        acoustic_descs = acousticIndices.get_acoustic_indices_descs()
        # singleResultArray is used to store the results of one file (List of dictionaries)
        singleResultArray = []

        # Traverse the acoustic tags
        for i in range(len(acoustic_headers)):
            # per indices in the length of the acoustic tags,
            # append dictionary items.
            singleResultArray.append({
                "index": acoustic_headers[i],
                "value": acoustic_indices[i],
                "desc": acoustic_descs[i]
            })

            # append result dictionary to the final results array
            if (DEBUG_FLAG):
                print("[WORKING] Calculated " + acoustic_headers[i] +
                      " - acousticIndices.py")
    except Exception as e:
        track = traceback.format_exc()
        print(track)
        singleResultArray = "ERROR_PRESENT"

    if (DEBUG_FLAG):
        print("[SUCCESS] Calculated acoustic indices - acousticIndices.py")
    return singleResultArray
def getAcousticIndices():
    # fileDictionary will be used to store the filecount keys with their respective file information
    fileDictionary = {}

    # Create file counter
    fileCount = 0

    print(
        "[WORKING] Attempting to run acoustic indices calculator - acousticIndices.py"
    )
    # loop through the files in the directory
    for file in os.listdir("instance/upload/"):

        # correct the file path with the prefixed upload folder
        filePath = "instance/upload/" + file
        data, fs = librosa.load(filePath, sr=None, offset=0, duration=60)

        # mono channel
        data = AudioProcessing.convert_to_mono(data)

        # changing sampling rate
        new_fs = 17640
        data_chunk = AudioProcessing.resample(data, fs, new_fs)

        # extracting indices
        acousticIndices = AcousticIndices(data_chunk, new_fs)
        acoustic_indices = acousticIndices.get_acoustic_indices()
        acoustic_headers = acousticIndices.get_acoustic_indices_headers()

        # singleResultArray is used to store the results of one file (List of dictionaries)
        singleResultArray = []

        # Traverse the acoustic tags
        for i in range(len(acoustic_headers)):
            # per indices in the length of the acoustic tags,
            # append dictionary items.
            singleResultArray.append({
                "index": acoustic_headers[i],
                "value": acoustic_indices[i]
            })
        # append result dictionary to the final results array
        print("[WORKING] Calculated " + acoustic_headers[i] +
              " - acousticIndices.py")
        fileDictionary[fileCount] = singleResultArray
        fileCount += 1

    print("[SUCCESS] Calculated acoustic indices - acousticIndices.py")
    return fileDictionary
        return feature_headers


if __name__ == "__main__":

    # Audio Filename to be read
    filename = "bird.mp3"

    # considering one minute of the audio - the indices are taken 1 minute audio segments
    data, fs = librosa.load(filename, sr=None, offset=0, duration=60)

    # mono channel
    data = AudioProcessing.convert_to_mono(data)

    # changing sampling rate
    new_fs = 17640
    data_chunk = AudioProcessing.resample(data, fs, new_fs)

    # extracting indices
    acousticIndices = AcousticIndices(data_chunk, new_fs)
    acoustic_indices = acousticIndices.get_acoustic_indices()
    acoustic_headers = acousticIndices.get_acoustic_indices_headers()

    acousticIndices = np.column_stack(acousticIndices)

    # creating a dataframe
    df = pd.DataFrame(acousticIndices)
    df.columns = acoustic_headers

    df.to_csv("acoustic_indices.csv")