SHOW:
|
|
- or go back to the newest paste.
| 1 | - | local sm_file |
| 1 | + | |
| 2 | ['1000'] = '4', ['0100'] = '2', | |
| 3 | ['0010'] = '8', ['0001'] = '6', | |
| 4 | ['1100'] = '1', ['1010'] = '7', | |
| 5 | ['1001'] = 'B', ['0110'] = 'A', | |
| 6 | ['0101'] = '3', ['0011'] = '9', | |
| 7 | ['1110'] = '<81>', ['0111'] = '<83>', | |
| 8 | ['1101'] = '<61>', ['1011'] = '<8B>', | |
| 9 | ['1111'] = '<19>', ['0000'] = '0', | |
| 10 | } | |
| 11 | ||
| 12 | function string:getFieldContents(field) | |
| 13 | local _, idx = self:find('#' .. field .. ':')
| |
| 14 | - | function string:getField(field) |
| 14 | + | |
| 15 | - | local _, idx = self:find('#' .. field:upper() .. ':')
|
| 15 | + | |
| 16 | end | |
| 17 | ||
| 18 | local function processMeasure(measure) | |
| 19 | local beat_notes = {''} -- reserve beat_notes[1] for opening tag if necessary
| |
| 20 | local note_count = 0 | |
| 21 | - | local beat_notes = {''}
|
| 21 | + | |
| 22 | table.insert(beat_notes, note_lookup_table[note]) | |
| 23 | table.insert(beat_notes, '') -- for possible note padding | |
| 24 | - | beat_notes[#beat_notes+1] = note_lookup_table[note] |
| 24 | + | |
| 25 | - | beat_notes[#beat_notes+1] = '' |
| 25 | + | |
| 26 | ||
| 27 | if note_count == 4 then | |
| 28 | for i=3, 9, 2 do beat_notes[i] = '0' end -- add appropriate note padding | |
| 29 | elseif note_count == 8 then | |
| 30 | - | for i=3, 9, 2 do beat_notes[i] = '0' end |
| 30 | + | |
| 31 | - | elseif note_count == 8 then |
| 31 | + | |
| 32 | beat_notes[1] = '['; table.insert(beat_notes, ']') | |
| 33 | elseif note_count == 16 then | |
| 34 | - | beat_notes[1], beat_notes[#beat_notes+1] = '[', ']' |
| 34 | + | beat_notes[1] = '('; table.insert(beat_notes, ')')
|
| 35 | elseif note_count == 24 then | |
| 36 | - | beat_notes[1], beat_notes[#beat_notes+1] = '(', ')'
|
| 36 | + | beat_notes[1] = '['; table.insert(beat_notes, ']') |
| 37 | elseif note_count == 32 then | |
| 38 | - | beat_notes[1], beat_notes[#beat_notes+1] = '[', ']' |
| 38 | + | |
| 39 | beat_notes[1] = '{'; table.insert(beat_notes, '}')
| |
| 40 | elseif note_count == 48 then | |
| 41 | - | beat_notes[1], beat_notes[#beat_notes+1] = '{', '}'
|
| 41 | + | |
| 42 | beat_notes[1] = '`'; table.insert(beat_notes, '\'') | |
| 43 | elseif note_count == 64 then | |
| 44 | - | beat_notes[1], beat_notes[#beat_notes+1] = '`', '\'' |
| 44 | + | beat_notes[1] = '{'; table.insert(beat_notes, '}')
|
| 45 | elseif note_count == 96 then | |
| 46 | - | beat_notes[1], beat_notes[#beat_notes+1] = '{', '}'
|
| 46 | + | |
| 47 | beat_notes[1] = '`'; table.insert(beat_notes, '\'') | |
| 48 | elseif note_count == 192 then | |
| 49 | - | beat_notes[1], beat_notes[#beat_notes+1] = '`', '\'' |
| 49 | + | beat_notes[1] = '`'; table.insert(beat_notes, '\'') |
| 50 | else | |
| 51 | - | beat_notes[1], beat_notes[#beat_notes+1] = '`', '\'' |
| 51 | + | return nil -- invalid note count |
| 52 | end | |
| 53 | - | return nil |
| 53 | + | |
| 54 | return table.concat(beat_notes) .. '\n' | |
| 55 | end | |
| 56 | ||
| 57 | local function smToDwi(full_path) | |
| 58 | -- open sm file to read contents into string | |
| 59 | - | local sm_filename = 'test' |
| 59 | + | local sm_file |
| 60 | - | local tmp = io.open(sm_filename .. '.sm') |
| 60 | + | do |
| 61 | - | sm_file = tmp:read('*a')
|
| 61 | + | local tmp = io.open(full_path .. '.sm') |
| 62 | - | tmp:close() |
| 62 | + | sm_file = tmp:read('*a')
|
| 63 | tmp:close() | |
| 64 | - | local title = sm_file:getField('TITLE')
|
| 64 | + | |
| 65 | - | local artist = sm_file:getField('ARTIST')
|
| 65 | + | |
| 66 | - | local file = sm_file:getField('MUSIC')
|
| 66 | + | local changebpm = sm_file:getFieldContents('BPMS')
|
| 67 | - | local gap = tonumber(sm_file:getField('OFFSET')) * -1000
|
| 67 | + | local start_bpm |
| 68 | - | local sample_start = sm_file:getField('SAMPLESTART')
|
| 68 | + | do -- separate first bpm and bpm changes |
| 69 | - | local sample_length = sm_file:getField('SAMPLELENGTH')
|
| 69 | + | start_bpm = changebpm:match('%d+%.?%d*%s*=%s*(%d+%.?%d*)')
|
| 70 | - | local changebpm = sm_file:getField('BPMS')
|
| 70 | + | local _, idx = changebpm:find('%d+%.?%d*%s*=%s*%d+%.?%d*%s*,')
|
| 71 | - | local note_data, idx = sm_file:getField('NOTES')
|
| 71 | + | if idx then |
| 72 | changebpm = changebpm:sub(idx + 1) | |
| 73 | - | local start_bpm = changebpm:match('%d*%.?%d*%s*=%s*(%d+%.?%d+)')
|
| 73 | + | else |
| 74 | - | if start_bpm == nil then end |
| 74 | + | changebpm = '' |
| 75 | end | |
| 76 | - | local _, idx = changebpm:find('%d*%.?%d*%s*=%s*%d+%.?%d+%s*,')
|
| 76 | + | |
| 77 | - | if idx then |
| 77 | + | |
| 78 | - | changebpm = changebpm:sub(idx + 1) |
| 78 | + | -- multiply all measures by 4 to convert to beat counts |
| 79 | - | else |
| 79 | + | local changebpm_str = {}
|
| 80 | - | changebpm = '' end |
| 80 | + | if changebpm ~= '' then |
| 81 | for beat, bpm, comma in changebpm:gmatch('(%d+%.?%d*)%s*(=%s*%d+%.?%d*)(,?)') do
| |
| 82 | - | local changebpm_str = {}
|
| 82 | + | table.insert(changebpm_str, string.format("%.3f", tonumber(beat) * 4)..bpm)
|
| 83 | - | if changebpm ~= '' then |
| 83 | + | if comma ~= '' then table.insert(changebpm_str, ',') end |
| 84 | - | for beat, bpm, comma in changebpm:gmatch('(%d*%.?%d*)%s*(=%s*%d+%.?%d*)(,?)') do
|
| 84 | + | end |
| 85 | - | changebpm_str[#changebpm_str+1] = string.format("%.3f", tonumber(beat) * 4)..bpm
|
| 85 | + | |
| 86 | - | if comma ~= '' then changebpm_str[#changebpm_str+1] = ',' end |
| 86 | + | |
| 87 | -- process common .sm file fields | |
| 88 | local file_contents = {}
| |
| 89 | table.insert(file_contents, '#TITLE:' .. sm_file:getFieldContents('TITLE') .. ';\n')
| |
| 90 | - | local file_contents = {}
|
| 90 | + | table.insert(file_contents, '#ARTIST:' .. sm_file:getFieldContents('ARTIST') .. ';\n')
|
| 91 | - | file_contents[#file_contents+1] = '#TITLE:' .. sm_file:getField('TITLE') .. ';\n'
|
| 91 | + | table.insert(file_contents, '#FILE:' .. sm_file:getFieldContents('MUSIC') .. ';\n')
|
| 92 | - | file_contents[#file_contents+1] = '#ARTIST:' .. sm_file:getField('ARTIST') .. ';\n'
|
| 92 | + | table.insert(file_contents, '#BPM:' .. start_bpm .. ';\n') |
| 93 | - | file_contents[#file_contents+1] = '#FILE:' .. sm_file:getField('MUSIC') .. ';\n'
|
| 93 | + | table.insert(file_contents, '#GAP:' .. tonumber(sm_file:getFieldContents('OFFSET')) * -1000 .. ';\n')
|
| 94 | - | file_contents[#file_contents+1] = '#BPM:' .. start_bpm .. ';\n' |
| 94 | + | table.insert(file_contents, '#SAMPLESTART:' .. sm_file:getFieldContents('SAMPLESTART') .. ';\n')
|
| 95 | - | file_contents[#file_contents+1] = '#GAP:' .. tonumber(sm_file:getField('OFFSET')) * -1000 .. ';\n'
|
| 95 | + | table.insert(file_contents, '#SAMPLELENGTH:' .. sm_file:getFieldContents('SAMPLELENGTH') .. ';\n\n')
|
| 96 | - | file_contents[#file_contents+1] = '#SAMPLESTART:' .. sm_file:getField('SAMPLESTART') .. ';\n'
|
| 96 | + | if changebpm ~= '' then |
| 97 | - | file_contents[#file_contents+1] = '#SAMPLELENGTH:' .. sm_file:getField('SAMPLELENGTH') .. ';\n\n'
|
| 97 | + | table.insert(file_contents, '#CHANGEBPM:' .. table.concat(changebpm_str) .. ';\n') |
| 98 | - | if changebpm ~= '' then |
| 98 | + | |
| 99 | - | file_contents[#file_contents+1] = '#CHANGEBPM:' .. table.concat(changebpm_str) .. ';\n' |
| 99 | + | table.insert(file_contents, '#SINGLE:MANIAC:1:\n') |
| 100 | ||
| 101 | - | file_contents[#file_contents+1] = '#SINGLE:MANIAC:1:\n' |
| 101 | + | do -- extract start of note data to end of file |
| 102 | local _, idx = sm_file:find('%d+%.%d+%s*%:', idx)
| |
| 103 | - | local _, idx = sm_file:find('%d+%.%d+%s*%:', idx)
|
| 103 | + | sm_file = sm_file:sub(idx + 1) |
| 104 | - | sm_file = sm_file:sub(idx + 1) |
| 104 | + | |
| 105 | ||
| 106 | - | for measure in sm_file:gmatch('.-[,;]') do
|
| 106 | + | for measure in sm_file:gmatch('.-[,;]') do
|
| 107 | - | file_contents[#file_contents+1] = processMeasure(measure) |
| 107 | + | table.insert(file_contents, processMeasure(measure)) |
| 108 | end | |
| 109 | ||
| 110 | - | local dwi_file = io.open(sm_filename .. '.dwi', 'w') |
| 110 | + | -- save converted file |
| 111 | - | dwi_file:write(table.concat(file_contents)..'\n') |
| 111 | + | local dwi_file = io.open(full_path .. '.dwi', 'w') |
| 112 | - | dwi_file:close() |
| 112 | + | dwi_file:write(table.concat(file_contents)..'\n') |
| 113 | dwi_file:close() | |
| 114 | end | |
| 115 | ||
| 116 | smToDwi('test') |