mirror of
https://github.com/walshbp/pym.git
synced 2025-10-29 11:20:58 +00:00
fix sdl wav, added sample music
This commit is contained in:
parent
0caf1693b9
commit
84a1ebabe8
Binary file not shown.
7
examples/pym_sdl_wav/assets/contrib.txt
Normal file
7
examples/pym_sdl_wav/assets/contrib.txt
Normal file
@ -0,0 +1,7 @@
|
||||
The Elevator Game is by Daneiel Birch and is licensed under Creative Commons:
|
||||
|
||||
https://creativecommons.org/licenses/by-nc-nd/4.0/
|
||||
|
||||
It was originally obtained from:
|
||||
|
||||
http://freemusicarchive.org/music/Daniel_Birch/MUSIC_FOR_TV_FILM__GAMES_VOL1/The_Elevator_Game
|
||||
@ -45,7 +45,8 @@ def main():
|
||||
settings.shuffleEnabled = 1
|
||||
settings.softCutRatingsEnabled = 1
|
||||
base_path = ""
|
||||
settings.presetURL = base_path + "/home/bryan/projectm/src/projectM-sdl/presets/presets_milkdrop_200/";
|
||||
settings.presetURL = base_path + "/home/bryan//projectm_install/share/projectM/presets/"
|
||||
#settings.presetURL = base_path + "/home/bryan/projectm/src/projectM-sdl/presets/presets_milkdrop_200/";
|
||||
settings.menuFontURL = base_path + "fonts/Vera.ttf";
|
||||
settings.titleFontURL = base_path + "fonts/Vera.ttf";
|
||||
|
||||
@ -53,7 +54,7 @@ def main():
|
||||
app.init(window, render)
|
||||
|
||||
# get an audio input device
|
||||
app.play_audio("Firefly.wav")
|
||||
app.play_audio("Daniel_Birch_-_08_-_The_Elevator_Game.wav")
|
||||
|
||||
# standard main loop
|
||||
frame_delay = 1000/FPS;
|
||||
|
||||
@ -28,8 +28,7 @@ class WavSound(object):
|
||||
self._bufpos = 0
|
||||
|
||||
self.pcm = pcm
|
||||
self.want_spec = SDL_AudioSpec(freq = 48000, aformat = AUDIO_F32,channels = 2, samples = 512)
|
||||
#self.want_spec = SDL_AudioSpec(freq = 0 ,aformat = 0,channels = 0, samples = 0)
|
||||
self.want_spec = SDL_AudioSpec(freq = 48000, aformat = AUDIO_S16,channels = 2, samples = 512)
|
||||
self.have_spec = SDL_AudioSpec(freq = 0 ,aformat = 0,channels = 0, samples = 0)
|
||||
|
||||
self.done = False
|
||||
@ -42,11 +41,21 @@ class WavSound(object):
|
||||
def _load_file(self, file):
|
||||
rw = SDL_RWFromFile(byteify(file, "utf-8"), b"rb")
|
||||
sp = SDL_LoadWAV_RW(rw, 1, ctypes.byref(self.want_spec), ctypes.byref(self._buf), ctypes.byref(self._length))
|
||||
# print ("want.freq: %d" % self.want_spec.freq)
|
||||
# print ("want.is bytesize: %d" % SDL_AUDIO_BITSIZE(self.want_spec.format))
|
||||
# print ("want.is is big endian: %d" % SDL_AUDIO_ISBIGENDIAN(self.want_spec.format))
|
||||
# print ("want.is int: %d" % SDL_AUDIO_ISINT(self.want_spec.format))
|
||||
# print ("want.is signed: %d" % SDL_AUDIO_ISSIGNED(self.want_spec.format))
|
||||
# print ("want.aformat1: %d" % self.want_spec.format)
|
||||
# print ("want.aformat2: %d" % AUDIO_S16)
|
||||
# print ("want.channels: %d" % self.want_spec.channels)
|
||||
# print ("want.samples: %d" % self.want_spec.samples)
|
||||
if sp is None:
|
||||
raise RuntimeError("Could not open audio file: {}".format(SDL_GetError()))
|
||||
if self.want_spec.format != AUDIO_S16:
|
||||
raise RuntimeError("Unable to get Int16 audio format !")
|
||||
|
||||
def _play_next(self, notused, stream, len):
|
||||
#print("In Callback %d " % len)
|
||||
length = self._length.value
|
||||
numbytes = min(len, length - self._bufpos)
|
||||
for i in range(0, numbytes):
|
||||
@ -57,10 +66,11 @@ class WavSound(object):
|
||||
rest = min(0, len - numbytes)
|
||||
for i in range(0, rest):
|
||||
stream[i] = 0
|
||||
|
||||
|
||||
# Are we done playing sound?
|
||||
if self._bufpos == length:
|
||||
self.done = True
|
||||
self._bufpos = 0
|
||||
|
||||
#
|
||||
# https://stackoverflow.com/questions/4355524/getting-data-from-ctypes-array-into-numpy
|
||||
@ -68,11 +78,11 @@ class WavSound(object):
|
||||
|
||||
buffer_from_memory = ctypes.pythonapi.PyMemoryView_FromMemory
|
||||
buffer_from_memory.restype = ctypes.py_object
|
||||
buffer = buffer_from_memory(stream, len, PyBUF_WRITE)
|
||||
stream_out = numpy.frombuffer(buffer, numpy.float32)
|
||||
#for i in range(10):
|
||||
# print("Data2 %f" % stream_out[i])
|
||||
self.pcm.addPCMfloat(stream_out)
|
||||
buffer = buffer_from_memory(stream, min(2048,len/2), PyBUF_WRITE)
|
||||
|
||||
stream_out = numpy.frombuffer(buffer, numpy.int16)
|
||||
|
||||
self.pcm.addPCM16Data(stream_out)
|
||||
|
||||
|
||||
class PmSdl(object):
|
||||
@ -101,7 +111,7 @@ class PmSdl(object):
|
||||
raise RuntimeError("Unable to open audio device: {}".format(SDL_GetError()))
|
||||
|
||||
if self.sound.want_spec.format != self.sound.have_spec.format:
|
||||
raise RuntimeError("Unable to get Float32 audio: {}".format(SDL_GetError()))
|
||||
raise RuntimeError("Unable to get Int16 audio: {}".format(SDL_GetError()))
|
||||
print ("Format {}".format(self.sound.have_spec.format))
|
||||
SDL_PauseAudioDevice(self.devid, 0)
|
||||
|
||||
|
||||
3
pym.pyx
3
pym.pyx
@ -621,6 +621,9 @@ cdef class PCM:
|
||||
# self.c_instance.addPCM16(pcm16);
|
||||
|
||||
def addPCM16Data(self, short[::1] pcm_data) :
|
||||
#print("Len %d" %len(pcm_data))
|
||||
#for i in range(10):
|
||||
# print("Data %f" % pcm_data[i])
|
||||
self.c_instance.addPCM16Data(&pcm_data[0], len(pcm_data))
|
||||
|
||||
#def addPCM8(self, unsigned char [:,:] pcm8):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user