Skip to content

Instantly share code, notes, and snippets.

@igorgue
Last active October 3, 2019 14:05
Show Gist options
  • Select an option

  • Save igorgue/96dea6585f7d6c3b9d2d to your computer and use it in GitHub Desktop.

Select an option

Save igorgue/96dea6585f7d6c3b9d2d to your computer and use it in GitHub Desktop.

Revisions

  1. igorgue revised this gist Feb 7, 2016. 1 changed file with 1 addition and 7 deletions.
    8 changes: 1 addition & 7 deletions grandpa_script.rb
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,7 @@
    # What's this?
    # ============
    #
    # A script that converts all kinds of files to the file format Balst Instruments GrandPA requires.
    #
    # 1. Grab each file from the dir
    # 2. See supported file types
    # 3. Convert all supported to .wav
    # 4. Get a list of all grandpa formats nad start poping file names
    # 5. Rename all .wav to grandpa format
    # A script that converts all kinds of files to the file format Bastl Instruments GrandPA requires.
    #
    # Install
    # =======
  2. igorgue created this gist Feb 7, 2016.
    65 changes: 65 additions & 0 deletions grandpa_script.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    #!/usr/bin/env ruby

    # What's this?
    # ============
    #
    # A script that converts all kinds of files to the file format Balst Instruments GrandPA requires.
    #
    # 1. Grab each file from the dir
    # 2. See supported file types
    # 3. Convert all supported to .wav
    # 4. Get a list of all grandpa formats nad start poping file names
    # 5. Rename all .wav to grandpa format
    #
    # Install
    # =======
    #
    # 1. Put all your files inside of the root dir of the SD card
    # 2. Copy this file to it
    # 3. Open terminal and run it

    cwd = File.expand_path(File.dirname(__FILE__))
    supported_extentions = ['.mp3', '.wav', '.aiff', '.ogg']

    # According to the manual:
    #
    # The files on the SD card need to be in the root directory and have specific
    # names e.g.: P0.wav -P9.wav, PA.wav -PZ.wav etc. The first letter of the name
    # has to be a capital P and second # letter 0-9 or A-Z (also capital).
    #
    # Samples have to be 22050hz, 16bit, mono wav files. (they can also be 44.1khz, but the sample rate will not
    # allow pitch up then).
    numbers_files = (0..9).map { |number| "P#{number}.wav" }
    letters_files = ('A'..'Z').map { |letter| "P#{letter}.wav" }
    grandpa_language_file_names = numbers_files + letters_files
    failed_files = [] # due to the limited amount of file names we can have

    ('A'..'Z').each do |letters|
    grandpa_language_file_names
    end

    Dir.glob("#{cwd}/*").each do |file|
    next unless File::file?(file)
    next unless supported_extentions.any? { |extension| file.end_with? extension }

    if grandpa_language_file_names.any?
    grandpa_name = grandpa_language_file_names.shift
    command_to_run = "ffmpeg -y -i #{file} -acodec pcm_s16le -ac 1 -ar 22050 #{grandpa_name}"

    puts "ORIGINAL FILE: #{file} ... GRANDPA NAME: #{grandpa_name}"

    # Run command, oh ruby joy
    puts command_to_run
    `#{command_to_run}`
    else
    failed_files << file
    end
    end

    if failed_files.any?
    puts "The following files did not convert due to the limited amount of files names we can have: \n"

    failed_files.each do |failed_file|
    puts failed_file
    end
    end