Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update akshara pulse tracker (rhythm extractor) and fix tool listing functions #84

Merged
merged 8 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions compiam/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,56 @@
import csv
import json
import yaml
import pickle

import compiam.utils
import numpy as np

from compiam import utils

def write_csv(data, out_path, header=None):
"""Writing two dimensional data into a file (.csv)
"""Writing multi-dimensional data into a file (.csv)

:param data: the data to write
:param output_path: the path where the data is going to be stored

:returns: None
"""
D = list(zip(*data))
data = np.array(data)
with open(out_path, "w") as f:
writer = csv.writer(f)
writer = csv.writer(f, delimiter=",")
if header:
assert len(header) == len(D[0]), "Header and row length mismatch"
if len(header) != len(data[0, :]):
raise ValueError("Header and row length mismatch")
writer.writerow(header)
for row in D:
writer.writerow(row)
writer.writerows(data)


def read_csv(file_path):
genisplaja marked this conversation as resolved.
Show resolved Hide resolved
"""Reading a csv file (.csv)

:param file_path: path to the csv

:returns: numpy array containing the data from the read CSV
"""
output = []
with open(file_path, "r") as f:
reader = csv.reader(f)
# Checking if header is string
header = next(reader)
try:
float(header[0])
has_header = True
except:
has_header = True
#Include header if is not based on strings
if has_header is False:
output.append(header)
for row in reader:
output.append([float(x) for x in row])
return np.array(output, dtype=float)


def save_object(obj, filename):
import pickle
with open(filename, 'wb') as outp: # Overwrites any existing file.
pickle.dump(obj, outp, pickle.HIGHEST_PROTOCOL)

Expand All @@ -39,7 +66,7 @@ def write_json(j, path):
if the directory doesn't exist, one will be created
:type path: str
"""
compiam.utils.create_if_not_exists(path)
utils.create_if_not_exists(path)
# Opening JSON file
with open(path, 'w') as f:
json.dump(j, f)
Expand Down
12 changes: 6 additions & 6 deletions compiam/melody/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import inspect, importlib as implib
from compiam.data import models_dict

to_avoid = [
TO_AVOID = [
x[0]
for x in inspect.getmembers(
implib.import_module("compiam.melody"), inspect.ismodule
Expand All @@ -24,24 +24,24 @@ def list_tasks():
for x in inspect.getmembers(
implib.import_module("compiam.melody"), inspect.ismodule
)
if x[0] not in to_avoid
if x[0] not in TO_AVOID
]


# Show user the available tools
def list_tools():
tools = [
tasks = [
x[0]
for x in inspect.getmembers(
implib.import_module("compiam.melody"), inspect.ismodule
)
if x[0] not in to_avoid
if x[0] not in TO_AVOID
]
tools_for_tasks = [
inspect.getmembers(
implib.import_module("compiam.melody." + tool), inspect.isclass
implib.import_module("compiam.melody." + task), inspect.isclass
)
for tool in tools
for task in tasks
]
tools_for_tasks = [
tool[1].__module__.split(".")[-2] + "." + tool[0]
Expand Down
12 changes: 12 additions & 0 deletions compiam/melody/pitch_extraction/ftanet_carnatic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
get_est_arr,
)
from compiam.melody.pitch_extraction.ftanet_carnatic.cfp import cfp_process
from compiam.io import write_csv


class FTANetCarnatic(object):
Expand Down Expand Up @@ -326,3 +327,14 @@ def normalise_pitch(pitch, tonic, bins_per_octave=120, max_value=4):
return normalisation(
pitch, tonic, bins_per_octave=bins_per_octave, max_value=max_value
)

@staticmethod
def write_csv(data, output_path):
"""Calling the write_csv function in compiam.io to write the output pitch curve in a fle

:param data: the data to write
:param output_path: the path where the data is going to be stored

:returns: None
"""
return write_csv(data, output_path)
16 changes: 14 additions & 2 deletions compiam/melody/pitch_extraction/melodia.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np

from compiam.utils.pitch import normalisation, resampling
from compiam.io import write_csv


class Melodia:
Expand Down Expand Up @@ -82,8 +83,8 @@ def extract(self, input_data, input_sr=44100, out_step=None):
audio = estd.EqloudLoader(filename=input_data, sampleRate=self.sampleRate)()
elif isinstance(input_data, np.ndarray):
print("Resampling... (input sampling rate is {}Hz, make sure this is correct)".format(input_sr))
resampling = estd.Resample(inputSampleRate=input_sr, outputSampleRate=self.sampleRate)()
input_data = resampling(input_data)
resample_audio = estd.Resample(inputSampleRate=input_sr, outputSampleRate=self.sampleRate)()
input_data = resample_audio(input_data)
audio = estd.EqualLoudness(signal=input_data)()
else:
raise ValueError("Input must be path to audio signal or an audio array")
Expand Down Expand Up @@ -134,3 +135,14 @@ def normalise_pitch(pitch, tonic, bins_per_octave=120, max_value=4):
return normalisation(
pitch, tonic, bins_per_octave=bins_per_octave, max_value=max_value
)

@staticmethod
def write_csv(data, output_path):
genisplaja marked this conversation as resolved.
Show resolved Hide resolved
"""Calling the write_csv function in compiam.io to write the output pitch curve in a fle

:param data: the data to write
:param output_path: the path where the data is going to be stored

:returns: None
"""
return write_csv(data, output_path)
12 changes: 6 additions & 6 deletions compiam/rhythm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import inspect, importlib as implib
from compiam.data import models_dict

to_avoid = [
TO_AVOID = [
x[0]
for x in inspect.getmembers(
implib.import_module("compiam.rhythm"), inspect.ismodule
Expand All @@ -22,24 +22,24 @@ def list_tasks():
for x in inspect.getmembers(
implib.import_module("compiam.rhythm"), inspect.ismodule
)
if x[0] not in to_avoid
if x[0] not in TO_AVOID
]


# Show user the available tools
def list_tools():
tools = [
tasks = [
x[0]
for x in inspect.getmembers(
implib.import_module("compiam.rhythm"), inspect.ismodule
)
if x[0] not in to_avoid
if x[0] not in TO_AVOID
]
tools_for_tasks = [
inspect.getmembers(
implib.import_module("compiam.rhythm." + tool), inspect.isclass
implib.import_module("compiam.rhythm." + task), inspect.isclass
)
for tool in tools
for task in tasks
]
tools_for_tasks = [
tool[1].__module__.split(".")[-2] + "." + tool[0]
Expand Down
Loading