title: Play .wav file and record whilst .wave file is played
body: <p>I am using Python3.6. My problem is to play a small segment of .wav audio (0.003 s) whilst recording this transmission for 1 s. It will stop recording and perform DSP techniques on the saved .wav file.</p>
  <p>I have the .wav file generated in python and saving to the computer, and then I open that file for signal processing, but right now my data is collected using Audacity where you can import a .wav file and then record whilst it plays.</p>
  <p>I have looked at <a href="https://stackoverflow.com/questions/38878908/play-and-record-sound-using-pyaudio-simultaneously">this</a> but it is more of a consecutive execution of playing what was recorded, whereas this application requires record for one second and play during that 1s a 0.003 s signal.</p>
  <p>The below code was something I tried from the hyperlink before given as an answer from another user but it does not operate in the style that is required.</p>
  <pre><code>import sounddevice as sd
  
  fs = 44100
  
  def callback(indata, outdata, frames, time, status):
      if status:
          print(status)
      outdata[:] = indata
  
  print(sd.query_devices())
  
  try:
      with sd.Stream(device=(0,1), samplerate=fs, dtype='float32', latency=None, channels=2, callback=callback):
          input()
  except KeyboardInterrupt:
      pass
  </code></pre>
  <p>My pseudocode for this is along the lines of:</p>
  <pre><code># import threading
  # import multiprocessing
  
  def playSound(waveFile):
      play(wavefile)
      return
  
  # A Flag that will be changed to 1 once the recording has been saved
  flagRecDone = 0
  def record(outputfile):
      # Save the recording as a .wav file to be used by the dspProcess
      record(outputfile, duration=1)
      flagRecDone = 1
      return
  
  # Executes every 7 Seconds
  def dspProcess(nameOfFile):
      while(True):
          # Wait for file to be seen in the file directory
          if(flagRecDone == 1):
              # Do some DSP magic
              return
          # (Indicating the file was successfully recorded and stored)
          # Save results in a CSV
      return
  
  def animateGraph(csvData):
      # Load the results from the csv once the dspProcess finishes
      # Shows the results after every run from dspProcess
      return
  
  # These 3 are welcome to be a single thread/process or function
  thread1.start(record(&quot;audioWav.wav&quot;))
  thread2.start(playSound(&quot;Chirp.wav&quot;))
  thread3.start(dspProcess(&quot;audioWav.wav&quot;))
  
  # This must be its own thread/process
  thread4.start(animateGraph(&quot;data.csv&quot;))
  </code></pre>
  <p>I am struggling to play the sound and record at the same time, some sources I have researched are not explaining correctly or I am failing to understand.</p>
  <p>Thanks a lot in advance!</p>