Ad
Pyaudio / Numpy Concatenated Notes Merely Extend The First Note
I'm trying to generate my own notes using pyaudio, but I'm running into what is surely a beginner's mistake. I can generate pure sin wav tones and play them sequentially, but if I try to concatenate them, I don't get three notes in a row, I get the original note played three times as long.
import numpy as np
import pyaudio
def play_note(note):
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=44100,
output=True)
stream.write(np.asarray(note, dtype=np.float32))
stream.stop_stream()
stream.close()
p.terminate()
sampling_rate = 44100
seconds = 1
x = np.arange(sampling_rate * seconds)
freqs = [440,660,880]
notes = []
for freq in freqs:
note = 100*np.sin(2 * np.pi * freq * x /sampling_rate)
notes.append(note)
# This is the behavior I want
for note in notes:
play_note(note)
# I would expect this to behave similarly, but without the gaps. It doesn't.
note = np.concatenate(notes)
play_note(note)
Ad
Answer
I get the same result with pyaudio 0.2.11 (running on Mac OS 10.12.6). I was able to fix it by adding the argument frames_per_buffer=1
to p.open()
and num_frames=len(note)
to stream.write()
:
def play_note(note):
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=44100,
output=True,
frames_per_buffer=1)
stream.write(np.asarray(note, dtype=np.float32), num_frames=len(note))
stream.stop_stream()
stream.close()
p.terminate()
I haven't investigated further to answer why the original version doesn't work or why this change fixes it. Perhaps a pyaudio guru will give a more thorough answer.
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad