python版的讀取音效檔到常量數組

來源:互聯網
上載者:User

    通常我們播放音效檔的時候會通過指令碼讀取所有的音效檔,然後放到數組裡面,然後產生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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.