Skip to content

Commit

Permalink
support wav format, support audio no interrupt mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Ran Duan committed Nov 9, 2021
1 parent 87e2858 commit 9fcf17b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
build/
dist/
__pycache__/
*.spec
*.spec
voice_bank/zqq/*
tempCodeRUnnerFile.py
reaper_file/
4 changes: 1 addition & 3 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@


在voice_bank中建立文件夹,里面放MP3文件。
在voice_bank中建立文件夹,里面放MP3或wav文件。

若文件夹以yue开头,则对任意按键,会随机播放文件夹里的音频。

Expand Down
11 changes: 9 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import os
import pygame

print(os.listdir("voice_bank\yue_315-2"))
NUM_MIXER_CHANNEL = 10

mixer = pygame.mixer
mixer.init()
mixer.set_num_channels(NUM_MIXER_CHANNEL)
mixer.Channel(0).play(mixer.Sound("voice_bank\\zqq\\a.mp3"))
# import time
# time.sleep(5)
33 changes: 24 additions & 9 deletions yue.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,44 @@
import random
import logging

NUM_MIXER_CHANNEL = 10
current_mixer_channel = 0
VOICE_BANK = None

mixer = pygame.mixer
mixer.init()
mixer.set_num_channels(NUM_MIXER_CHANNEL)
music = pygame.mixer.music

mixer.init()

VOICE_BANK = None

def on_press(key):
global current_mixer_channel
try:
if VOICE_BANK.startswith("yue"):
if key is None:
print("unknown key")
elif VOICE_BANK.startswith("yue"):
yue_list = os.listdir(os.path.join("voice_bank", VOICE_BANK))
path = os.path.join("voice_bank", VOICE_BANK, random.choice(yue_list))
else:
elif hasattr(key, "char"):
keychar = key.char
path = os.path.join("voice_bank", VOICE_BANK, keychar + ".mp3")
else:
keystring = str(key).split(".")[-1]
path = os.path.join("voice_bank", VOICE_BANK, keystring + ".mp3")
# logging.debug("play:", path)
music.load(path)
music.play()
# music.load(path)
# music.play()
if not os.path.exists(path):
path = path[:-4] + ".wav"
print(path)
mixer.Channel(current_mixer_channel).play(mixer.Sound(path))
current_mixer_channel = (current_mixer_channel + 1) % NUM_MIXER_CHANNEL
# time.sleep(5)
except AttributeError:
# logging.debug('special key {0} pressed'.format(key))
print('special key {0} pressed'.format(key))
pass
except Exception as e:
# logging.debug(e)
print(e)
pass

def on_release(key):
Expand Down

0 comments on commit 9fcf17b

Please sign in to comment.