zoukankan      html  css  js  c++  java
  • python版的读取声音文件到常量数组

        通常我们播放声音文件的时候会通过脚本读取所有的声音文件,然后放到数组里面,然后生成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)

  • 相关阅读:
    linux System V IPC Mechanisms
    linux pipes
    linux create a process
    linux processes identifiers
    linux processes
    beaglebone-black reference url
    git commit steps(1)
    hadoop hadoop install (1)
    OpenWrite方法打开现有文件并进行写入
    OpenRead方法打开文件并读取
  • 原文地址:https://www.cnblogs.com/secbook/p/2655403.html
Copyright © 2011-2022 走看看