IOError: [Errno -9998] Invalid number of channels when using PyAudio on Spyder (Python 2.7)

0

I'm fairly new to coding and I'm trying to make an audio visualizer on Spyder. Currently I've been getting an error message which is:

    runfile('/Users/christinanguyen/Downloads/audio visualizer code.py', wdir='/Users/christinanguyen/Downloads')
Traceback (most recent call last):

  File "<ipython-input-34-a74bf7660894>", line 1, in <module>
    runfile('/Users/christinanguyen/Downloads/audio visualizer code.py', wdir='/Users/christinanguyen/Downloads')

  File "/Users/christinanguyen/anaconda2/lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 704, in runfile
    execfile(filename, namespace)

  File "/Users/christinanguyen/anaconda2/lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 100, in execfile
    builtins.execfile(filename, *where)

  File "/Users/christinanguyen/Downloads/audio visualizer code.py", line 22, in <module>
    input_device_index = 2)

  File "/Users/christinanguyen/anaconda2/lib/python2.7/site-packages/pyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)

  File "/Users/christinanguyen/anaconda2/lib/python2.7/site-packages/pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)

IOError: [Errno -9998] Invalid number of channels

this is my code for the visualizer so far:

import pygame as pg
import pyaudio
import numpy as np
from pygame.locals import (HWSURFACE, DOUBLEBUF, RESIZABLE)  
from math import ceil

CHUNK =512
drawChunk = 32
RATE = 44100
CHANNELS = 1



p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
              channels=1,
              input=True,
              rate=RATE,
              frames_per_buffer=CHUNK,
              input_device_index = 2)
def get_dataS(stream,CHUNK):
    data = np.fromstring(stream.read(CHUNK,exception_on_overflow = False),dtype=np.int16)
    peak=np.average(np.abs(data))
    return(peak/50)


pg.init()
screen = pg.display.set_mode((500,500),HWSURFACE|DOUBLEBUF|RESIZABLE)
clock = pg.time.Clock()
done = False
spriteGroup = pg.sprite.Group()
drawGroup = pg.strite.Group()
bars = 70
flashclock = 0
flashdur = 50

class SimpleBeatDetection:
   def __init__(self, history = 10):
       self.local_energy = np.zeros(history) #simple ring buffer
       self.local_energy_index = 0 #the index of the oldest element

   def detect_beat(self, signal):

       samples = signal.astype(np.int) #room for squares
       instant_energy = np.dot(samples, samples) / float(0xffffffff) #normalize

       local_energy_average = self.local_energy.mean()
       local_energy_variance = self.local_energy.var()

       beat_sensibility = (-0.002 * local_energy_variance) +2.7
       beat = instant_energy > beat_sensibility * local_energy_average

       self.local_energy_index -=1
       if self.local_energy_index < 0:
           self.local_energy_index = len(self.local_energy) - 1

       return beat
beater = SimpleBeatDetection
for i in range(bars):
       spriteGroup.add(bars((i*ceil(pg.display.get_surface().get_size()[0]/bars),pg.display.get_surface().get_size()[1]-32),
       (ceil(pg.display.get_surface().get_size()[0]/bars),4),
       color=(0x8E7CC3)))
       fps = 0

while not done:
       for event in pg.event.get():
           if event.type == pg.QUIT:
               done = True



       for i in spriteGroup:
           i.update((get_dataS(stream,drawChunk)),clock.get_time())
       if (beater.detect_beat(np.frombuffer(stream.read(CHUNK,exception_on_overflow = False), np.int16))):
               screen.fil(0xFFFFFF)
               flashclock = 0
       if flashclock < flashdur:
           screen.fill(0xFFFFFF)
           flashclock+=clock.get_time()
       else:
           screen.fill(0)

       spriteGroup.draw(screen)
       pg.display.flip()
       clock.tick(fps)
pg.quit

I have a Macbook Air (13 inch Early 2014) and am running Spyder 2.7 Python. I'm guessing it's something with my audio channels on my computer but I am not very sure.

python
python-2.7
audio
spyder
pyaudio
asked on Stack Overflow Mar 18, 2019 by cainguyen

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0