Advertisement
Guest User

dwi converter WIP

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