Skip to content

Commit

Permalink
Support for single soundscapes the legacy way
Browse files Browse the repository at this point in the history
  • Loading branch information
antonyharfield committed Oct 28, 2024
1 parent 90e1c19 commit 70f229e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
6 changes: 6 additions & 0 deletions soundscapes/config/read_config_from_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ def read_config_from_env() -> dict:
if "SOUNDSCAPE_NORMALIZE" in os.environ:
config['soundscape_normalize'] = float(os.getenv("SOUNDSCAPE_NORMALIZE"))

if "PLAYLIST_ID" in os.environ:
config['playlist_id'] = int(os.getenv("PLAYLIST_ID")) if os.getenv("PLAYLIST_ID") is not None else None

if "JOB_NAME" in os.environ:
config['job_name'] = os.getenv("JOB_NAME")

return config
7 changes: 4 additions & 3 deletions soundscapes/config/validate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
log = get_logger()

def validate_config(config: dict) -> None:
not_set_project = not 'project' in config
not_set_project = 'project' not in config
not_set_playlist_id = 'playlist_id' not in config

if not_set_project:
log.critical('Invalid configuration: project not set')
if not_set_project or not_set_playlist_id:
log.critical('Invalid configuration: project not set or playlist_id not set')
exit(1)
32 changes: 32 additions & 0 deletions soundscapes/legacy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from .config.logs import get_logger
from .config.read_config import read_config
from .old.playlist_to_soundscape import playlist_to_soundscape
from .old.db import connect, get_automated_user, create_job

log = get_logger()

def main(config):
conn = connect()

playlist_id = config['playlist_id']
job_name = config['job_name']
soundscape_aggregation = config['soundscape_aggregation']
soundscape_bin_size = config['soundscape_bin_size']
soundscape_threshold = config['soundscape_threshold']
soundscape_normalize = config['soundscape_normalize']

user_id = get_automated_user(conn)

job_id = create_job(conn, playlist_id, user_id, soundscape_aggregation, soundscape_bin_size, soundscape_threshold, soundscape_normalize, job_name)
print('- Created and initialized job', job_id)

playlist_to_soundscape(job_id)
print('- Completed job', job_id)


if __name__ == "__main__":
log.info('PROCESS: Initialization')
config = read_config()
log.info('PROCESS: Job started')
main(config)
log.info('PROCESS: Job completed')
5 changes: 3 additions & 2 deletions soundscapes/old/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def create_playlist(conn, project_id, site_id, site_name, year):
cursor.close()
return playlist_id, playlist_name

def create_job(conn, playlist_id, user_id, aggregation = 'time_of_day', bin_size = 344, threshold = 0.05, normalize = 1) -> Union[int,None]:
def create_job(conn, playlist_id, user_id, aggregation = 'time_of_day', bin_size = 344, threshold = 0.05, normalize = 1, soundscape_name = None) -> Union[int,None]:
cursor = conn.cursor()

# Get aggregation id from identifier
Expand All @@ -149,6 +149,7 @@ def create_job(conn, playlist_id, user_id, aggregation = 'time_of_day', bin_size

# Additional parameters
max_hertz = 24000 # TODO compute this from the recordings
job_name = playlist_name if soundscape_name is None else soundscape_name
# job_name_suffix = soundscape_hash(playlist_id, aggregation_type_id, bin_size, threshold, normalize) # TODO make job name unique

cursor.execute(
Expand All @@ -161,7 +162,7 @@ def create_job(conn, playlist_id, user_id, aggregation = 'time_of_day', bin_size
cursor.execute(
'''insert into job_params_soundscape (job_id, playlist_id, name, max_hertz, soundscape_aggregation_type_id, bin_size, threshold, normalize)
values (%s, %s, %s, %s, %s, %s, %s, %s)''',
(job_id, playlist_id, playlist_name, max_hertz, aggregation_type_id, bin_size, threshold, normalize))
(job_id, playlist_id, job_name, max_hertz, aggregation_type_id, bin_size, threshold, normalize))
conn.commit()
cursor.close()

Expand Down

0 comments on commit 70f229e

Please sign in to comment.