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)

  • 相关阅读:
    day7 面向对象 静态方法 类方法 属性方法 类的特殊成员方法 元类 反射 异常处理
    day6 面向对象 封装 继承 多态 类与实例在内存中的关系 经典类和新式类
    day5 time datetime random os sys shutil json pickle shelve xml configparser hashlib subprocess logging re正则 python计算器
    kafka常用操作命令
    linux基础
    django学习1——初识web应用程序
    mysql数据库(三)——pymysql模块
    mysql数据库(二)——表的查询
    mysql数据库(一)
    Python常用模块——re模块
  • 原文地址:https://www.cnblogs.com/secbook/p/2655403.html
Copyright © 2011-2022 走看看