def run():
    if len(sys.argv) < 2:
        print("Usage: %s wavefile" % os.path.basename(sys.argv[0]))
        print("    Using an example wav file...")
        dirname = os.path.dirname(__file__)
        fname = os.path.join(dirname, "hey.wav")
    else:
        fname = sys.argv[1]

    sink = SoundSink()
    sink.activate()

    source = SoundSource(position=[10, 3, 3])
    source.looping = True

    data = load_wav_file(fname)
    source.queue(data)

    sink.play(source)
    while source.position[0] > -10:
        source.position = [
            source.position[0] - 1, source.position[1], source.position[2]
        ]
        sink.update()
        print("playing at %r" % source.position)
        time.sleep(1)
    print("done")
Exemplo n.º 2
0
 def play_sfx(self, name, offset=(0., 0., 0.), volume=1.):
     source = SoundSource()
     source.queue(load_wav_file(get_sfx(name)))
     source.gain = volume
     source.position = tuple(
         new_pt(*self.sink.listener.position) + new_pt(*offset))
     self.sink.play(source)
def run():
    if len (sys.argv) < 2:
        print ("Usage: %s wavefile" % os.path.basename(sys.argv[0]))
        print ("    Using an example wav file...")
        dirname = os.path.dirname(__file__)
        fname = os.path.join(dirname, "hey.wav")
    else:
        fname = sys.argv[1]


    sink = SoundSink()
    sink.activate()

    source = SoundSource(position=[10, 3, 3])
    source.looping = True

    data = load_wav_file(fname)
    source.queue(data)

    sink.play(source)
    while source.position[0] > -10:
        source.position = [source.position[0] - 1,
                           source.position[1],
                           source.position[2]]
        sink.update()
        print("playing at %r" % source.position)
        time.sleep(1)
    print("done")
Exemplo n.º 4
0
def make_sounds(items):

    sink = SoundSink()
    sink.activate()

    print("Frame with these items: " + str([item.label for item in items]))

    for item in items:
        label = item.label.strip()
        position = item.box.center
        area = item.box.area

        source_x = (position[0] - frame_width / 2) / (frame_width / 2) * 5
        source_z = -1 / math.sqrt(area / frame_area)

        print("{label} @ ({x:2f}, {z:2f})".format(label=label,
                                                  x=source_x,
                                                  z=source_z))

        base_name = os.path.join(temp_path, label)
        wav_file = base_name + ".wav"

        if (not os.path.exists(wav_file)):

            tts = gTTS(label)
            tts.save(base_name + '.mp3')

            sound = AudioSegment.from_mp3(base_name + '.mp3')
            sound.export(wav_file, format="wav")

        data = load_wav_file(wav_file)

        duration = 0.0

        with contextlib.closing(wave.open(wav_file, 'r')) as f:
            frames = f.getnframes()
            rate = f.getframerate()
            duration = frames / float(rate)

        source = SoundSource(position=[0, 0, 0])
        source.looping = False
        source.queue(data)

        sink.play(source)
        source.position = [source_x, 0, source_z]
        sink.update()
        time.sleep(duration + 0.1)
                    snake_direction = 0
                    listener.orientation = [0, 0, -1, 0, 1, 0]
                elif snake_direction == 2:
                    snake_direction = 1
                    listener.orientation = [1, 0, 0, 0, 1, 0]
                elif snake_direction == 3:
                    snake_direction = 2
                    listener.orientation = [0, 0, 1, 0, 1, 0]

    # Find if snake touched apple
    if (snake_xpos[0] + snake_cell_size > apple_pos[0]) and (snake_xpos[0] < apple_pos[0] + apple_size) and \
       (snake_ypos[0] + snake_cell_size > apple_pos[1]) and (snake_ypos[0] < apple_pos[1] + apple_size):
        # Place a new apple somewhere.
        apple_pos = (random.randint(0, screen_width - apple_size), random.randint(0, screen_height - apple_size))

        sound_source.position = (new_xposition(apple_pos[0]), 0, new_yposition(apple_pos[1]))

        # Snake's size should increase. Add some garbage element to snake_xpos and snake_ypos.
        # This cell would be placed at it's respective position later.
        snake_xpos.append(404)
        snake_ypos.append(404)

    # # Update position of snake's tail.
    # # Each cell should take position of cell in front of it.
    index = len(snake_xpos) - 1
    while index > 0:
        snake_xpos[index] = snake_xpos[index - 1]
        snake_ypos[index] = snake_ypos[index - 1]
        index -= 1

    # Update position of snake's head.
Exemplo n.º 6
0
import time
import math
from openal.audio import SoundSink, SoundSource
from openal.loaders import load_wav_file

if __name__ == "__main__":
    sink = SoundSink()
    sink.activate()
    source = SoundSource(position=[0, 0, 0])
    source.looping = True
    data = load_wav_file("./sounds/Blip_Select.wav")
    source.queue(data)
    sink.play(source)
    t = 0
    while True:
        x_pos = 5 * math.sin(math.radians(t))
        source.position = [x_pos, source.position[1], source.position[2]]
        sink.update()
        print("playing at %r" % source.position)
        time.sleep(0.1)
        t += 5
Exemplo n.º 7
0
from openal.audio import SoundData
from openal.loaders import load_wav_file
from openal.audio import SoundSink, SoundSource, SoundListener
import time
import math

if __name__ == "__main__":
    sink = SoundSink()
    sink.activate()
    listener = SoundListener()
    listener.orientation = (0,0,1,0,0,1)
    source1 = SoundSource(position=[0, 0, 3])
    source1.looping = True
    source2 = SoundSource(position=[0, 0, 3],pitch=2.0)
    source2.looping = True
    data2 = load_wav_file("./hey.wav")
    source1.queue(data2)
    source2.queue(data2)
    sink.play(source1)
    sink.play(source2)
    t = 0
    while True:
        x_pos = 5*math.sin(math.radians(t))
        source1.position = [x_pos, source1.position[1], source1.position[2]]
        source2.position = [0, source2.position[1], source2.position[2]]
        sink.update()
        print("playing source 1 at %r" % source1.position)
        print("playing source 2 at %r" % source2.position)
        time.sleep(0.1)
        t += 5