Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local note_lookup_table = {
- ['1000'] = '4', ['0100'] = '2',
- ['0010'] = '8', ['0001'] = '6',
- ['1100'] = '1', ['1010'] = '7',
- ['1001'] = 'B', ['0110'] = 'A',
- ['0101'] = '3', ['0011'] = '9',
- ['1110'] = '<81>', ['0111'] = '<83>',
- ['1101'] = '<61>', ['1011'] = '<8B>',
- ['1111'] = '<19>', ['0000'] = '0',
- }
- function string:getFieldContents(field)
- local _, idx = self:find('#' .. field .. ':')
- local str = self:match('%b:;', idx)
- return str:sub(2, #str - 1)
- end
- local function processMeasure(measure)
- local beat_notes = {''} -- reserve beat_notes[1] for opening tag if necessary
- local note_count = 0
- for note in measure:gmatch('%d%d%d%d') do
- table.insert(beat_notes, note_lookup_table[note])
- table.insert(beat_notes, '') -- for possible note padding
- note_count = note_count + 1
- end
- if note_count == 4 then
- for i=3, 9, 2 do beat_notes[i] = '0' end -- add appropriate note padding
- elseif note_count == 8 then
- elseif note_count == 12 then
- for i=3, 25, 2 do beat_notes[i] = '0' end
- beat_notes[1] = '['; table.insert(beat_notes, ']')
- elseif note_count == 16 then
- beat_notes[1] = '('; table.insert(beat_notes, ')')
- elseif note_count == 24 then
- beat_notes[1] = '['; table.insert(beat_notes, ']')
- elseif note_count == 32 then
- for i=3, 65, 2 do beat_notes[i] = '0' end
- beat_notes[1] = '{'; table.insert(beat_notes, '}')
- elseif note_count == 48 then
- for i=3, 97, 2 do beat_notes[i] = '000' end
- beat_notes[1] = '`'; table.insert(beat_notes, '\'')
- elseif note_count == 64 then
- beat_notes[1] = '{'; table.insert(beat_notes, '}')
- elseif note_count == 96 then
- for i=3, 193, 2 do beat_notes[i] = '0' end
- beat_notes[1] = '`'; table.insert(beat_notes, '\'')
- elseif note_count == 192 then
- beat_notes[1] = '`'; table.insert(beat_notes, '\'')
- else
- return nil -- invalid note count
- end
- return table.concat(beat_notes) .. '\n'
- end
- local function smToDwi(full_path)
- -- open sm file to read contents into string
- local sm_file
- do
- local tmp = io.open(full_path .. '.sm')
- sm_file = tmp:read('*a')
- tmp:close()
- end
- local changebpm = sm_file:getFieldContents('BPMS')
- local start_bpm
- do -- separate first bpm and bpm changes
- start_bpm = changebpm:match('%d+%.?%d*%s*=%s*(%d+%.?%d*)')
- local _, idx = changebpm:find('%d+%.?%d*%s*=%s*%d+%.?%d*%s*,')
- if idx then
- changebpm = changebpm:sub(idx + 1)
- else
- changebpm = ''
- end
- end
- -- multiply all measures by 4 to convert to beat counts
- local changebpm_str = {}
- if changebpm ~= '' then
- for beat, bpm, comma in changebpm:gmatch('(%d+%.?%d*)%s*(=%s*%d+%.?%d*)(,?)') do
- table.insert(changebpm_str, string.format("%.3f", tonumber(beat) * 4)..bpm)
- if comma ~= '' then table.insert(changebpm_str, ',') end
- end
- end
- -- process common .sm file fields
- local file_contents = {}
- table.insert(file_contents, '#TITLE:' .. sm_file:getFieldContents('TITLE') .. ';\n')
- table.insert(file_contents, '#ARTIST:' .. sm_file:getFieldContents('ARTIST') .. ';\n')
- table.insert(file_contents, '#FILE:' .. sm_file:getFieldContents('MUSIC') .. ';\n')
- table.insert(file_contents, '#BPM:' .. start_bpm .. ';\n')
- table.insert(file_contents, '#GAP:' .. tonumber(sm_file:getFieldContents('OFFSET')) * -1000 .. ';\n')
- table.insert(file_contents, '#SAMPLESTART:' .. sm_file:getFieldContents('SAMPLESTART') .. ';\n')
- table.insert(file_contents, '#SAMPLELENGTH:' .. sm_file:getFieldContents('SAMPLELENGTH') .. ';\n\n')
- if changebpm ~= '' then
- table.insert(file_contents, '#CHANGEBPM:' .. table.concat(changebpm_str) .. ';\n')
- end
- table.insert(file_contents, '#SINGLE:MANIAC:1:\n')
- do -- extract start of note data to end of file
- local _, idx = sm_file:find('%d+%.%d+%s*%:', idx)
- sm_file = sm_file:sub(idx + 1)
- end
- for measure in sm_file:gmatch('.-[,;]') do
- table.insert(file_contents, processMeasure(measure))
- end
- -- save converted file
- local dwi_file = io.open(full_path .. '.dwi', 'w')
- dwi_file:write(table.concat(file_contents)..'\n')
- dwi_file:close()
- end
- smToDwi('test')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement