Beispiel #1
0
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()

    xint = int(x.get())
    yint = int(y.get())
    zint = int(z.get())
    source = SoundSource(position=[xint, yint, zint])
    source.looping = False

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

    sink.play(source)

    sink.update()
    time.sleep(2)
    print("done")
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")
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")
def mainloop():
    listener = SoundListener()
    listener.orientation = (0, 0, -1, 0, 1, 0)

    sink = SoundSink()
    sink.activate()

    rain = place_sound([0, -7, -1], load_sound("rfrog.wav"), True)
    sink.play(rain)

    birds = place_sound([5, 5, 5], load_sound("chirps1.wav"), True)
    sink.play(birds)

    creek = place_sound([-7, -7, 7], load_sound("creek.wav"), True)
    sink.play(creek)

    while 1 == 1:
        birds.position = move_source_random(birds, -8, 8, 0, 7, -10, 10)
        char = ord(getch.getch())

        if char == 97:
            listener.orientation = orient_listener(listener.orientation, False)

        if char == 100:
            listener.orientation = orient_listener(listener.orientation, True)

        if char == 112:
            break

        sink.update()
Beispiel #5
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)
Beispiel #6
0
class AudioOut:
    def __init__(self, parent, wav_file_paths):
        self.parent = parent
        self.wav_file_paths = wav_file_paths
        self.snd_src = None
        self.sink = SoundSink()
        self.sink.activate()
        self.listener = SoundListener()
        self.sink.listener = self.listener
        self.snd_data = []
        for fp in wav_file_paths: self.snd_data.append( load_wav_file(fp) )
        self.wav_file_paths = wav_file_paths
        writeFile(self.parent.log_file_path, '%s, [audioOut], audioOut mod init.\n'%(get_time_stamp()))
        
    # --------------------------------------------------
    
    def play(self, snd_idx, loop=True):
        self.snd_src = SoundSource(gain=0.25, position=[0,0,0])
        self.snd_src.looping = loop
        self.snd_src.queue( self.snd_data[snd_idx] )
        self.sink.play(self.snd_src)
        self.sink.update()
        writeFile(self.parent.log_file_path, '%s, [audioOut], sound (%s) play starts.\n'%(get_time_stamp(), self.wav_file_paths[snd_idx]))
    
    # --------------------------------------------------
    
    def move(self, pos):
        self.snd_src.position = [ pos[0], pos[1], pos[2] ]
        if pos[0] == 0: self.snd_src.gain = 0.25
        else: self.snd_src.gain = 0.5
        self.sink.update()
       
    # --------------------------------------------------
    
    def stop(self):
        if self.snd_src != None:
            self.sink.stop(self.snd_src)
            self.sink.update()
            self.snd_src = None
        writeFile(self.parent.log_file_path, '%s, [audioOut], sound stopped.\n'%(get_time_stamp()))
    # Update position of snake's head.
    if snake_direction == 0:
        snake_ypos[0] -= snake_cell_size
    if snake_direction == 1:
        snake_xpos[0] += snake_cell_size
    if snake_direction == 2:
        snake_ypos[0] += snake_cell_size
    if snake_direction == 3:
        snake_xpos[0] -= snake_cell_size

    if snake_xpos[0] < 0:
        snake_xpos[0] = screen_width
    if snake_xpos[0] > screen_width:
        snake_xpos[0] = 0

    if snake_ypos[0] < 0:
        snake_ypos[0] = screen_height
    if snake_ypos[0] > screen_height:
        snake_ypos[0] = 0

    listener.position = (new_xposition(snake_xpos[0]), 0, new_yposition(snake_ypos[0]))

    # Render image of apple and snake on screen
    screen.fill((0, 255, 0))
    screen.blit(apple_image, apple_pos)
    for (x, y) in zip(snake_xpos, snake_ypos):
        screen.blit(snake_image, (x, y))

    print sound_source.position, listener.position
    sound_sink.update()
    pygame.display.update()
