Beispiel #1
0
def flacize_after_sampling(output_folder,
                           groups,
                           sfzfile,
                           cleanup_aif_files=True):
    new_groups = []

    old_paths_to_unlink = [
        full_path(output_folder, r.attributes['sample']) for group in groups
        for r in group.regions
    ]

    for group in groups:
        # Make one FLAC file per key, to get more compression.
        output = sum([
            list(concat_samples(key_regions, output_folder, note_name(key)))
            for key, key_regions in group_by_attr(
                group.regions, ['key', 'pitch_keycenter']).iteritems()
        ], [])
        new_groups.append(Group(group.attributes, output))

    with open(sfzfile + '.flac.sfz', 'w') as file:
        file.write("\n".join([str(group) for group in new_groups]))

    if cleanup_aif_files:
        for path in old_paths_to_unlink:
            try:
                os.unlink(path)
            except OSError as e:
                print "Could not unlink path: %s: %s" % (path, e)
Beispiel #2
0
def quantize_velocity(regions, velocity_levels=5):
    lowestvel = min(map(lambda x: int(x.attributes['xfin_loivel']), regions))
    highestvel = max(map(lambda x: int(x.attributes['xfin_hivel']), regions))

    velspan = 127
    pitch_skip = velspan / velocity_levels

    evenly_divided = \
        int(keyspan / pitch_levels) == float(keyspan) / float(pitch_levels)

    # a dict of sample_pitch -> [lokey, hikey, pitch_keycenter]
    pitchmapping = {}
    for key in xrange(lowestkey + (pitch_skip / 2),
                      highestkey + 1 + (pitch_skip / 2), pitch_skip):
        pitchmapping[key] = {
            'lokey': key - (pitch_skip / 2),
            'pitch_keycenter': key,
            'hikey': key + (pitch_skip / 2) - (0 if evenly_divided else 1),
        }

    for key, regions in group_by_attr(regions, 'key').iteritems():
        if int(key) in pitchmapping:
            for region in regions:
                region.attributes.update(pitchmapping[int(key)])
                del region.attributes['key']
                yield region
Beispiel #3
0
            region.attributes['end'] = (global_offset + sample_length -
                                        ANTI_CLICK_OFFSET)
            # TODO: make sure endpoint is a zero crossing to prevent clicks
            region.attributes['sample'] = output_filename
            outfile.write("file '%s'\n" % full_path(path, sample))
            global_offset += sample_length

    create_flac(concat_filename, full_path(path, output_filename))
    os.unlink(concat_filename)

    return regions


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='flac-ize SFZ files into one sprite sample')
    parser.add_argument('files', type=str, help='files to process', nargs='+')
    args = parser.parse_args()

    for filename in args.files:
        for group in SFZFile(open(filename).read()).groups:
            # Make one FLAC file per key, to get more compression.
            output = sum([
                list(concat_samples(regions, filename, note_name(key)))
                for key, regions in tqdm(
                    group_by_attr(group.regions, 'key').iteritems())
            ], [])
            print group.just_group()
            for region in output:
                print region
Beispiel #4
0
            ('amp_velcurve_%d' % int(high.attributes['hivel'])):
            1,
            ('amp_velcurve_%d' % (int(high.attributes['lovel']) + 1)):
            diff,
        })
    # print the last region that didn't have a lower counterpart
    low = velsorted[-1]
    for attr in REMOVE_ATTRS:
        if attr in low.attributes:
            del low.attributes[attr]
    velcurve.update({
        ('amp_velcurve_%d' % int(low.attributes['hivel'])):
        1,
        ('amp_velcurve_%d' % (int(low.attributes['lovel']) + 1)):
        0,
    })
    return Group(velcurve, velsorted)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='volume-level sfz files with non-normalized samples')
    parser.add_argument('files', type=str, help='files to process', nargs='+')
    args = parser.parse_args()

    for filename in args.files:
        sfz = SFZFile(open(filename).read())
        regions = sum([group.regions for group in sfz.groups], [])
        for key, regions in group_by_attr(regions, 'key').iteritems():
            print level_volume(regions)