通常我們播放音效檔的時候會通過指令碼讀取所有的音效檔,然後放到數組裡面,然後產生int型的索引,這樣播放的時候就不直接傳聲音名字了,
而是這樣playMusic( MUICS_BG_1 }, playSound( SE_DOG ); 這樣無論聲音格式如何變化,代碼都無需改變。
之前都是Perl寫的,這次為了和其他人保持一致改成python版的了,貌似沒啥區別。貼代碼備份
總目錄media,下列 video, music, sound 三個目錄,分別是視頻,背景音和音效
#Auther: lancer
#Data: 2011-6-7
#Function: Get name of all media files from folder and process it, then write to another file as array format.
#Version: 1.00
#Modified
#Version: 1.01
#Data:
import os
import re
import shutil
media_path = "C:\\workspace\\bada\\PigeonSquadron\\trunk\\code\\res\\media\\"
to_process_file = "C:\\workspace\\bada\\PigeonSquadron\\trunk\\code\\src\\native\\inc\\bada\SoundEngine.h"
index_prefix = '#define '
max_suffix = '_MAPPING_COUNT'
print media_path
code_array_list = []
def add_define(name, filelist):
index = 0
code_array_list.append(index_prefix + '' + 'SOUND_NONE ' + str(-1) + '\n')
for file in filelist:
if not re.match('.svn\Z', file):
file = name + '_' + file.replace(' ','')
file = re.sub('(.mp3|.aac|.wma|.m4a|.xmf|.3ga|.mmf|.mid|.wav|.amr|.mp4)\Z', '', file)
file = file.upper()
file = index_prefix + file + ' ' + str(index) + '\n'
code_array_list.append(file)
index += 1
count = index_prefix + name.upper() + max_suffix + ' ' + str(index) + '\n\n'
code_array_list.append(count)
#static char* s_videoFiles[ VIDEO_MAPPING_COUNT ] =
#{
#"badaBI.mp4",
#};
def add_array(name, filelist):
start = 'static char* s_' + name + 'Files[ ' + name.upper() + max_suffix + ' ] = ' + '\n{\n'
end = '};\n\n'
code_array_list.append(start);
for file in filelist:
if not re.match('.svn\Z', file):
code_array_list.append(' "' + file + '",\n');
code_array_list.append(end);
def process_folder(name):
filelist = os.listdir(media_path+name)
#print filelist
add_define(name, filelist)
add_array(name, filelist)
#read files from media folder and compose to list
list_files = os.listdir(media_path)
#cmd = "dir /A-D /B"
#list_file = os.popen(cmd).readlines();
#print list_files
for folder in list_files:
if folder=='sound' or folder=='music' or folder=='video':
#print "found path:"+folder
process_folder(folder)
else:
print "useless folder: " + folder
#for line in code_array_list:
#print line
file_to_write = 'temp.txt'
ignore_input = False
#open code file that include sound list and replace it
readf = file(to_process_file, 'r') # open for read
writef = file(file_to_write, 'w') # open for write
while True:
line = readf.readline()
if len(line) == 0: # Zero length indicates EOF
break
#print line,
if not ignore_input:
writef.write(line) # write text to file
if re.match('\A//# sound list to be processed start', line):
ignore_input = True
else:
if re.match('\A//# sound list to be processed end', line):
ignore_input = False
for item in code_array_list:
writef.write(item)
writef.write(line) # write text to file
# Notice comma to avoid automatic newline added by Python
readf.close() # close the file
writef.close();
shutil.move(file_to_write, to_process_file)