LoginSignup
0
1

More than 1 year has passed since last update.

ADX2スクリプトで原音再生

Last updated at Posted at 2021-07-16

内容

pyaudioを利用するとオーディオの解析とかできそうなので
とりあえず選択したウェーブフォームの元波形を再生するものを作ってみた

環境:CRI Atom Craft 3.45.00

pyaudioのインストール

windowsでpython3.8を使っているのですが、pyaudioがインストールできなくて、
これ
を参考にインストールした。

pipwin
何か非公式なものみたいだけど・・・とりあえず動いた

スクリプト

PlayWav.py
# --Description:[tatmos][Preview]選択したオブジェクトの原音を再生

import pyaudio
import wave
import time
import cri.atomcraft.project as acproject
import cri.atomcraft.debug as acdebug

p = pyaudio.PyAudio()

#--------
#AUDIOのDEVICEを列挙
#hostAPICount = p.get_host_api_count()
#for cnt in range(hostAPICount):
#    print(a.get_host_api_info_by_index(cnt))

#音を鳴らすDEVICEを指定
DEVICE_INDEX = 0

#--------

#選択中のマテリアルまたはウェーブフォームリージョンまたはキューを得る
selectedMaterials = acproject.get_selected_objects("Material")["data"]
selectedWaveformRegions = acproject.get_selected_objects("WaveformRegion")["data"]
selectedCues = acproject.get_selected_objects("Cue")["data"]

selectedMaterial = None

if len(selectedMaterials) > 0:
    selectedMaterial = selectedMaterials[0]
if len(selectedCues) > 0:
    selectedWaveformRegions = acproject.find_objects(selectedCues[0],"WaveformRegion")["data"]
if len(selectedWaveformRegions) > 0:
    selectedMaterial = acproject.get_value(selectedWaveformRegions[0], "LinkMaterial")["data"]

if not selectedMaterial:
    acdebug.warning("再生するマテリアルまたはウェーブフォームリージョンまたはキューを選択してください。")
    sys.exit()

print(acproject.get_value(selectedMaterial, "Name")["data"])

input_path = acproject.get_value(selectedMaterial, "SrcFileAbsolutePath")["data"]

if not input_path:
    acdebug.warning("マテリアルのパスがみつかりません。")
    sys.exit()

print("Path:" + str(input_path))

#--------
#ファイルを開く
wf = wave.open(input_path, "rb")


def callback(in_data, frame_count, time_info, status):
    data = wf.readframes(frame_count)
    return (data, pyaudio.paContinue)

#ストリームの作成
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                input_device_index = DEVICE_INDEX,
                output=True,
                stream_callback=callback)

#再生
stream.start_stream()
print("再生中 ... " + input_path)

#再生終わりまで待つ
while stream.is_active():
    time.sleep(0.1)

print("done.")
stream.stop_stream()
stream.close()
p.terminate()
wf.close()
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1