Beispiel #8
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
class AudioManager:
    format = paInt16
    chunk_size = 1024
    rate = 44100

    audio_scale = 10.

    def __init__(self, conn: DirectConnection):
        self.p = PyAudio()
        atexit.register(self.p.terminate)
        self.stream = None  # type: Stream

        self.sink = SoundSink()
        self.sink.activate()

        self.source = SoundSource()

        def close():
            del self.sink

        atexit.register(close)

        self.source.gain = 1.0
        self.pos = self._pos = (random(), random())
        self.sink.play(self.source)

        self.conn = conn
        self.mic_data = Queue()
        self.speaker_data = Queue()
        self.energy_tracker = EnergyTracker(self.rate)

    @property
    def pos(self):
        return self._pos

    @pos.setter
    def pos(self, pos):
        self.source.position = (self.audio_scale * pos[0], 0,
                                self.audio_scale * pos[1])
        self._pos = pos

    def create_stream(self):
        if self.stream:
            raise RuntimeError('Already created stream!')
        stream = self.p.open(format=self.format,
                             channels=1,
                             rate=self.rate,
                             input=True,
                             output=False,
                             stream_callback=self.callback,
                             frames_per_buffer=self.chunk_size)
        atexit.register(stream.close)
        atexit.register(stream.stop_stream)
        stream.start_stream()
        self.stream = stream
        Thread(target=self._stream_thread, daemon=True).start()

    def _stream_thread(self):
        while True:
            new_chunk = self.mic_data.get()
            audio = np.fromstring(new_chunk, dtype=np.int16)
            audio = audio.astype(np.float32) / float(np.iinfo(audio.dtype).max)
            if not self.energy_tracker.update(audio):
                continue

            self.conn.send(
                json.dumps({
                    'type': 'audio',
                    'audio': b64encode(new_chunk).decode('ascii')
                }))

    def update(self):
        self.sink.update()

    def process_audio_message(self, message):
        new_chunk = b64decode(message['audio'])
        print('Add', len(new_chunk))
        self.source.queue(
            SoundData(new_chunk, 1, 16, len(new_chunk), self.rate))

    def callback(self, in_data, frame_count, time_info, status):
        self.mic_data.put(in_data)
        return None, paContinue
Beispiel #10
0
class stereosound:
    def __init__(self):
        self.sink = SoundSink()
        self.sink.activate()
        self.listener = SoundListener()
        self.listener.orientation = (0,0,1,0,0,1)
        self.sources = [SoundSource(position = [i, 0, 0], pitch = 1) for i in range(-1, 2, 2)]
        self.intense_sources = [SoundSource(position = [i, 0, 0], pitch = 1) for i in range(-1, 2, 2)]
        #pitch: 5,4,3,2,1
        self.data = load_wav_file("./beep.wav")
        self.intense_data = load_wav_file("./high_beep.wav")
        '''
        for source in self.sources:
            #we want the sources to be looping
            source.looping = True
            source.queue(self.data)
        '''
        for source in self.intense_sources:
            source.looping = True
            source.queue(self.intense_data)
        self.threading_pool = []
        for mild_source in self.sources:
            t = sound_source(self.sink, mild_source)
            self.threading_pool.append(t)
        for intense_source in self.intense_sources:
            t = sound_source(self.sink, intense_source)
            self.threading_pool.append(t)
        #threading pool: mild left; mild right; i left; i right
        #self.sink.play(source1)
        #self.sink.play(source2)
        self.cutoff = [i * (config.MAX_DISTANCE / len(self.sources)) for i in range(len(self.sources))]

    #return index for desired sound source in cutoff
    def determine_cutoff(self, avg_dis):
        for n in range(len(self.cutoff) - 1):
            lower_bound = self.cutoff[n]
            upper_bound = self.cutoff[n + 1]
            if(avg_dis >= lower_bound and avg_dis <= upper_bound):
                return n
        return len(self.cutoff) - 1

    def alert(self, ear = "left", intensity = "mild", command = "start"):
        if ear == "left":
            first_mul = 0
        else:
            first_mul = 1
        if intensity == "mild":
            second_mul = 0
        else:
            second_mul = 2
        if command == "start":
            self.threading_pool[first_mul + second_mul].start()
        elif command == "stop":
            self.threading_pool[first_mul + second_mul].stop()

    def _play(self, coord_tuple, avg_dis, val = "low"):
        if(not (np.array(coord_tuple).any())): return
        source_ind = self.determine_cutoff(avg_dis)
        print "calling sources" + str(source_ind)
        use_source = self.sources[source_ind]
        #embed()
        if val == "low":
            use_source.queue(self.data)
        if val == "high":
            use_source.queue(self.intense_data)
        coord_tuple = normalize(coord_tuple)
        use_source.position = [coord_tuple[0], coord_tuple[1], coord_tuple[2]]
        #time.sleep(0.02)
        self.sink.update()
        self.sink.play(use_source)