Advertisement
Guest User

dwi converter

a guest
Jul 18th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.94 KB | None | 0 0
  1. local sm_file
  2.  
  3. local note_lookup_table = {
  4.     ['1000'] = '4',    ['0100'] = '2',
  5.     ['0010'] = '8',    ['0001'] = '6',
  6.     ['1100'] = '1',    ['1010'] = '7',
  7.     ['1001'] = 'B',    ['0110'] = 'A',
  8.     ['0101'] = '3',    ['0011'] = '9',
  9.     ['1110'] = '<81>', ['0111'] = '<83>',
  10.     ['1101'] = '<61>', ['1011'] = '<8B>',
  11.     ['1111'] = '<19>', ['0000'] = '0',
  12. }
  13.  
  14. function string:getField(field)
  15.     local _, idx = self:find('#' .. field:upper() .. ':')
  16.     local str = self:match('%b:;', idx)
  17.     return str:sub(2, #str - 1)
  18. end
  19.  
  20. local function processMeasure(measure)
  21.     local beat_notes = {''}
  22.     local note_count = 0
  23.     for note in measure:gmatch('%d%d%d%d') do
  24.         beat_notes[#beat_notes+1] = note_lookup_table[note]
  25.         beat_notes[#beat_notes+1] = ''
  26.         note_count = note_count + 1
  27.     end
  28.  
  29.     if note_count == 4 then
  30.         for i=3, 9, 2 do beat_notes[i] = '0' end
  31.     elseif note_count == 8 then
  32.     elseif note_count == 12 then
  33.         for i=3, 25, 2 do beat_notes[i] = '0' end
  34.         beat_notes[1], beat_notes[#beat_notes+1] = '[', ']'
  35.     elseif note_count == 16 then
  36.         beat_notes[1], beat_notes[#beat_notes+1] = '(', ')'
  37.     elseif note_count == 24 then
  38.         beat_notes[1], beat_notes[#beat_notes+1] = '[', ']'
  39.     elseif note_count == 32 then
  40.         for i=3, 65, 2 do beat_notes[i] = '0' end
  41.         beat_notes[1], beat_notes[#beat_notes+1] = '{', '}'
  42.     elseif note_count == 48 then
  43.         for i=3, 97, 2 do beat_notes[i] = '000' end
  44.         beat_notes[1], beat_notes[#beat_notes+1] = '`', '\''
  45.     elseif note_count == 64 then
  46.         beat_notes[1], beat_notes[#beat_notes+1] = '{', '}'
  47.     elseif note_count == 96 then
  48.         for i=3, 193, 2 do beat_notes[i] = '0' end
  49.         beat_notes[1], beat_notes[#beat_notes+1] = '`', '\''
  50.     elseif note_count == 192 then
  51.         beat_notes[1], beat_notes[#beat_notes+1] = '`', '\''
  52.     else
  53.         return nil
  54.     end
  55.  
  56.     return table.concat(beat_notes) .. '\n'
  57. end
  58.  
  59. local sm_filename = 'test'
  60. local tmp = io.open(sm_filename .. '.sm')
  61. sm_file = tmp:read('*a')
  62. tmp:close()
  63.  
  64. local title = sm_file:getField('TITLE')
  65. local artist = sm_file:getField('ARTIST')
  66. local file = sm_file:getField('MUSIC')
  67. local gap = tonumber(sm_file:getField('OFFSET')) * -1000
  68. local sample_start = sm_file:getField('SAMPLESTART')
  69. local sample_length = sm_file:getField('SAMPLELENGTH')
  70. local changebpm = sm_file:getField('BPMS')
  71. local note_data, idx = sm_file:getField('NOTES')
  72.  
  73. local start_bpm = changebpm:match('%d*%.?%d*%s*=%s*(%d+%.?%d+)')
  74. if start_bpm == nil then end
  75.  
  76. local _, idx = changebpm:find('%d*%.?%d*%s*=%s*%d+%.?%d+%s*,')
  77. if idx then
  78.     changebpm = changebpm:sub(idx + 1)
  79. else
  80.     changebpm = '' end
  81.  
  82. local changebpm_str = {}
  83. if changebpm ~= '' then
  84.     for beat, bpm, comma in changebpm:gmatch('(%d*%.?%d*)%s*(=%s*%d+%.?%d*)(,?)') do
  85.         changebpm_str[#changebpm_str+1] = string.format("%.3f", tonumber(beat) * 4)..bpm
  86.         if comma ~= '' then changebpm_str[#changebpm_str+1] = ',' end
  87.     end
  88. end
  89.  
  90. local file_contents = {}
  91. file_contents[#file_contents+1] = '#TITLE:'         .. sm_file:getField('TITLE')                    .. ';\n'
  92. file_contents[#file_contents+1] = '#ARTIST:'        .. sm_file:getField('ARTIST')                   .. ';\n'
  93. file_contents[#file_contents+1] = '#FILE:'          .. sm_file:getField('MUSIC')                    .. ';\n'
  94. file_contents[#file_contents+1] = '#BPM:'           .. start_bpm                                    .. ';\n'
  95. file_contents[#file_contents+1] = '#GAP:'           .. tonumber(sm_file:getField('OFFSET')) * -1000 .. ';\n'
  96. file_contents[#file_contents+1] = '#SAMPLESTART:'   .. sm_file:getField('SAMPLESTART')              .. ';\n'
  97. file_contents[#file_contents+1] = '#SAMPLELENGTH:'  .. sm_file:getField('SAMPLELENGTH')             .. ';\n\n'
  98. if changebpm ~= '' then
  99.     file_contents[#file_contents+1] = '#CHANGEBPM:' .. table.concat(changebpm_str)                  .. ';\n'
  100. end
  101. file_contents[#file_contents+1] = '#SINGLE:MANIAC:1:\n'
  102.  
  103. local _, idx = sm_file:find('%d+%.%d+%s*%:', idx)
  104. sm_file = sm_file:sub(idx + 1)
  105.  
  106. for measure in sm_file:gmatch('.-[,;]') do
  107.     file_contents[#file_contents+1] = processMeasure(measure)
  108. end
  109.  
  110. local dwi_file = io.open(sm_filename .. '.dwi', 'w')
  111. dwi_file:write(table.concat(file_contents)..'\n')
  112. dwi_file:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement