title: record output sound in python
body: <p>i want to programatically record sound coming out of my laptop in python.  i found <code>PyAudio</code> and came up with the following program that accomplishes the task:</p>
  
  <pre><code>import pyaudio, wave, sys
  
  chunk = 1024
  FORMAT = pyaudio.paInt16
  CHANNELS = 1
  RATE = 44100
  RECORD_SECONDS = 5
  WAVE_OUTPUT_FILENAME = sys.argv[1]
  
  p = pyaudio.PyAudio()
  channel_map = (0, 1)
  
  stream_info = pyaudio.PaMacCoreStreamInfo(
      flags = pyaudio.PaMacCoreStreamInfo.paMacCorePlayNice,
      channel_map = channel_map)
  
  stream = p.open(format = FORMAT,
                  rate = RATE,
                  input = True,
                  input_host_api_specific_stream_info = stream_info,
                  channels = CHANNELS)
  
  all = []
  for i in range(0, RATE / chunk * RECORD_SECONDS):
          data = stream.read(chunk)
          all.append(data)
  stream.close()
  p.terminate()
  
  data = ''.join(all)
  wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
  wf.setnchannels(CHANNELS)
  wf.setsampwidth(p.get_sample_size(FORMAT))
  wf.setframerate(RATE)
  wf.writeframes(data)
  wf.close()
  </code></pre>
  
  <p>the problem is i have to connect the headphone jack to the microphone jack.  i tried replacing these lines:</p>
  
  <pre><code>input = True,
  input_host_api_specific_stream_info = stream_info,
  </code></pre>
  
  <p>with these:</p>
  
  <pre><code>output = True,
  output_host_api_specific_stream_info = stream_info,
  </code></pre>
  
  <p>but then i get this error:</p>
  
  <blockquote>
    <p>Traceback (most recent call last):<br>
     File "./test.py", line 25, in <br>
       data = stream.read(chunk)<br>
     File "/Library/Python/2.5/site-packages/pyaudio.py", line 562, in read<br>
       paCanNotReadFromAnOutputOnlyStream)<br>
    IOError: [Errno Not input stream] -9975  </p>
  </blockquote>
  
  <p>is there a way to instantiate the PyAudio stream so that it inputs from the computer's output and i don't have to connect the headphone jack to the microphone?  is there a better way to go about this?  i'd prefer to stick w/ a python app and avoid cocoa.</p>