def jam_pack(jam, **kwargs): """Pack data into a jams sandbox. If not already present, this creates a `muda` field within `jam.sandbox`, along with `history`, `state`, and version arrays which are populated by deformation objects. Any additional fields can be added to the `muda` sandbox by supplying keyword arguments. Parameters ---------- jam : jams.JAMS A JAMS object Returns ------- jam : jams.JAMS The updated JAMS object Examples -------- >>> jam = jams.JAMS() >>> muda.jam_pack(jam, my_data=dict(foo=5, bar=None)) >>> jam.sandbox <Sandbox: muda> >>> jam.sandbox.muda <Sandbox: state, version, my_data, history> >>> jam.sandbox.muda.my_data {'foo': 5, 'bar': None} """ if not hasattr(jam.sandbox, "muda"): # If there's no mudabox, create one jam.sandbox.muda = jams.Sandbox( history=[], state=[], version=dict( muda=version, librosa=librosa.__version__, jams=jams.__version__, pysoundfile=psf.__version__, ), ) elif not isinstance(jam.sandbox.muda, jams.Sandbox): # If there is a muda entry, but it's not a sandbox, coerce it jam.sandbox.muda = jams.Sandbox(**jam.sandbox.muda) jam.sandbox.muda.update(**kwargs) return jam
def lyrics_to_jams(lyric_data, description=None): """Convert lyric annotations into jams format. Args: lyric_data (annotations.LyricData): lyric annotation object description (str): annotation descriptoin Returns: jams.Annotation: jams annotation object. """ jannot_lyric = jams.Annotation(namespace="lyrics") jannot_lyric.annotation_metadata = jams.AnnotationMetadata( data_source="mirdata") if lyric_data is not None: if not isinstance(lyric_data, annotations.LyricData): raise TypeError("Type should be LyricData.") for beg, end, lyric in zip(lyric_data.intervals[:, 0], lyric_data.intervals[:, 1], lyric_data.lyrics): jannot_lyric.append(time=beg, duration=end - beg, value=lyric) if description is not None: jannot_lyric.sandbox = jams.Sandbox(name=description) return jannot_lyric
def lyrics_to_jams(lyrics): ''' Convert lyrics annotations into jams format. Parameters ---------- lyrics: tuple A tuple in the format (LyricData, str), where str describes the annotation and LyricData is the lyric mirdata annotation format. Returns ------- jannot_lyric: JAM lyric annotation object. ''' jannot_lyric = jams.Annotation(namespace='lyrics') jannot_lyric.annotation_metadata = jams.AnnotationMetadata( data_source='mirdata') if lyrics[0] is not None: if type(lyrics[0]) != utils.LyricData: raise TypeError('Type should be LyricData.') for beg, end, lyric in zip(lyrics[0].start_times, lyrics[0].end_times, lyrics[0].lyrics): jannot_lyric.append(time=beg, duration=end - beg, value=lyric) if lyrics[1] is not None: jannot_lyric.sandbox = jams.Sandbox(name=lyrics[1]) return jannot_lyric
def fill_annotation_metadata(ann, row): """Fills the annotation metadata.""" ann.annotation_metadata.annotation_tools = 'Sonic Visualiser' ann.annotation_metadata.curator = (jams.Curator( name='Justin Salamon', email='*****@*****.**')) annotators = { 'annotators': [{ 'name': 'Justin Salamon', 'email': '*****@*****.**' }, { 'name': 'Christopher Jacoby', 'email': '*****@*****.**' }] } ann.annotation_metadata.annotator = jams.Sandbox(**annotators) ann.annotation_metadata.version = '1.0' ann.annotation_metadata.corpus = 'UrbanSound8K' ann.annotation_metadata.annotation_rules = ( 'See: J. Salamon, C. Jacoby and J. P. Bello, "A Dataset and Taxonomy ' 'for Urban Sound Research", in Proc. 22nd ACM International Conference ' 'on Multimedia, Orlando, USA, Nov. 2014.') ann.annotation_metadata.data_source = ( 'https://serv.cusp.nyu.edu/projects/urbansounddataset/') ann.annotation_metadata.validation = '' # Store all metadata in sandbox too ann.sandbox.update(**row)
def process_track(input_dir, output_dir, metadata, tags, compress): # Construct track metadata duration = get_track_duration( os.path.join(input_dir, 'audio', metadata['filename'])) file_meta = jams.FileMetadata(title=metadata['title'], artist=metadata['artist'], duration=duration, identifiers=jams.Sandbox(id=metadata.name)) # Get the tag annotation amd = jams.AnnotationMetadata(curator=jams.Curator(**__curator__), corpus=__corpus__) ann = jams.Annotation('tag_cal10k', annotation_metadata=amd) for tag in tags: ann.append(time=0, duration=duration, value=tag) jam = jams.JAMS(file_metadata=file_meta) jam.annotations.append(ann) jam.sandbox.content_path = metadata['filename'] save_jam(output_dir, jam, metadata.name, compress)
def events_to_jams(events): """ Convert events annotations into jams format. Parameters ---------- events: tuple A tuple in the format (EventData, str), where str describes the annotation and EventData is the events mirdata annotation format. Returns ------- jannot_events: JAM tag_open annotation object. """ jannot_events = jams.Annotation(namespace='tag_open') jannot_events.annotation_metadata = jams.AnnotationMetadata(data_source='mirdata') if events[0] is not None: if type(events[0]) != utils.EventData: raise TypeError('Type should be EventData.') for beg, end, label in zip( events[0].start_times, events[0].end_times, events[0].event ): jannot_events.append(time=beg, duration=end - beg, value=str(label)) if events[1] is not None: jannot_events.sandbox = jams.Sandbox(name=events[1]) return jannot_events
def tag_gtzan_to_jams(tags): """ Convert tag-gtzan annotations into jams format. Parameters ---------- tags: tuple A tuple in the format (str, str), where the first str is the tag and the second describes the annotation. Returns ------- jannot_tag_gtzan: JAM tag_gtzan annotation object. """ jannot_tag_gtzan = jams.Annotation(namespace='tag_gtzan') jannot_tag_gtzan.annotation_metadata = jams.AnnotationMetadata( data_source='mirdata' ) if tags[0] is not None: if not isinstance(tags[0], str): raise TypeError('Type should be str.') jannot_tag_gtzan.append(time=0.0, duration=0.0, value=tags[0]) if tags[1] is not None: jannot_tag_gtzan.sandbox = jams.Sandbox(name=tags[1]) return jannot_tag_gtzan
def notes_to_jams(notes): ''' Convert notes annotations into jams format using note_to_midi from librosa. Parameters ---------- notes: tuple A tuple in the format (NoteData, str), where str describes the annotation and NoteData is the notes mirdata annotation format. Returns ------- jannot_notes: JAM note_midi annotation object. ''' jannot_note = jams.Annotation(namespace='note_hz') jannot_note.annotation_metadata = jams.AnnotationMetadata( data_source='mirdata') if notes[0] is not None: if type(notes[0]) != utils.NoteData: raise TypeError('Type should be NoteData.') for beg, end, n in zip(notes[0].intervals[:, 0], notes[0].intervals[:, 1], notes[0].notes): jannot_note.append(time=beg, duration=end - beg, value=n) if notes[1] is not None: jannot_note.sandbox = jams.Sandbox(name=notes[1]) return jannot_note
def tags_to_jams(tags, annotator=None, duration=0, namespace="tag_open", description=None): """Convert tags annotations into jams format. Args: tags (annotations.Tags): tags annotation object annotator (str): annotator id namespace (str): the jams-compatible tag namespace description (str): annotation description Returns: jams.Annotation: jams annotation object. """ ann = jams.Annotation(namespace=namespace) ann.annotation_metadata = jams.AnnotationMetadata( data_source="soundata", annotator={"id": annotator} if annotator is not None else None, ) for t, c in zip(tags.labels, tags.confidence): ann.append(time=0.0, duration=duration, value=t, confidence=c) if description is not None: ann.sandbox = jams.Sandbox(name=description) return ann
def f0s_to_jams(f0_data, description=None): """Convert f0 annotations into jams format. Args: f0_data (annotations.F0Data): f0 annotation object description (str): annotation descriptoin Returns: jams.Annotation: jams annotation object. """ jannot_f0 = jams.Annotation(namespace="pitch_contour") jannot_f0.annotation_metadata = jams.AnnotationMetadata( data_source="mirdata") if f0_data is not None: if not isinstance(f0_data, annotations.F0Data): raise TypeError("Type should be F0Data.") for t, f, c in zip(f0_data.times, f0_data.frequencies, f0_data.confidence): jannot_f0.append( time=t, duration=0.0, value={ "index": 0, "frequency": f, "voiced": f > 0 }, confidence=c, ) if description is not None: jannot_f0.sandbox = jams.Sandbox(name=description) return jannot_f0
def chords_to_jams(chords): ''' Convert chords annotations into jams format. Parameters ---------- chords: tuple A tuple in the format (ChordData, str), where str describes the annotation and ChordData is the chords mirdata annotation format. Returns ------- jannot_chord: JAM chord annotation object. ''' jannot_chord = jams.Annotation(namespace='chord') jannot_chord.annotation_metadata = jams.AnnotationMetadata( data_source='mirdata') if chords[0] is not None: if type(chords[0]) != utils.ChordData: raise TypeError('Type should be ChordData.') for beg, end, ch in zip(chords[0].intervals[:, 0], chords[0].intervals[:, 1], chords[0].labels): jannot_chord.append(time=beg, duration=end - beg, value=ch) if chords[1] is not None: jannot_chord.sandbox = jams.Sandbox(name=chords[1]) return jannot_chord
def keys_to_jams(keys): ''' Convert keys annotations into jams format. Parameters ---------- keys: tuple A tuple in the format (KeyData, str), where str describes the annotation and KeyData is the keys mirdata annotation format. Returns ------- jannot_key: JAM key_mode annotation object. ''' jannot_key = jams.Annotation(namespace='key_mode') jannot_key.annotation_metadata = jams.AnnotationMetadata( data_source='mirdata') if keys[0] is not None: if type(keys[0]) != utils.KeyData: raise TypeError('Type should be KeyData.') for beg, end, key in zip(keys[0].start_times, keys[0].end_times, keys[0].keys): jannot_key.append(time=beg, duration=end - beg, value=key) if keys[1] is not None: jannot_key.sandbox = jams.Sandbox(name=keys[1]) return jannot_key
def events_to_jams(event_data, description=None): """Convert events annotations into jams format. Args: event_data (annotations.EventData): event data object description (str): annotation description Returns: jams.Annotation: jams annotation object. """ jannot_events = jams.Annotation(namespace="tag_open") jannot_events.annotation_metadata = jams.AnnotationMetadata( data_source="mirdata") if event_data is not None: if not isinstance(event_data, annotations.EventData): raise TypeError("Type should be EventData.") for beg, end, label in zip(event_data.intervals[:, 0], event_data.intervals[:, 1], event_data.events): jannot_events.append(time=beg, duration=end - beg, value=label) if description is not None: jannot_events.sandbox = jams.Sandbox(name=description) return jannot_events
def f0s_to_jams(f0s): ''' Convert f0 annotations into jams format. Parameters ---------- f0s: tuple A tuple in the format (F0Data, str), where str describes the annotation and F0Data is the f0 mirdata annotation format. Returns ------- jannot_f0: JAM pitch_contour annotation object. ''' jannot_f0 = jams.Annotation(namespace='pitch_contour') jannot_f0.annotation_metadata = jams.AnnotationMetadata( data_source='mirdata') if f0s[0] is not None: if type(f0s[0]) != utils.F0Data: raise TypeError('Type should be F0Data.') for t, f, c in zip(f0s[0].times, f0s[0].frequencies, f0s[0].confidence): jannot_f0.append(time=t, duration=0.0, value=f, confidence=c) if f0s[1] is not None: jannot_f0.sandbox = jams.Sandbox(name=f0s[1]) return jannot_f0
def sections_to_jams(section_data, description=None): """Convert section annotations into jams format. Args: section_data (annotations.SectionData): section data object description (str): annotation description Returns: jams.Annotation: jams annotation object. """ jannot_seg = jams.Annotation(namespace="segment_open") jannot_seg.annotation_metadata = jams.AnnotationMetadata( data_source="mirdata") if section_data is not None: if not isinstance(section_data, annotations.SectionData): raise TypeError("Type should be SectionData.") for inter, seg in zip(section_data.intervals, section_data.labels): jannot_seg.append(time=inter[0], duration=inter[1] - inter[0], value=seg) if description is not None: jannot_seg.sandbox = jams.Sandbox(name=description) return jannot_seg
def test_jams(): data = dict(time=[0.0, 1.0], duration=[0.5, 0.5], value=['one', 'two'], confidence=[0.9, 0.9]) real_ann = jams.AnnotationArray( annotations=[jams.Annotation('tag_open', data=data)]) meta = dict(title='Test track', artist='Test artist', release='Test release', duration=31.3) real_fm = jams.FileMetadata(**meta) real_sandbox = jams.Sandbox(description='none') def __test(annotations, file_metadata, sandbox): jam = jams.JAMS(annotations=annotations, file_metadata=file_metadata, sandbox=sandbox) if file_metadata is not None: eq_(dict(file_metadata), dict(jam.file_metadata)) if sandbox is not None: eq_(dict(sandbox), dict(jam.sandbox)) if annotations is not None: eq_(annotations, jam.annotations) for ann in [None, real_ann]: for fm in [None, real_fm]: for sandbox in [None, real_sandbox]: yield __test, ann, fm, sandbox
def test_annotation(): def __test(namespace, data, amd, sandbox): ann = jams.Annotation(namespace, data=data, annotation_metadata=amd, sandbox=sandbox) eq_(namespace, ann.namespace) if amd is not None: eq_(dict(amd), dict(ann.annotation_metadata)) if sandbox is not None: eq_(dict(sandbox), dict(ann.sandbox)) if data is not None: assert ann.data.equals(jams.JamsFrame.from_dict(data)) real_sandbox = jams.Sandbox(description='none') real_amd = jams.AnnotationMetadata(corpus='test collection') real_data = dict(time=[0.0, 1.0], duration=[0.5, 0.5], value=['one', 'two'], confidence=[0.9, 0.9]) namespace = 'tag_open' for data in [None, real_data]: for amd in [None, real_amd]: for sandbox in [None, real_sandbox]: yield __test, namespace, data, amd, sandbox
def test_annotation_metadata(): def __test(data, curator, annotator): md = jams.AnnotationMetadata(curator=curator, annotator=annotator, **data) if curator is not None: eq_(dict(md.curator), dict(curator)) if annotator is not None: eq_(dict(md.annotator), dict(annotator)) real_data = dict(md) real_data.pop('curator') real_data.pop('annotator') eq_(real_data, data) dummies = dict(version='0', corpus='test', annotation_tools='nose', annotation_rules='brains', validation='unnecessary', data_source='null') real_curator = jams.Curator(name='nobody', email='*****@*****.**') real_annotator = jams.Sandbox(description='none') for curator in [None, real_curator]: for annotator in [None, real_annotator]: yield __test, dummies, curator, annotator
def events_to_jams(events, description=None): """Convert events annotations into jams format. Args: events (annotations.Events): events data object description (str): annotation description Returns: jams.Annotation: jams annotation object. """ jannot_events = jams.Annotation(namespace="segment_open") jannot_events.annotation_metadata = jams.AnnotationMetadata( data_source="soundata") for inter, label, conf in zip(events.intervals, events.labels, events.confidence): jannot_events.append( time=inter[0], duration=inter[1] - inter[0], value=label, confidence=conf, ) if description is not None: jannot_events.sandbox = jams.Sandbox(name=description) return jannot_events
def f0s_to_jams(f0s): """ Convert f0 annotations into jams format. Parameters ---------- f0s: tuple A tuple in the format (F0Data, str), where str describes the annotation and F0Data is the f0 mirdata annotation format. Returns ------- jannot_f0: JAM pitch_contour annotation object. """ jannot_f0 = jams.Annotation(namespace="pitch_contour") jannot_f0.annotation_metadata = jams.AnnotationMetadata( data_source="mirdata") if f0s[0] is not None: if not isinstance(f0s[0], utils.F0Data): raise TypeError("Type should be F0Data.") for t, f, c in zip(f0s[0].times, f0s[0].frequencies, f0s[0].confidence): jannot_f0.append( time=t, duration=0.0, value={ "index": 0, "frequency": f, "voiced": f > 0 }, confidence=c, ) if f0s[1] is not None: jannot_f0.sandbox = jams.Sandbox(name=f0s[1]) return jannot_f0
def sections_to_jams(sections): ''' Convert sections annotations into jams format. Parameters ---------- sections: tuple A tuple in the format (SectionData, str), where str describes the annotation and SectionData is the sections mirdata annotation format. Returns ------- jannot_seg: JAM segment_open annotation object. ''' jannot_seg = jams.Annotation(namespace='segment_open') jannot_seg.annotation_metadata = jams.AnnotationMetadata( data_source='mirdata') if sections[0] is not None: if type(sections[0]) != utils.SectionData: raise TypeError('Type should be SectionData.') for inter, seg in zip(sections[0].intervals, sections[0].labels): jannot_seg.append(time=inter[0], duration=inter[1] - inter[0], value=seg) if sections[1] is not None: jannot_seg.sandbox = jams.Sandbox(name=sections[1]) return jannot_seg
def beats_to_jams(beats): ''' Convert beats annotations into jams format. Parameters ---------- beats: tuple A tuple in the format (BeatData, str), where str describes the annotation and BeatData is the beats mirdata annotation format. Returns ------- jannot_beat: JAM beat annotation object. ''' jannot_beat = jams.Annotation(namespace='beat') jannot_beat.annotation_metadata = jams.AnnotationMetadata( data_source='mirdata') if beats[0] is not None: if type(beats[0]) != utils.BeatData: raise TypeError('Type should be BeatData.') for t, p in zip(beats[0].beat_times, beats[0].beat_positions): jannot_beat.append(time=t, duration=0.0, value=p) if beats[1] is not None: jannot_beat.sandbox = jams.Sandbox(name=beats[1]) return jannot_beat
def test_sandbox(): data = dict(key1='value 1', key2='value 2') J = jams.Sandbox(**data) for key, value in six.iteritems(data): assert value == J[key]
def get_sandbox(): # sandbox = { # "boundaries_id": "sf", # "labels_id": null, # "timestamp": "2020/06/06 13:39:32", # "annot_beats": false, # "feature": "cqt", # "framesync": false, # "feature_default": null, # "M_gaussian": 27, # "m_embedded": 3, # "k_nearest": 0.04, # "Mp_adaptive": 28, # "offset_thres": 0.05, # "bound_norm_feats": Infinity, # "hier": false # } sandbox = jams.Sandbox( boundaries_id= "sf", labels_id= None, timestamp= "2020/06/06 13:39:32", annot_beats= False, feature= "cqt", framesync= False, feature_default= None, M_gaussian= 27, m_embedded= 3, k_nearest= 0.04, Mp_adaptive= 28, offset_thres= 0.05, bound_norm_feats= np.Infinity, hier= False ) return sandbox # jam = { # "annotations" : { # "annotation_metadata" : { # "curator" : { # "name" : "", # "email" : "" # }, # "annotator" : {}, # "version" : "", # "corpus" : "", # "annotation_tools" : "", # "annotation_rules" : "", # "validation" : "", # "data_source" : "MSAF" # }, # "namespace" : "segment_salami_function", # "data" : observations # } # }
def test_jobject_eq(d1, d2, match): J1 = jams.JObject(**d1) J2 = jams.JObject(**d2) # Test self-equivalence assert J1 == J1 assert J2 == J2 # Test equivalence in both directions assert (J1 == J2) == match assert (J2 == J1) == match # Test type safety J3 = jams.Sandbox(**d1) assert not J1 == J3
def tempos_to_jams(tempo_data, description=None): """Convert tempo annotations into jams format. Args: tempo_data (annotations.TempoData): tempo data object description (str): annotation description Returns: jams.Annotation: jams annotation object. """ jannot_tempo = jams.Annotation(namespace="tempo") jannot_tempo.annotation_metadata = jams.AnnotationMetadata(data_source="mirdata") if tempo_data is not None: if not isinstance(tempo_data, float) and not isinstance(tempo_data, int): raise TypeError("Type should be float or int.") jannot_tempo.append(time=0, duration=0, confidence=1, value=tempo_data) if description is not None: jannot_tempo.sandbox = jams.Sandbox(name=description) return jannot_tempo
def tag_to_jams(tag_data, namespace="tag_open", description=None): """Convert lyric annotations into jams format. Args: lyric_data (annotations.LyricData): lyric annotation object namespace (str): the jams-compatible tag namespace description (str): annotation descriptoin Returns: jams.Annotation: jams annotation object. """ jannot_tag = jams.Annotation(namespace=namespace) jannot_tag.annotation_metadata = jams.AnnotationMetadata(data_source="mirdata") if tag_data is not None: if not isinstance(tag_data, str): raise TypeError("Type should be str.") jannot_tag.append(time=0.0, duration=0.0, value=tag_data) if description is not None: jannot_tag.sandbox = jams.Sandbox(name=description) return jannot_tag
def beats_to_jams(beat_data, description=None): """Convert beat annotations into jams format. Args: beat_data (annotations.BeatData): beat data object description (str): annotation description Returns: jams.Annotation: jams annotation object. """ jannot_beat = jams.Annotation(namespace="beat") jannot_beat.annotation_metadata = jams.AnnotationMetadata(data_source="mirdata") if beat_data is not None: if not isinstance(beat_data, annotations.BeatData): raise TypeError("Type should be BeatData.") for t, p in zip(beat_data.times, beat_data.positions): jannot_beat.append(time=t, duration=0.0, value=p) if description is not None: jannot_beat.sandbox = jams.Sandbox(name=description) return jannot_beat
def tempos_to_jams(tempos): """ Convert tempo annotations into jams format. Parameters ---------- tempo: tuple A tuple in the format (float, str), where str describes the annotation and float is the tempo in beats per minute. Returns ------- jannot_tempo: JAM tempo annotation object. """ jannot_tempo = jams.Annotation(namespace='tempo') jannot_tempo.annotation_metadata = jams.AnnotationMetadata(data_source='mirdata') if tempos[0] is not None: if not isinstance(tempos[0], float) and not isinstance(tempos[0], int): raise TypeError('Type should be float or int.') jannot_tempo.append(time=0, duration=0, confidence=1, value=tempos[0]) if tempos[1] is not None: jannot_tempo.sandbox = jams.Sandbox(name=tempos[1]) return jannot_tempo
def keys_to_jams(key_data, description): """Convert key annotations into jams format. Args: key_data (annotations.KeyData): key data object description (str): annotation description Returns: jams.Annotation: jams annotation object. """ jannot_key = jams.Annotation(namespace="key_mode") jannot_key.annotation_metadata = jams.AnnotationMetadata( data_source="mirdata") if key_data is not None: if not isinstance(key_data, annotations.KeyData): raise TypeError("Type should be KeyData.") for beg, end, key in zip(key_data.intervals[:, 0], key_data.intervals[:, 1], key_data.keys): jannot_key.append(time=beg, duration=end - beg, value=key) if description is not None: jannot_key.sandbox = jams.Sandbox(name=description) return jannot_key