音声の波形表示

投稿ツリー


このトピックの投稿一覧へ

通常 音声の波形表示

msg# 1
depth:
0
前の投稿 - 次の投稿 | 親投稿 - 子投稿.1 | 投稿日時 2012/3/3 22:52
Takeshi  新米 居住地: 愛知  投稿数: 9
お久しぶりです。
前からBlenderで音声波形表示をしてみたかったので
スクリプトを書いてみました。

少し古いスクリプトの解説がこちらです。
http://www.nicovideo.jp/watch/sm17099891

こっちにも、掲載しておきました
http://takeshiblackfox.blog16.fc2.com/blog-entry-6.html

一切の責任は負いませんが、自由に使っていただいてかまいません。
二次配布も、改変もOKです。


Add-onとして登録してONにしておけば、toolshelfから起動できます
bl_info = {
    "name": "Create Wave Form",
    "author": "Taketan",
    "version": (1, 0),
    "blender": (2, 6, 2),
    "api": 44136,
    "location": "View3D > Tool Shelf > AudioWave",
    "description": "Add AudioWave from wav file",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Add Object"}


import wave
import struct
import bpy
from bpy.props import *
from math import *



class AudioWave_OT(bpy.types.Operator):
    bl_description = 'Add Wave Form from wav file'
    bl_idname = 'audiowave.create'
    bl_label = 'Add AudioWave'
    bl_undo = True
    
    @classmethod
    def makeMaterial(self,name, diffuse, specular, alpha):
        mat = bpy.data.materials.new(name)
        mat.diffuse_color = diffuse
        mat.diffuse_shader = "LAMBERT"
        mat.diffuse_intensity = 1.0
        mat.specular_color = specular
        mat.specular_shader = "COOKTORR"
        mat.specular_intensity = 0.5
        mat.alpha = alpha
        mat.ambient = 1
        return mat

    def setMaterial(self,ob, mat):
        me = ob.data
        me.materials.append(mat)

    def addaudiowave(self,file_path,obj_number,offset_time,space,intensity,obj_type):
        
        ## GetParam ###################################
        mat             = self.makeMaterial("AudioMat", (1,0.5,0.5), (1,1,1), 1)
        frame_start = bpy.context.scene.frame_start
        frame_end =  bpy.context.scene.frame_end
        nframe = frame_end - frame_start + 1
        
        ## Read Wave File ########################
        try:
            wf = wave.open(file_path, "r")
            w_nframe    = wf.getnframes()
            w_rframe    = wf.getframerate()
            w_nchannel  = wf.getnchannels()  
            data=[]
            for c_frame in range(frame_start,frame_end + 1):
                if c_frame*w_rframe/24 >= w_nframe:
                    break
                wf.setpos(int(w_rframe*(c_frame/24+offset_time)))
                w_buffer = wf.readframes(obj_number)
                data.append(struct.unpack("{0}h".format(w_nchannel*obj_number),w_buffer))
            wf.close()
        except:
            print("Can't open wav file")
            return



        ## 
        for obj_n in range(obj_number):
            if obj_type == "0":
                bpy.ops.mesh.primitive_uv_sphere_add(segments=16, ring_count=8, size=0.08, location=(obj_n*space,0,0))
            else:
                bpy.ops.mesh.primitive_cube_add(location=(obj_n*space,0,0))
            obj = bpy.context.object
            if obj_type == "1":
                obj.scale *= 0.05
            self.setMaterial(obj, mat)
            obj.animation_data_create()
            obj.animation_data.action = bpy.data.actions.new(name="MyAction")
            fcu_z = obj.animation_data.action.fcurves.new(data_path="location", index=2)
            fcu_z.keyframe_points.add(nframe)
        
            i=0
            for c_frame in range(frame_start, frame_end+1):
                if c_frame*w_rframe/24 >= w_nframe:
                    fcu_z.keyframe_points[i].co = c_frame, 0
                else:
                    z = data[i][w_nchannel*obj_n]
                    position = z*(0.5 - 0.5*cos(2*pi*obj_n/obj_number))*intensity
                    fcu_z.keyframe_points[i].co = c_frame, position
                i+=1
        
    
    def execute(self, context=None):
        scene = bpy.context.scene
        
        file_path = scene.audiowave_file_path
        obj_number = scene.audiowave_obj_number
        offset_time = scene.audiowave_offset_time
        space = scene.audiowave_space
        intensity = scene.audiowave_intensity
        obj_type = scene.audiowave_obj_type
        
        self.addaudiowave(file_path,obj_number,offset_time,space,intensity,obj_type)
        return {'FINISHED'}


class AudioWave_PT(bpy.types.Panel):
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_label = "AudioWave"
    
    def draw(self, context):
        layout = self.layout
        scene = bpy.context.scene
        layout.prop(scene, "audiowave_file_path", text="filepath")
        layout.prop(scene, "audiowave_obj_number", text="amount")
        layout.prop(scene, "audiowave_offset_time", text="offset time")
        layout.prop(scene, "audiowave_space", text="space")
        layout.prop(scene, "audiowave_intensity", text="intensity")
        layout.prop(scene, "audiowave_obj_type", expand=True)
        layout.operator("audiowave.create", text="Create")
        
### Registration ###
def register():
    bpy.utils.register_class(AudioWave_OT)
    bpy.utils.register_class(AudioWave_PT)
    
    ## append property
    bpy.types.Scene.audiowave_file_path = StringProperty(
        name="wav file path",
        description="Select wav file for create audiowave",
        default="",
        maxlen=255,
        options={'ANIMATABLE'},
        subtype='FILE_PATH')
    bpy.types.Scene.audiowave_obj_number = IntProperty(
        name="Wave obj number",
        description="number of audiowave's particle obj",
        default=50,
        min=1,
        max=1000)
    bpy.types.Scene.audiowave_offset_time = FloatProperty(
        name="Offset Time",
        description="Offset audio [s]",
        default=0)
    bpy.types.Scene.audiowave_space = FloatProperty(
        name="Space while particle",
        description="Space while particle obj",
        default=0.1)
    bpy.types.Scene.audiowave_intensity = FloatProperty(
        name="AudioWave intensity",
        description="intensity of audiowave",
        default=0.0002)

    bpy.types.Scene.audiowave_obj_type = EnumProperty(
        name="Obj type",
        description="Select obj type",
        items=[("0","uv sphere","uv sphere"),
               ("1","cube","cube")],
        default='1')


def unregister():
    bpy.utils.unregister_class(AudioWave_OT)
    bpy.utils.unregister_class(AudioWave_PT)
    
    ## del property
    del bpy.types.Scene.audiowave_filepath
    del bpy.types.Scene.audiowave_obj_number
    del bpy.types.Scene.audiowave_offset_time
    del bpy.types.Scene.audiowave_space
    del bpy.types.Scene.audiowave_intensity
    del bpy.tepes.Scene.audiowave_obj_type

if __name__ == '__main__':
    register()


投票数:22 平均点:2.27

  条件検索へ


ログイン

ユーザ名:

パスワード:



パスワード紛失

クイックリンク

2021/07/01版
●Blender.org
BlenderFoundation
- Blenderのダウンロード
- 公式チュート等
- 公式マニュアル(和訳)

●ニュース(英文)
BlenderNation

●Blenderコミュニティ
blenderartists.org

●Blender Q&A
- Blender Stack Exchange

●テストビルド
Buildbot(自動生成)


●開発関連
公式開発サイト
Blender開発blog
Blender Wiki