本文整理汇总了Python中alsaaudio.PCM属性的典型用法代码示例。如果您正苦于以下问题:Python alsaaudio.PCM属性的具体用法?Python alsaaudio.PCM怎么用?Python alsaaudio.PCM使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类alsaaudio
的用法示例。
在下文中一共展示了alsaaudio.PCM属性的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: start
# 需要导入模块: import alsaaudio [as 别名]
# 或者: from alsaaudio import PCM [as 别名]
def start():
last = GPIO.input(button)
while True:
val = GPIO.input(button)
GPIO.wait_for_edge(button, GPIO.FALLING) # we wait for the button to be pressed
GPIO.output(lights[1], GPIO.HIGH)
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, device)
inp.setchannels(1)
inp.setrate(16000)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(500)
audio = ""
while(GPIO.input(button)==0): # we keep recording while the button is pressed
l, data = inp.read()
if l:
audio += data
rf = open(path+'recording.wav', 'w')
rf.write(audio)
rf.close()
inp = None
alexa()
示例2: play
# 需要导入模块: import alsaaudio [as 别名]
# 或者: from alsaaudio import PCM [as 别名]
def play(f):
device = alsaaudio.PCM()
device.setchannels(f.getnchannels())
device.setrate(f.getframerate())
if f.getsampwidth() == 1:
device.setformat(alsaaudio.PCM_FORMAT_U8)
# Otherwise we assume signed data, little endian
elif f.getsampwidth() == 2:
device.setformat(alsaaudio.PCM_FORMAT_S16_LE)
elif f.getsampwidth() == 3:
device.setformat(alsaaudio.PCM_FORMAT_S24_LE)
elif f.getsampwidth() == 4:
device.setformat(alsaaudio.PCM_FORMAT_S32_LE)
else:
raise ValueError('Unsupported format')
device.setperiodsize(320)
data = f.readframes(320)
while data:
# Read data from stdin
device.write(data)
data = f.readframes(320)
示例3: doring
# 需要导入模块: import alsaaudio [as 别名]
# 或者: from alsaaudio import PCM [as 别名]
def doring(self):
if self.ringfile is not None:
self.ringfile.rewind()
else:
self.ringfile = wave.open(self.config["soundfiles"]["ringtone"], 'rb')
self.device = alsaaudio.PCM(card="pulse")
self.device.setchannels(self.ringfile.getnchannels())
self.device.setrate(self.ringfile.getframerate())
self.device.setperiodsize(320)
while self.shouldring:
data = self.ringfile.readframes(320)
while data:
self.device.write(data)
data = self.ringfile.readframes(320)
self.ringfile.rewind()
time.sleep(2)
if time.time() - 60 > self.ringstart:
self.stop()
示例4: run
# 需要导入模块: import alsaaudio [as 别名]
# 或者: from alsaaudio import PCM [as 别名]
def run(self):
# open audio file and device
audio_file = wave.open(self.soundfile, 'rb')
audio_device = alsaaudio.PCM(alsaaudio.PCM_PLAYBACK, alsaaudio.PCM_NORMAL, 'default')
# we are hard coding the audio format!
audio_device.setchannels(2)
audio_device.setrate(44100)
audio_device.setformat(alsaaudio.PCM_FORMAT_S16_LE)
audio_device.setperiodsize(980)
# play the audio
audio_data = audio_file.readframes(980)
while audio_data:
audio_device.write(audio_data)
audio_data = audio_file.readframes(980)
audio_file.close()
# Class for blinking the leds
示例5: acquire
# 需要导入模块: import alsaaudio [as 别名]
# 或者: from alsaaudio import PCM [as 别名]
def acquire(self):
if self._session.is_active():
try:
pcm_args = {
'type': alsa.PCM_PLAYBACK,
'mode': alsa.PCM_NORMAL,
}
if self._args.playback_device != 'default':
pcm_args['device'] = self._args.playback_device
else:
pcm_args['card'] = self._args.device
pcm = alsa.PCM(**pcm_args)
pcm.setchannels(CHANNELS)
pcm.setrate(RATE)
pcm.setperiodsize(PERIODSIZE)
pcm.setformat(alsa.PCM_FORMAT_S16_LE)
self._device = pcm
print "AlsaSink: device acquired"
except alsa.ALSAAudioError as error:
print "Unable to acquire device: ", error
self.release()
示例6: open_stream
# 需要导入模块: import alsaaudio [as 别名]
# 或者: from alsaaudio import PCM [as 别名]
def open_stream(self, bits, channels, rate, chunksize=1024, output=True):
# Check if format is supported
is_supported_fmt = self.supports_format(bits, channels, rate,
output=output)
if not is_supported_fmt:
msg_fmt = ("ALSAAudioDevice ({name}) doesn't support " +
"%s format (Int{bits}, {channels}-channel at" +
" {rate} Hz)") % ('output' if output else 'input')
msg = msg_fmt.format(name=self.name,
bits=bits,
channels=channels,
rate=rate)
self._logger.critical(msg)
raise Exception(msg)
# Everything looks fine, open the PCM stream
pcm_type = alsaaudio.PCM_PLAYBACK if output else alsaaudio.PCM_CAPTURE
stream = alsaaudio.PCM(type=pcm_type,
mode=alsaaudio.PCM_NORMAL,
device=self.name)
stream.setchannels(channels)
stream.setrate(rate)
stream.setformat(bits_to_samplefmt(bits))
stream.setperiodsize(chunksize)
self._logger.debug("%s stream opened on device '%s' (%d Hz, %d " +
"channel, %d bit)", "output" if output else "input",
self.slug, rate, channels, bits)
try:
yield stream
finally:
stream.close()
self._logger.debug("%s stream closed on device '%s'",
"output" if output else "input", self.slug)
示例7: playhandset
# 需要导入模块: import alsaaudio [as 别名]
# 或者: from alsaaudio import PCM [as 别名]
def playhandset(self):
print "Starting dialtone"
wv = wave.open(self.handsetfile)
device = alsaaudio.PCM(card="plug:external")
#device.setchannels(wv.getnchannels())
#device.setrate(wv.getframerate())
#device.setperiodsize(320)
data = wv.readframes(320)
while data and self.shouldplayhandset:
device.write(data)
data = wv.readframes(320)
wv.rewind()
wv.close()
示例8: playfile
# 需要导入模块: import alsaaudio [as 别名]
# 或者: from alsaaudio import PCM [as 别名]
def playfile(self, file):
wv = wave.open(file)
self.device = alsaaudio.PCM(card="pulse")
self.device.setchannels(wv.getnchannels())
self.device.setrate(wv.getframerate())
self.device.setperiodsize(320)
data = wv.readframes(320)
while data:
self.device.write(data)
data = wv.readframes(320)
wv.rewind()
wv.close()
示例9: play
# 需要导入模块: import alsaaudio [as 别名]
# 或者: from alsaaudio import PCM [as 别名]
def play(self, file_path):
if self.convert:
self.convert_mp3_to_wav(file_path_mp3=file_path)
f = wave.open(file_path, 'rb')
pcm_type = alsaaudio.PCM_PLAYBACK
stream = alsaaudio.PCM(type=pcm_type,
mode=alsaaudio.PCM_NORMAL,
device=self.device)
# Set attributes
stream.setchannels(f.getnchannels())
stream.setrate(f.getframerate())
bits = f.getsampwidth()*8
stream.setformat(bits_to_samplefmt(bits))
stream.setperiodsize(CHUNK)
logger.debug("[PyAlsaAudioPlayer] %d channels, %d sampling rate, %d bit" % (f.getnchannels(),
f.getframerate(),
bits))
data = f.readframes(CHUNK)
while data:
# Read data from stdin
stream.write(data)
data = f.readframes(CHUNK)
f.close()
stream.close()
示例10: open_stream
# 需要导入模块: import alsaaudio [as 别名]
# 或者: from alsaaudio import PCM [as 别名]
def open_stream(self, bits, channels, rate, chunksize=1024, output=True):
# Check if format is supported
is_supported_fmt = self.supports_format(bits, channels, rate,
output=output)
if not is_supported_fmt:
msg_fmt = ("ALSAAudioDevice ({name}) doesn't support " +
"%s format (Int{bits}, {channels}-channel at" +
" {rate} Hz)") % ('output' if output else 'input')
msg = msg_fmt.format(name=self.name,
bits=bits,
channels=channels,
rate=rate)
self._logger.critical(msg)
raise Exception(msg)
# Everything looks fine, open the PCM stream
pcm_type = alsaaudio.PCM_PLAYBACK if output else alsaaudio.PCM_CAPTURE
stream = alsaaudio.PCM(type=pcm_type,
mode=alsaaudio.PCM_NORMAL,
device='default')
stream.setchannels(channels)
stream.setrate(rate)
stream.setformat(bits_to_samplefmt(bits))
stream.setperiodsize(chunksize)
self._logger.debug("%s stream opened on device '%s' (%d Hz, %d " +
"channel, %d bit)", "output" if output else "input",
self.slug, rate, channels, bits)
try:
yield stream
finally:
stream.close()
self._logger.debug("%s stream closed on device '%s'",
"output" if output else "input", self.slug)
示例11: _record
# 需要导入模块: import alsaaudio [as 别名]
# 或者: from alsaaudio import PCM [as 别名]
def _record(self, cb):
inp = None
try:
inp = alsaaudio.PCM(
alsaaudio.PCM_CAPTURE,
alsaaudio.PCM_NORMAL,
device=self.record_device,
)
inp.setchannels(1)
inp.setrate(16000)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(1600) # 100ms
finalize = False
while not finalize:
l, data = inp.read()
if not self.currently_recording:
finalize = True
if l or finalize:
# self.recorded_raw.write(data)
cb(data, finalize)
except Exception:
print(traceback.format_exc())
finally:
self.currently_recording = False
if inp:
inp.close()
示例12: recordAudioSegments
# 需要导入模块: import alsaaudio [as 别名]
# 或者: from alsaaudio import PCM [as 别名]
def recordAudioSegments(RecordPath, BLOCKSIZE):
# This function is used for recording audio segments (until ctr+c is pressed)
# ARGUMENTS:
# - RecordPath: the path where the wav segments will be stored
# - BLOCKSIZE: segment recording size (in seconds)
#
# NOTE: filenames are based on clock() value
print "Press Ctr+C to stop recording"
RecordPath += os.sep
d = os.path.dirname(RecordPath)
if os.path.exists(d) and RecordPath!=".":
shutil.rmtree(RecordPath)
os.makedirs(RecordPath)
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
inp.setchannels(1)
inp.setrate(Fs)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(512)
midTermBufferSize = int(Fs*BLOCKSIZE)
midTermBuffer = []
curWindow = []
elapsedTime = "%08.3f" % (time.time())
while 1:
l,data = inp.read()
if l:
for i in range(len(data)/2):
curWindow.append(audioop.getsample(data, 2, i))
if (len(curWindow)+len(midTermBuffer)>midTermBufferSize):
samplesToCopyToMidBuffer = midTermBufferSize - len(midTermBuffer)
else:
samplesToCopyToMidBuffer = len(curWindow)
midTermBuffer = midTermBuffer + curWindow[0:samplesToCopyToMidBuffer];
del(curWindow[0:samplesToCopyToMidBuffer])
if len(midTermBuffer) == midTermBufferSize:
# allData = allData + midTermBuffer
curWavFileName = RecordPath + os.sep + str(elapsedTime) + ".wav"
midTermBufferArray = numpy.int16(midTermBuffer)
wavfile.write(curWavFileName, Fs, midTermBufferArray)
print "AUDIO OUTPUT: Saved " + curWavFileName
midTermBuffer = []
elapsedTime = "%08.3f" % (time.time())
示例13: __test_device
# 需要导入模块: import alsaaudio [as 别名]
# 或者: from alsaaudio import PCM [as 别名]
def __test_device(device, record):
"""
Test selected ALSA device, making sure it handles both stereo and
mono and both 44.1KHz and 22.05KHz on output, mono and 16 kHz on
input.
On a typical RPI configuration, default with hifiberry card is not
configured to do software-mono, so we'll use
plughw:CARD=sndrpihifiberry instead.
Likewise, on 2019 cards, hw:CARD=seeed2micvoicec is not able to run
mono sound.
@param device: name of the sound device
@type device: six.text_type
@param record: C{True} if this method is looking for recording
device. C{False} if the device should only playback.
@type record: bool
"""
try:
dev = None
if record:
dev = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, device=device)
else:
dev = alsaaudio.PCM(device=device)
if (
dev.setformat(alsaaudio.PCM_FORMAT_S16_LE)
!= alsaaudio.PCM_FORMAT_S16_LE
):
return False
if record:
if dev.setchannels(1) != 1:
return False
if dev.setrate(16000) != 16000:
return False
else:
if dev.setchannels(2) != 2:
return False
if dev.setchannels(1) != 1:
return False
if dev.setrate(44100) != 44100:
return False
if dev.setrate(22050) != 22050:
return False
except alsaaudio.ALSAAudioError:
return False
finally:
if dev:
dev.close()
return True