def midi_to_lilypond(midi_fname, output_fname=None, overwrite=False):
    """Convert the given midi file to a lilypond source file.

    This uses the command midi2ly with the absolute pitches and explicit durations flag.
    Afterwards it will remove all utf-8 from the source file.

    Args:
        midi_fname (str): the filename of the midi file
        output_fname (str): the output lilypond file if None we use the basename of the midi file
        overwrite (boolean): if we overwrite the lilypond file if it already exists

    Returns:
        str: the path to the lilypond source file
    """
    output_fname = output_fname or os.path.splitext(midi_fname)[0] + '.ly'

    if overwrite:
        if os.path.isfile(output_fname):
            os.remove(output_fname)
    else:
        if os.path.isfile(output_fname):
            return output_fname

    run_command(['midi2ly', '-a', '-e', '-o', output_fname, midi_fname])
    run_command(['iconv', '-f', 'utf-8', '-t', 'utf-8', '-c', output_fname, '-o', output_fname])

    return output_fname
Exemple #2
0
 def convert(self, midi_fname, wav_fname):
     ensure_dir_exists(wav_fname)
     run_command('fluidsynth -g {gain} -F {wav} {soundfont} {midi}'.format(
         wav=wav_fname,
         soundfont=self._sound_font,
         midi=midi_fname,
         gain=self.gain * 10))
Exemple #3
0
def draw_rectangle(image_fname, fill_color, stroke_color, stroke_width, position, end_position, output_fname=None):
    """Draw a rectangle on a given image.

    Args:
        image_fname: the image to draw on
        fill_color (str): the imagemagick color to use for filling
        stroke_color (str): the imagemagick color for the border
        stroke_width (int): the width fo the stroke
        position (tuple): the x,y position of the top-left of the rectangle
        end_position (tuple): the x,y end position of the rectangle to draw
        output_fname: the output image, if not set it defaults to the input image

    """
    if not bash_function_exists('convert'):
        raise RuntimeError('The function convert does not exists, please install ImageMagick or some similar tool.')

    output_fname = output_fname or image_fname

    command = ['convert', '-quality', '100', '-fill', fill_color, '-stroke', stroke_color,
               '-strokewidth', str(stroke_width),
               '-draw', 'rectangle ' + ','.join(map(str, position)) + ' ' + ','.join(map(str, end_position)),
               image_fname, output_fname]
    run_command(command)

    return image_fname
Exemple #4
0
def midi_to_lilypond(midi_fname, output_fname=None, overwrite=False):
    """Convert the given midi file to a lilypond source file.

    This uses the command midi2ly with the absolute pitches and explicit durations flag.
    Afterwards it will remove all utf-8 from the source file.

    Args:
        midi_fname (str): the filename of the midi file
        output_fname (str): the output lilypond file if None we use the basename of the midi file
        overwrite (boolean): if we overwrite the lilypond file if it already exists

    Returns:
        str: the path to the lilypond source file
    """
    output_fname = output_fname or os.path.splitext(midi_fname)[0] + '.ly'

    if overwrite:
        if os.path.isfile(output_fname):
            os.remove(output_fname)
    else:
        if os.path.isfile(output_fname):
            return output_fname

    run_command(['midi2ly', '-a', '-e', '-o', output_fname, midi_fname])
    run_command([
        'iconv', '-f', 'utf-8', '-t', 'utf-8', '-c', output_fname, '-o',
        output_fname
    ])

    return output_fname
Exemple #5
0
    def convert(self, midi_fname, wav_fname):
        ensure_dir_exists(wav_fname)

        with tempfile.NamedTemporaryFile('w') as tmp_file:
            tmp_file.write('soundfont {}'.format(self._sound_font))
            tmp_file.flush()
            run_command('timidity -c {config} --output-24bit -A120 -Ow -o {wav} {midi}'.format(
                config=tmp_file.name, wav=wav_fname, midi=midi_fname))
Exemple #6
0
    def convert(self, midi_fname, wav_fname):
        ensure_dir_exists(wav_fname)

        with tempfile.NamedTemporaryFile('w') as tmp_file:
            tmp_file.write('soundfont {}'.format(self._sound_font))
            tmp_file.flush()
            run_command(
                'timidity -c {config} --output-24bit -A120 -Ow -o {wav} {midi}'
                .format(config=tmp_file.name, wav=wav_fname, midi=midi_fname))
Exemple #7
0
def concatenate_images(output_fname, image_list):
    """Append all the given files to each other in the order given.

    Args:
        output_fname (str): the output filename
        image_list (list of str): the filenames of the images to append
    """
    if not bash_function_exists('convert'):
        raise RuntimeError('The function convert does not exists, please install ImageMagick or some similar tool.')

    ensure_dir_exists(output_fname)
    command = ['convert', '-append']
    command.extend(image_list)
    command.append(output_fname)
    run_command(command)
Exemple #8
0
def trim_image(image_fname, output_fname=None):
    """Trim the given images.

    Args:
        image_fname: the image to trim
        output_fname: the output image, if not set it defaults to the input image

    Returns:
        str: the name of the output image
    """
    if not bash_function_exists('convert'):
        raise RuntimeError('The function convert does not exists, please install ImageMagick or some similar tool.')

    output_fname = output_fname or image_fname
    run_command(['convert', '-trim', image_fname, output_fname])
    return output_fname
Exemple #9
0
def get_image_size(image_fname):
    """Get the size of the given image.

    Args:
        image_fname (str): the image we want to get the width and height of

    Returns:
        tuple: width, height of the given image
    """
    if not bash_function_exists('identify'):
        raise RuntimeError('The function convert does not exists, please install ImageMagick or some similar tool.')
    value = run_command(['identify', '-format', '%[fx:w]x%[fx:h]', image_fname]).decode("utf-8")
    return list(map(int, value.split('x')))
Exemple #10
0
 def convert(self, midi_fname, wav_fname):
     ensure_dir_exists(wav_fname)
     run_command('fluidsynth -g {gain} -F {wav} {soundfont} {midi}'.format(
         wav=wav_fname, soundfont=self._sound_font, midi=midi_fname, gain=self.gain * 10))
Exemple #11
0
 def to_ogg(self, wav_fname, output_fname):
     ensure_dir_exists(output_fname)
     remove_file_if_exists(output_fname)
     run_command('{command} -i {wav} -acodec libvorbis {ogg}'.format(
         command=self.command_name, wav=wav_fname, ogg=output_fname))
Exemple #12
0
 def to_mp3(self, wav_fname, output_fname):
     ensure_dir_exists(output_fname)
     remove_file_if_exists(output_fname)
     run_command('{command} -i {wav} -vn -ar 44100 -ac 2 -ab 192k -f mp3 {mp3}'.format(
         command=self.command_name, wav=wav_fname, mp3=output_fname))
Exemple #13
0
 def to_ogg(self, wav_fname, output_fname):
     ensure_dir_exists(output_fname)
     remove_file_if_exists(output_fname)
     run_command('{command} -i {wav} -acodec libvorbis {ogg}'.format(
         command=self.command_name, wav=wav_fname, ogg=output_fname))
Exemple #14
0
 def to_mp3(self, wav_fname, output_fname):
     ensure_dir_exists(output_fname)
     remove_file_if_exists(output_fname)
     run_command(
         '{command} -i {wav} -vn -ar 44100 -ac 2 -ab 192k -f mp3 {mp3}'.
         format(command=self.command_name, wav=wav_fname, mp3=output_fname))