예제 #1
0
def test_task_simple_chord_absent(SR, HOP_LENGTH):

    jam = jams.JAMS(file_metadata=dict(duration=4.0))
    trans = pumpp.task.SimpleChordTransformer(name='chord_s')

    output = trans.transform(jam)

    # Mask should be false since we have no matching namespace
    assert not np.any(output['chord_s/_valid'])

    # Check the shape
    assert output['chord_s/pitch'].shape == (1, 4 * (SR // HOP_LENGTH), 12)

    # Make sure it's empty
    assert not np.any(output['chord_s/pitch'])

    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
예제 #2
0
def test_task_beatpos_absent(SR, HOP_LENGTH, MAX_DIVISIONS, SPARSE):
    jam = jams.JAMS(file_metadata=dict(duration=4.0))
    trans = pumpp.task.BeatPositionTransformer(name='beat',
                                               max_divisions=MAX_DIVISIONS,
                                               sr=SR,
                                               hop_length=HOP_LENGTH,
                                               sparse=SPARSE)

    output = trans.transform(jam)

    assert not np.any(output['beat/_valid'])

    Y_pred = trans.encoder.inverse_transform(output['beat/position'][0])

    assert all([_ == 'X' for _ in Y_pred])

    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
예제 #3
0
def test_task_beat_absent():

    # Construct a jam
    jam = jams.JAMS(file_metadata=dict(duration=4.0))

    # One second = one frame
    T = crema.task.BeatTransformer()

    output = T.transform(jam)

    # Make sure we have the mask
    eq_(output['mask_beat'], False)
    eq_(output['mask_downbeat'], False)

    # Check the shape: 4 seconds at 2 samples per second
    assert np.allclose(output['output_beat'].shape, [4 * N_REPEAT, 1])
    assert np.allclose(output['output_downbeat'].shape, [4 * N_REPEAT, 1])
    assert not np.any(output['output_beat'])
    assert not np.any(output['output_downbeat'])
예제 #4
0
def test_task_tslabel_absent():
    labels = ['alpha', 'beta', 'psycho', 'aqua', 'disco']

    jam = jams.JAMS(file_metadata=dict(duration=4.0))
    T = crema.task.TimeSeriesLabelTransformer(namespace='tag_open',
                                              name='madeup',
                                              labels=labels)

    output = T.transform(jam)

    # Mask should be false since we have no matching namespace
    eq_(output['mask_madeup'], False)
    y = output['output_madeup']

    # Check the shape
    assert np.allclose(y.shape, [4 * N_REPEAT, len(labels)])

    # Make sure it's empty
    assert not np.any(y)
예제 #5
0
def test_task_glabel_absent():
    labels = ['alpha', 'beta', 'psycho', 'aqua', 'disco']

    jam = jams.JAMS(file_metadata=dict(duration=4.0))
    T = crema.task.GlobalLabelTransformer(namespace='tag_open',
                                          name='madeup',
                                          labels=labels)

    output = T.transform(jam)

    # Mask should be false since we have no matching namespace
    eq_(output['mask_madeup'], False)

    # Check the shape
    eq_(output['output_madeup'].ndim, 1)
    eq_(output['output_madeup'].shape[0], len(labels))

    # Make sure it's empty
    assert not np.any(output['output_madeup'])
예제 #6
0
def test_task_beat_nometer(SR, HOP_LENGTH):

    # Construct a jam
    jam = jams.JAMS(file_metadata=dict(duration=4.0))

    ann = jams.Annotation(namespace='beat')

    ann.append(time=0, duration=0.0)
    ann.append(time=1, duration=0.0)
    ann.append(time=2, duration=0.0)
    ann.append(time=3, duration=0.0)

    jam.annotations.append(ann)

    # One second = one frame
    trans = pumpp.task.BeatTransformer(name='beat')

    output = trans.transform(jam)

    # Make sure we have the mask
    assert np.all(
        output['beat/_valid'] == [0, 4 * trans.sr // trans.hop_length])
    assert not output['beat/mask_downbeat']

    # Check the shape: 4 seconds at 2 samples per second
    assert output['beat/beat'].shape == (1, 4 * (SR // HOP_LENGTH), 1)
    assert output['beat/downbeat'].shape == (1, 4 * (SR // HOP_LENGTH), 1)

    # Ideal vectors:
    #   a beat every second (two samples)
    #   no downbeats

    beat_true = np.asarray([1, 1, 1, 1])
    downbeat_true = np.asarray([0, 0, 0, 0])

    assert np.all(output['beat/beat'][0, ::(SR // HOP_LENGTH)] == beat_true)
    assert np.all(output['beat/downbeat'][0, ::(SR //
                                                HOP_LENGTH)] == downbeat_true)

    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
예제 #7
0
def test_task_key_present(SR, HOP_LENGTH, SPARSE):
    # Create jams with key annotation
    jam = jams.JAMS(file_metadata=dict(duration=12.0))

    ann = jams.Annotation(namespace='key_mode')

    ann.append(time=0, duration=2.0, value='C:minor')
    ann.append(time=2, duration=2.0, value='N')
    ann.append(time=4, duration=2.0, value='Eb')
    ann.append(time=8, duration=2.0, value='D:major')
    ann.append(time=10, duration=2.0, value='D:lydian')

    jam.annotations.append(ann)

    trans = pumpp.task.KeyTransformer(name='key',
                                      sr=SR,
                                      hop_length=HOP_LENGTH,
                                      sparse=SPARSE)

    output = trans.transform(jam)

    # Make sure we have the mask
    assert np.all(
        output['key/_valid'] == [0, 12 * trans.sr // trans.hop_length])

    # Ideal vectors:
    # pcp = Cmin, N, Eb, N, D, D_lyd
    pcp_true = np.array([
        _encode_key_str('C:minor', SPARSE)[0],
        _encode_key_str('N', SPARSE)[0],
        _encode_key_str('Eb:major', SPARSE)[0],
        _encode_key_str('N', SPARSE)[0],
        _encode_key_str('D', SPARSE)[0],
        _encode_key_str('D:lydian', SPARSE)[0]
    ])

    assert np.all(output['key/pitch_profile'] == np.repeat(
        pcp_true, (SR * 2 // HOP_LENGTH), axis=0))

    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
예제 #8
0
def test_task_beat_present(SR, HOP_LENGTH):

    # Construct a jam
    jam = jams.JAMS(file_metadata=dict(duration=4.0))

    ann = jams.Annotation(namespace='beat')

    ann.append(time=0, duration=0.0, value=1)
    ann.append(time=1, duration=0.0, value=2)
    ann.append(time=2, duration=0.0, value=3)
    ann.append(time=3, duration=0.0, value=1)

    jam.annotations.append(ann)

    trans = pumpp.task.BeatTransformer(name='beat')

    output = trans.transform(jam)

    # Make sure we have the masks
    assert np.all(
        output['beat/_valid'] == [0, 4 * trans.sr // trans.hop_length])
    assert output['beat/mask_downbeat']

    # The first channel measures beats
    # The second channel measures downbeats
    assert output['beat/beat'].shape == (1, 4 * (SR // HOP_LENGTH), 1)
    assert output['beat/downbeat'].shape == (1, 4 * (SR // HOP_LENGTH), 1)

    # Ideal vectors:
    #   a beat every second (two samples)
    #   a downbeat every three seconds (6 samples)

    beat_true = np.asarray([[1, 1, 1, 1]]).T
    downbeat_true = np.asarray([[1, 0, 0, 1]]).T

    assert np.all(output['beat/beat'][0, ::(SR // HOP_LENGTH)] == beat_true)
    assert np.all(output['beat/downbeat'][0, ::(SR //
                                                HOP_LENGTH)] == downbeat_true)

    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
예제 #9
0
def test_task_dlabel_auto(SR, HOP_LENGTH):
    jam = jams.JAMS(file_metadata=dict(duration=4.0))
    trans = pumpp.task.DynamicLabelTransformer(name='genre',
                                               namespace='tag_gtzan')

    output = trans.transform(jam)

    # Mask should be false since we have no matching namespace
    assert not np.any(output['genre/_valid'])

    y = output['genre/tags']

    # Check the shape
    assert y.shape == (1, 4 * (SR // HOP_LENGTH), 10)

    # Make sure it's empty
    assert not np.any(y)
    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
예제 #10
0
def test_task_slabel_auto():
    jam = jams.JAMS(file_metadata=dict(duration=4.0))
    trans = pumpp.task.StaticLabelTransformer(name='genre',
                                              namespace='tag_gtzan')

    output = trans.transform(jam)

    # Mask should be false since we have no matching namespace
    assert not np.any(output['genre/_valid'])

    # Check the shape
    assert output['genre/tags'].ndim == 2
    assert output['genre/tags'].shape[1] == 10

    # Make sure it's empty
    assert not np.any(output['genre/tags'])

    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
예제 #11
0
def test_load_jam_audio():

    def __test(jam_in, audio_file):

        jam = muda.load_jam_audio(jam_in, audio_file)

        assert hasattr(jam.sandbox, 'muda')

        eq_(jam.file_metadata.duration,
            librosa.get_duration(**jam.sandbox.muda._audio))

    # Add an empty jams test for missing duration
    yield __test, jams.JAMS(), 'data/fixture.wav'

    yield __test, 'data/fixture.jams', 'data/fixture.wav'

    yield __test, jams.load('data/fixture.jams'), 'data/fixture.wav'

    with open('data/fixture.jams', 'r') as fdesc:
        yield __test, fdesc, 'data/fixture.wav'
예제 #12
0
def create_JAMS(in_dir, metadata, out_file):
    """Creates a JAMS file given the path to a SALAMI track.

    Parameters
    ----------
    in_dir : str
        Path to the input directory
    metadata : str
        Metadata read from the CSV file
    out_file : str
        Output JAMS file
    """
    path = os.path.join(
        in_dir,
        "annotations",
        metadata[0],
    )

    # Sanity check
    if not os.path.exists(path):
        logging.warning("Path not found %s", path)
        return

    # New JAMS and annotation
    jam = jams.JAMS()

    # Create Annotations if they exist
    # Maximum 3 annotations per file
    for annotation_id in range(1, 4):
        if os.path.isfile(
                os.path.join(path, "textfile" + str(annotation_id) + ".txt")):
            create_annotations(jam, path, annotation_id, metadata)

    # Get the duration from the annotations
    dur = get_duration(jam)

    # Global file metadata
    fill_global_metadata(jam, metadata, dur)

    # Save JAMS
    jam.save(out_file)
예제 #13
0
파일: test_jams.py 프로젝트: nwh/jams
def test_jams_trim_valid():
    # For a valid scenario, ensure everything behaves as expected
    jam = jams.JAMS()
    jam.file_metadata.duration = 15

    namespace = 'tag_open'
    data = dict(time=[5.0, 5.0, 10.0],
                duration=[2.0, 4.0, 4.0],
                value=['one', 'two', 'three'],
                confidence=[0.9, 0.9, 0.9])
    ann = jams.Annotation(namespace, data=data, time=5.0, duration=10.0)
    for _ in range(5):
        jam.annotations.append(ann)

    ann_copy = jams.Annotation(namespace, data=data, time=5.0, duration=10.0)
    ann_trim = ann_copy.trim(0, 10, strict=False)
    jam_trim = jam.trim(0, 10, strict=False)

    for ann in jam_trim.annotations:
        assert ann.data == ann_trim.data

    assert jam_trim.file_metadata.duration == jam.file_metadata.duration
    assert jam_trim.sandbox.trim == [{'start_time': 0, 'end_time': 10}]

    # Multiple trims
    jam_trim = jam.trim(0, 10).trim(8, 10)
    ann_trim = ann_copy.trim(0, 10).trim(8, 10)

    for ann in jam_trim.annotations:
        assert ann.data == ann_trim.data

    assert jam_trim.sandbox.trim == ([{
        'start_time': 0,
        'end_time': 10
    }, {
        'start_time': 8,
        'end_time': 10
    }])

    # Make sure file metadata copied over correctly
    assert jam_trim.file_metadata == jam.file_metadata
예제 #14
0
def parse_smc(input_dir, output_dir):
    '''Convert smc to jams'''

    # Get a list of the wavs, tags, and txts

    wav_files = find_with_extension(os.path.join(input_dir, 'SMC_MIREX_Audio'),
                                    'wav',
                                    depth=1)

    ann_files = find_with_extension(os.path.join(
        input_dir, 'SMC_MIREX_Annotations_05_08_2014'),
                                    'txt',
                                    depth=1)

    tag_files = find_with_extension(os.path.join(input_dir, 'SMC_MIREX_Tags'),
                                    'tag',
                                    depth=1)

    # Make sure everything lines up
    assert len(wav_files) == len(ann_files)
    assert len(wav_files) == len(tag_files)

    for wav, ann, tag in zip(wav_files, ann_files, tag_files):
        # Get the file metadata
        metadata = smc_file_metadata(wav)

        # Get the annotation
        beat_annotation = smc_annotation(ann)

        # Get the tags
        tag_annotation = smc_tags(tag, metadata.duration)

        jam = jams.JAMS(file_metadata=metadata)
        jam.annotations.append(beat_annotation)
        jam.annotations.append(tag_annotation)

        # Add content path to the top-level sandbox
        jam.sandbox.content_path = os.path.basename(wav)

        # Save the jam
        save_jam(output_dir, jam)
예제 #15
0
def test_task_chord_absent():

    jam = jams.JAMS(file_metadata=dict(duration=4.0))
    T = crema.task.ChordTransformer()

    output = T.transform(jam)

    # Mask should be false since we have no matching namespace
    eq_(output['mask_chord'], False)

    # Check the shape
    assert np.allclose(output['output_pitches'].shape, [4 * N_REPEAT, 12])
    assert np.allclose(output['output_root'].shape, [4 * N_REPEAT, 13])
    assert np.allclose(output['output_bass'].shape, [4 * N_REPEAT, 13])

    # Make sure it's empty
    assert not np.any(output['output_pitches'])
    assert not np.any(output['output_root'][:, :12])
    assert not np.any(output['output_bass'][:, :12])
    assert np.all(output['output_root'][:, 12])
    assert np.all(output['output_bass'][:, 12])
예제 #16
0
    def __test(dimension, name):

        var_name = 'output_{:s}'.format(name)
        mask_name = 'mask_{:s}'.format(name)

        jam = jams.JAMS(file_metadata=dict(duration=4.0))
        T = crema.task.VectorTransformer(namespace='vector',
                                         dimension=dimension,
                                         name=name)

        output = T.transform(jam)

        # Mask should be false since we have no matching namespace
        eq_(output[mask_name], False)

        # Check the shape
        eq_(output[var_name].ndim, 1)
        eq_(output[var_name].shape[0], dimension)

        # Make sure it's empty
        assert not np.any(output[var_name])
예제 #17
0
def test_task_key_tag_absent(SR, HOP_LENGTH, SPARSE):

    jam = jams.JAMS(file_metadata=dict(duration=4.0))
    trans = pumpp.task.KeyTagTransformer(name='key',
                                         sr=SR,
                                         hop_length=HOP_LENGTH,
                                         sparse=SPARSE)

    output = trans.transform(jam)

    # Valid range is 0 since we have no matching namespace
    assert not np.any(output['key/_valid'])

    # Make sure it's all no-key
    Y_pred = trans.encoder.inverse_transform(output['key/tag'][0])

    assert all([_ == 'N' for _ in Y_pred])

    # Check the shape
    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
예제 #18
0
def test_task_dlabel_present(SR, HOP_LENGTH):
    labels = ['alpha', 'beta', 'psycho', 'aqua', 'disco']

    jam = jams.JAMS(file_metadata=dict(duration=4.0))

    ann = jams.Annotation(namespace='tag_open')

    ann.append(time=0, duration=1.0, value='alpha')
    ann.append(time=0, duration=1.0, value='beta')
    ann.append(time=1, duration=1.0, value='some nonsense')
    ann.append(time=3, duration=1.0, value='disco')

    jam.annotations.append(ann)
    trans = pumpp.task.DynamicLabelTransformer(name='madeup',
                                               namespace='tag_open',
                                               labels=labels)

    output = trans.transform(jam)

    # Mask should be true
    assert np.all(
        output['madeup/_valid'] == [0, 4 * trans.sr // trans.hop_length])

    y = output['madeup/tags']

    # Check the shape
    assert y.shape == (1, 4 * (SR // HOP_LENGTH), len(labels))

    # Decode the labels
    predictions = trans.encoder.inverse_transform(y[0, ::(SR // HOP_LENGTH)])

    true_labels = [['alpha', 'beta'], [], [], ['disco']]

    for truth, pred in zip(true_labels, predictions):
        assert set(truth) == set(pred)

    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
예제 #19
0
def test_task_slabel_present():
    labels = ['alpha', 'beta', 'psycho', 'aqua', 'disco']

    jam = jams.JAMS(file_metadata=dict(duration=4.0))

    ann = jams.Annotation(namespace='tag_open')

    ann.append(time=0, duration=1.0, value='alpha')
    ann.append(time=0, duration=1.0, value='beta')
    ann.append(time=1, duration=1.0, value='some nonsense')
    ann.append(time=3, duration=1.0, value='disco')

    jam.annotations.append(ann)
    trans = pumpp.task.StaticLabelTransformer(name='madeup',
                                              namespace='tag_open',
                                              labels=labels)

    output = trans.transform(jam)

    # Mask should be true
    assert np.all(
        output['madeup/_valid'] == [0, 4 * trans.sr // trans.hop_length])

    # Check the shape
    assert output['madeup/tags'].ndim == 2
    assert output['madeup/tags'].shape[1] == len(labels)

    # Decode the labels
    y_pred = output['madeup/tags'][0]
    predictions = trans.encoder.inverse_transform(y_pred.reshape((1, -1)))[0]

    true_labels = ['alpha', 'beta', 'disco']

    assert set(true_labels) == set(predictions)

    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
예제 #20
0
def test_task_beat_absent(SR, HOP_LENGTH):

    # Construct a jam
    jam = jams.JAMS(file_metadata=dict(duration=4.0))

    # One second = one frame
    trans = pumpp.task.BeatTransformer(name='beat')

    output = trans.transform(jam)

    # Make sure we have the mask
    assert not np.any(output['beat/_valid'])
    assert not output['beat/mask_downbeat']

    # Check the shape: 4 seconds at 2 samples per second
    assert output['beat/beat'].shape == (1, 4 * (SR // HOP_LENGTH), 1)
    assert output['beat/downbeat'].shape == (1, 4 * (SR // HOP_LENGTH), 1)
    assert not np.any(output['beat/beat'])
    assert not np.any(output['beat/downbeat'])

    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
예제 #21
0
def test_task_slabel_absent():
    labels = ['alpha', 'beta', 'psycho', 'aqua', 'disco']

    jam = jams.JAMS(file_metadata=dict(duration=4.0))
    trans = pumpp.task.StaticLabelTransformer(name='madeup',
                                              namespace='tag_open',
                                              labels=labels)

    output = trans.transform(jam)

    # Mask should be false since we have no matching namespace
    assert not np.any(output['madeup/_valid'])

    # Check the shape
    assert output['madeup/tags'].ndim == 2
    assert output['madeup/tags'].shape[1] == len(labels)

    # Make sure it's empty
    assert not np.any(output['madeup/tags'])

    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
예제 #22
0
def test_task_structure_absent(SR, HOP_LENGTH):
    jam = jams.JAMS(file_metadata=dict(duration=4.0))

    trans = pumpp.task.StructureTransformer(name='struct',
                                            sr=SR,
                                            hop_length=HOP_LENGTH)

    output = trans.transform(jam)

    # Mask should be true
    assert not np.any(output['struct/_valid'])

    y = output['struct/agree']

    # Check the shape
    assert y.shape == (1, 4 * (SR // HOP_LENGTH), 4 * (SR // HOP_LENGTH))

    # With a null structure annotation, all frames are similar
    assert np.all(y)

    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
예제 #23
0
def process_track(input_dir, output_dir, metadata, tags, compress):

    # Construct track metadata
    duration = get_track_duration(
        os.path.join(input_dir, 'mp3',
                     os.path.extsep.join([metadata['track'], 'mp3'])))

    artist, _ = metadata['track'].split('-', 1)

    artist = ARTIST_MAP.get(artist, artist)

    title = metadata['track'].replace('{:s}-'.format(artist), '')

    artist = artist.replace('_', ' ')
    title = title.replace('_', ' ')

    file_meta = jams.FileMetadata(title=title,
                                  artist=artist,
                                  duration=duration)

    # Get the tag annotation
    amd = jams.AnnotationMetadata(curator=jams.Curator(**__curator__),
                                  corpus=__corpus__)

    ann = jams.Annotation('tag_cal500', annotation_metadata=amd)

    for value, confidence in tags.iteritems():
        ann.append(time=0,
                   duration=duration,
                   value=value,
                   confidence=confidence)

    jam = jams.JAMS(file_metadata=file_meta)
    jam.annotations.append(ann)
    jam.sandbox.content_path = metadata['track']

    save_jam(output_dir, jam, metadata.name, compress)
예제 #24
0
def test_task_beatpos_tail(SR, HOP_LENGTH, SPARSE):
    # This test checks for implicit end-of-bar encodings
    jam = jams.JAMS(file_metadata=dict(duration=10.0))

    ann = jams.Annotation(namespace='beat')

    Y_true = [0, 2, 3, 1, 2, 3, 1, 2, 3]

    Y_true_out = [
        'X', '03/02', '03/03', '03/01', '03/02', '03/03', '03/01', '03/02',
        '03/03'
    ]

    for i, y in enumerate(Y_true):
        ann.append(time=i, duration=0, value=y)

    jam.annotations.append(ann)

    trans = pumpp.task.BeatPositionTransformer(name='beat',
                                               max_divisions=4,
                                               sr=SR,
                                               hop_length=HOP_LENGTH,
                                               sparse=SPARSE)

    output = trans.transform(jam)

    assert np.all(
        output['beat/_valid'] == [0, 10 * trans.sr // trans.hop_length])

    Y_pred = trans.encoder.inverse_transform(output['beat/position'][0])

    # This trimming is here because duration is inferred from the track,
    # not the ytrue_out
    Y_expected = np.repeat(Y_true_out, (SR // HOP_LENGTH),
                           axis=0).astype(Y_pred.dtype)
    for i, (y1, y2) in enumerate(zip(Y_pred, Y_expected)):
        assert y1 == y2
예제 #25
0
def test_task_simple_chord_present(SR, HOP_LENGTH):

    # Construct a jam
    jam = jams.JAMS(file_metadata=dict(duration=5.0))

    ann = jams.Annotation(namespace='chord')

    ann.append(time=0, duration=1.0, value='C:maj')
    ann.append(time=1, duration=1.0, value='C:maj/3')
    ann.append(time=3, duration=1.0, value='D:maj')
    ann.append(time=4, duration=1.0, value='N')

    jam.annotations.append(ann)

    # One second = one frame
    trans = pumpp.task.SimpleChordTransformer(name='chord_s')

    output = trans.transform(jam)

    # Make sure we have the mask
    assert np.all(
        output['chord_s/_valid'] == [0, 5 * trans.sr // trans.hop_length])

    # Ideal vectors:
    # pcp = Cmaj, Cmaj, N, Dmaj, N
    pcp_true = np.asarray([[1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
                           [1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

    assert np.all(output['chord_s/pitch'] == np.repeat(
        pcp_true, (SR // HOP_LENGTH), axis=0))
    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
def pred_to_jam(beats, pred):
    time = 0.0
    label = ud.labels[pred[0]]
    observations = []
    
    n = len(pred)
    for i in range(0,n):
        if ud.labels[pred[i]] == label: continue
        else:
            new_time = beats[i]
            new_label = ud.labels[pred[i]]

            observations.append(jams.Observation(time, new_time-time, label, None))

            time = new_time
            label = new_label
    observations.append(jams.Observation(time, beats[-1]-time, ud.labels[pred[-1]], None))

    annotation = jams.Annotation("segment_open"
                                , observations
                                , jd.get_annotation_metadata()
                                , jd.get_sandbox())
    jam = jams.JAMS([annotation], jams.FileMetadata(duration=beats[-1]))
    return jam
예제 #27
0
def test_task_beat_nometer():

    # Construct a jam
    jam = jams.JAMS(file_metadata=dict(duration=4.0))

    ann = jams.Annotation(namespace='beat')

    ann.append(time=0, duration=0.0)
    ann.append(time=1, duration=0.0)
    ann.append(time=2, duration=0.0)
    ann.append(time=3, duration=0.0)

    jam.annotations.append(ann)

    # One second = one frame
    T = crema.task.BeatTransformer()

    output = T.transform(jam)

    # Make sure we have the mask
    eq_(output['mask_beat'], True)
    eq_(output['mask_downbeat'], False)

    # Check the shape: 4 seconds at 2 samples per second
    assert np.allclose(output['output_beat'].shape, [4 * N_REPEAT, 1])
    assert np.allclose(output['output_downbeat'].shape, [4 * N_REPEAT, 1])

    # Ideal vectors:
    #   a beat every second (two samples)
    #   no downbeats

    beat_true = np.asarray([1, 1, 1, 1])
    downbeat_true = np.asarray([0, 0, 0, 0])

    assert np.allclose(output['output_beat'][::N_REPEAT], beat_true)
    assert np.allclose(output['output_downbeat'][::N_REPEAT], downbeat_true)
예제 #28
0
def test_task_beat_present():

    # Construct a jam
    jam = jams.JAMS(file_metadata=dict(duration=4.0))

    ann = jams.Annotation(namespace='beat')

    ann.append(time=0, duration=0.0, value=1)
    ann.append(time=1, duration=0.0, value=2)
    ann.append(time=2, duration=0.0, value=3)
    ann.append(time=3, duration=0.0, value=1)

    jam.annotations.append(ann)

    T = crema.task.BeatTransformer()

    output = T.transform(jam)

    # Make sure we have the masks
    eq_(output['mask_beat'], True)
    eq_(output['mask_downbeat'], True)

    # The first channel measures beats
    # The second channel measures downbeats
    assert np.allclose(output['output_beat'].shape, [4 * N_REPEAT, 1])
    assert np.allclose(output['output_downbeat'].shape, [4 * N_REPEAT, 1])

    # Ideal vectors:
    #   a beat every second (two samples)
    #   a downbeat every three seconds (6 samples)

    beat_true = np.asarray([[1, 1, 1, 1]]).T
    downbeat_true = np.asarray([[1, 0, 0, 1]]).T

    assert np.allclose(output['output_beat'][::N_REPEAT], beat_true)
    assert np.allclose(output['output_downbeat'][::N_REPEAT], downbeat_true)
예제 #29
0
def test_transform_noprefix():

    labels = ['foo', 'bar', 'baz']

    jam = jams.JAMS(file_metadata=dict(duration=4.0))
    trans = pumpp.task.StaticLabelTransformer(name=None,
                                              namespace='tag_open',
                                              labels=labels)

    output = trans.transform(jam)

    # Mask should be false since we have no matching namespace
    assert not np.any(output['_valid'])

    # Check the shape
    assert output['tags'].ndim == 2
    assert output['tags'].shape[1] == len(labels)

    # Make sure it's empty
    assert not np.any(output['tags'])

    for key in trans.fields:
        assert shape_match(output[key].shape[1:], trans.fields[key].shape)
        assert type_match(output[key].dtype, trans.fields[key].dtype)
예제 #30
0
def parse_patterns(csv_file, kern_file, patterns, out_file):
    """Parses the set of patterns and saves the results into the output file.

    Parameters
    ----------
    csv_file : str
        Path to the main csv file from which the pattern is extracted.
    kern_file : str
        Path to the main kern file from which to extract the metadata.
    patterns: list of list of strings (files)
        Set of all the patterns with the occurrences of a given piece.
    out_file: string (path)
        Path to the output file to save the set of patterns in the MIREX
        format.
    """
    # Create JAMS and add some metada
    jam = jams.JAMS()
    curator = jams.Curator(name="Tom Collins", email="*****@*****.**")
    fill_file_metadata(jam, kern_file, csv_file)
    ann_meta = jams.AnnotationMetadata(curator=curator,
                                       version="August2013",
                                       corpus="JKU Development Dataset")

    # Create actual annotation
    annot = jams.Annotation(namespace="pattern_jku",
                            annotation_metadata=ann_meta)

    # Get bpm and first and last onsets
    bpm = get_bpm(kern_file)
    first_onset, last_onset = get_first_last_onset(csv_file)

    pattern_n = 1
    for pattern in patterns:
        occ_n = 1
        for occ_file in pattern:
            start, end = find_in_csv(csv_file, occ_file)
            with open(csv_file, "r") as f:
                file_reader = list(csv.reader(f))
                for i in range(start, end):
                    value = {
                        "midi_pitch": float(file_reader[i][1]),
                        "morph_pitch": float(file_reader[i][2]),
                        "staff": int(float(
                            file_reader[i]
                            [4])),  # Hack to convert 0.000000000 into an int
                        "pattern_id": pattern_n,
                        "occurrence_id": occ_n
                    }
                    # Transform onset to time
                    time = onset_to_seconds(float(file_reader[i][0]),
                                            first_onset, bpm)
                    dur = onset_to_seconds(float(file_reader[i][3]), 0, bpm)
                    annot.data.add_observation(time=time,
                                               duration=dur,
                                               value=value)
            occ_n += 1
        pattern_n += 1

    # Annotation to the jams
    jam.annotations.append(annot)

    # Save file
    jam.save(out_file)