prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Add synthetic data mode to spectrogram
  The pyaudio package is not available on all platforms. An option mode to generate synthetic dat would allow the app to be run everywhere in some capacity. 
  
  @philippjfr I think you might already have some code that could be re-used?
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `examples/app/spectrogram/audio.py`
  Content:
  ```
  1 from __future__ import print_function
  2 
  3 try:
  4     import pyaudio
  5 except:
  6     print("This demo requires pyaudio installed to function")
  7     import sys
  8     sys.exit(1)
  9 
  10 import numpy as np
  11 import scipy as sp
  12 from scipy.integrate import simps
  13 
  14 NUM_SAMPLES = 1024
  15 SAMPLING_RATE = 44100
  16 MAX_FREQ = SAMPLING_RATE / 2
  17 FREQ_SAMPLES = NUM_SAMPLES / 8
  18 TIMESLICE = 100  # ms
  19 NUM_BINS = 16
  20 
  21 data = {'values': None}
  22 
  23 def _get_audio_data():
  24     pa = pyaudio.PyAudio()
  25     stream = pa.open(
  26         format=pyaudio.paInt16,
  27         channels=1,
  28         rate=SAMPLING_RATE,
  29         input=True,
  30         frames_per_buffer=NUM_SAMPLES
  31     )
  32 
  33     while True:
  34         try:
  35             raw_data  = np.fromstring(stream.read(NUM_SAMPLES), dtype=np.int16)
  36             signal = raw_data / 32768.0
  37             fft = sp.fft(signal)
  38             spectrum = abs(fft)[:NUM_SAMPLES/2]
  39             power = spectrum**2
  40             bins = simps(np.split(power, NUM_BINS))
  41             data['values'] = signal, spectrum, bins
  42         except:
  43             continue
  44
  ```
  
  Path: `examples/app/spectrogram/server_lifecycle.py`
  Content:
  ```
  1 from threading import Thread
  2 
  3 import audio
  4 
  5 def on_server_loaded(server_context):
  6     t = Thread(target=audio._get_audio_data, args=())
  7     t.setDaemon(True)
  8     t.start()
  9
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/examples/app/spectrogram/audio.py b/examples/app/spectrogram/audio.py
  --- a/examples/app/spectrogram/audio.py
  +++ b/examples/app/spectrogram/audio.py
  @@ -1,11 +1,6 @@
   from __future__ import print_function
   
  -try:
  -    import pyaudio
  -except:
  -    print("This demo requires pyaudio installed to function")
  -    import sys
  -    sys.exit(1)
  +from time import sleep
   
   import numpy as np
   import scipy as sp
  @@ -20,24 +15,51 @@
   
   data = {'values': None}
   
  -def _get_audio_data():
  -    pa = pyaudio.PyAudio()
  -    stream = pa.open(
  -        format=pyaudio.paInt16,
  -        channels=1,
  -        rate=SAMPLING_RATE,
  -        input=True,
  -        frames_per_buffer=NUM_SAMPLES
  -    )
  -
  -    while True:
  -        try:
  -            raw_data  = np.fromstring(stream.read(NUM_SAMPLES), dtype=np.int16)
  -            signal = raw_data / 32768.0
  +try:
  +    import pyaudio
  +
  +    def update_audio_data():
  +        pa = pyaudio.PyAudio()
  +        stream = pa.open(
  +            format=pyaudio.paInt16,
  +            channels=1,
  +            rate=SAMPLING_RATE,
  +            input=True,
  +            frames_per_buffer=NUM_SAMPLES
  +        )
  +
  +        while True:
  +            try:
  +                raw_data  = np.fromstring(stream.read(NUM_SAMPLES), dtype=np.int16)
  +                signal = raw_data / 32768.0
  +                fft = sp.fft(signal)
  +                spectrum = abs(fft)[:int(NUM_SAMPLES/2)]
  +                power = spectrum**2
  +                bins = simps(np.split(power, NUM_BINS))
  +                data['values'] = signal, spectrum, bins
  +            except:
  +                continue
  +
  +except:
  +    print()
  +    print(" *** Pyaudio package not installed, using synthesized audio data ***")
  +    print()
  +
  +    # These are basically picked out of a hat to show something vaguely interesting
  +    _t = np.arange(0, NUM_SAMPLES/SAMPLING_RATE, 1.0/SAMPLING_RATE)
  +    _f = 2000 + 3000*(2+np.sin(4*np.linspace(0, 2*np.pi, 500)))
  +    _i = 0
  +
  +    def update_audio_data():
  +        while True:
  +            global _i
  +            A = 0.3 + 0.05 * np.random.random()
  +            signal = A*np.sin(2*np.pi*_f[_i]*_t + np.sin(2*np.pi*200*_t))
  +
               fft = sp.fft(signal)
  -            spectrum = abs(fft)[:NUM_SAMPLES/2]
  +            spectrum = abs(fft)[:int(NUM_SAMPLES/2)]
               power = spectrum**2
               bins = simps(np.split(power, NUM_BINS))
               data['values'] = signal, spectrum, bins
  -        except:
  -            continue
  +            _i = (_i + 1) % len(_f)
  +            sleep(1.0/12)
  diff --git a/examples/app/spectrogram/server_lifecycle.py b/examples/app/spectrogram/server_lifecycle.py
  --- a/examples/app/spectrogram/server_lifecycle.py
  +++ b/examples/app/spectrogram/server_lifecycle.py
  @@ -3,6 +3,6 @@
   import audio
   
   def on_server_loaded(server_context):
  -    t = Thread(target=audio._get_audio_data, args=())
  +    t = Thread(target=audio.update_audio_data, args=())
       t.setDaemon(True)
       t.start()
  
verification_info: {"golden_diff": "diff --git a/examples/app/spectrogram/audio.py b/examples/app/spectrogram/audio.py\n--- a/examples/app/spectrogram/audio.py\n+++ b/examples/app/spectrogram/audio.py\n@@ -1,11 +1,6 @@\n from __future__ import print_function\n \n-try:\n-    import pyaudio\n-except:\n-    print(\"This demo requires pyaudio installed to function\")\n-    import sys\n-    sys.exit(1)\n+from time import sleep\n \n import numpy as np\n import scipy as sp\n@@ -20,24 +15,51 @@\n \n data = {'values': None}\n \n-def _get_audio_data():\n-    pa = pyaudio.PyAudio()\n-    stream = pa.open(\n-        format=pyaudio.paInt16,\n-        channels=1,\n-        rate=SAMPLING_RATE,\n-        input=True,\n-        frames_per_buffer=NUM_SAMPLES\n-    )\n-\n-    while True:\n-        try:\n-            raw_data  = np.fromstring(stream.read(NUM_SAMPLES), dtype=np.int16)\n-            signal = raw_data / 32768.0\n+try:\n+    import pyaudio\n+\n+    def update_audio_data():\n+        pa = pyaudio.PyAudio()\n+        stream = pa.open(\n+            format=pyaudio.paInt16,\n+            channels=1,\n+            rate=SAMPLING_RATE,\n+            input=True,\n+            frames_per_buffer=NUM_SAMPLES\n+        )\n+\n+        while True:\n+            try:\n+                raw_data  = np.fromstring(stream.read(NUM_SAMPLES), dtype=np.int16)\n+                signal = raw_data / 32768.0\n+                fft = sp.fft(signal)\n+                spectrum = abs(fft)[:int(NUM_SAMPLES/2)]\n+                power = spectrum**2\n+                bins = simps(np.split(power, NUM_BINS))\n+                data['values'] = signal, spectrum, bins\n+            except:\n+                continue\n+\n+except:\n+    print()\n+    print(\" *** Pyaudio package not installed, using synthesized audio data ***\")\n+    print()\n+\n+    # These are basically picked out of a hat to show something vaguely interesting\n+    _t = np.arange(0, NUM_SAMPLES/SAMPLING_RATE, 1.0/SAMPLING_RATE)\n+    _f = 2000 + 3000*(2+np.sin(4*np.linspace(0, 2*np.pi, 500)))\n+    _i = 0\n+\n+    def update_audio_data():\n+        while True:\n+            global _i\n+            A = 0.3 + 0.05 * np.random.random()\n+            signal = A*np.sin(2*np.pi*_f[_i]*_t + np.sin(2*np.pi*200*_t))\n+\n             fft = sp.fft(signal)\n-            spectrum = abs(fft)[:NUM_SAMPLES/2]\n+            spectrum = abs(fft)[:int(NUM_SAMPLES/2)]\n             power = spectrum**2\n             bins = simps(np.split(power, NUM_BINS))\n             data['values'] = signal, spectrum, bins\n-        except:\n-            continue\n+            _i = (_i + 1) % len(_f)\n+            sleep(1.0/12)\ndiff --git a/examples/app/spectrogram/server_lifecycle.py b/examples/app/spectrogram/server_lifecycle.py\n--- a/examples/app/spectrogram/server_lifecycle.py\n+++ b/examples/app/spectrogram/server_lifecycle.py\n@@ -3,6 +3,6 @@\n import audio\n \n def on_server_loaded(server_context):\n-    t = Thread(target=audio._get_audio_data, args=())\n+    t = Thread(target=audio.update_audio_data, args=())\n     t.setDaemon(True)\n     t.start()\n", "issue": "Add synthetic data mode to spectrogram\nThe pyaudio package is not available on all platforms. An option mode to generate synthetic dat would allow the app to be run everywhere in some capacity. \r\n\r\n@philippjfr I think you might already have some code that could be re-used?\n", "before_files": [{"content": "from __future__ import print_function\n\ntry:\n    import pyaudio\nexcept:\n    print(\"This demo requires pyaudio installed to function\")\n    import sys\n    sys.exit(1)\n\nimport numpy as np\nimport scipy as sp\nfrom scipy.integrate import simps\n\nNUM_SAMPLES = 1024\nSAMPLING_RATE = 44100\nMAX_FREQ = SAMPLING_RATE / 2\nFREQ_SAMPLES = NUM_SAMPLES / 8\nTIMESLICE = 100  # ms\nNUM_BINS = 16\n\ndata = {'values': None}\n\ndef _get_audio_data():\n    pa = pyaudio.PyAudio()\n    stream = pa.open(\n        format=pyaudio.paInt16,\n        channels=1,\n        rate=SAMPLING_RATE,\n        input=True,\n        frames_per_buffer=NUM_SAMPLES\n    )\n\n    while True:\n        try:\n            raw_data  = np.fromstring(stream.read(NUM_SAMPLES), dtype=np.int16)\n            signal = raw_data / 32768.0\n            fft = sp.fft(signal)\n            spectrum = abs(fft)[:NUM_SAMPLES/2]\n            power = spectrum**2\n            bins = simps(np.split(power, NUM_BINS))\n            data['values'] = signal, spectrum, bins\n        except:\n            continue\n", "path": "examples/app/spectrogram/audio.py"}, {"content": "from threading import Thread\n\nimport audio\n\ndef on_server_loaded(server_context):\n    t = Thread(target=audio._get_audio_data, args=())\n    t.setDaemon(True)\n    t.start()\n", "path": "examples/app/spectrogram/server_lifecycle.py"}], "after_files": [{"content": "from __future__ import print_function\n\nfrom time import sleep\n\nimport numpy as np\nimport scipy as sp\nfrom scipy.integrate import simps\n\nNUM_SAMPLES = 1024\nSAMPLING_RATE = 44100\nMAX_FREQ = SAMPLING_RATE / 2\nFREQ_SAMPLES = NUM_SAMPLES / 8\nTIMESLICE = 100  # ms\nNUM_BINS = 16\n\ndata = {'values': None}\n\ntry:\n    import pyaudio\n\n    def update_audio_data():\n        pa = pyaudio.PyAudio()\n        stream = pa.open(\n            format=pyaudio.paInt16,\n            channels=1,\n            rate=SAMPLING_RATE,\n            input=True,\n            frames_per_buffer=NUM_SAMPLES\n        )\n\n        while True:\n            try:\n                raw_data  = np.fromstring(stream.read(NUM_SAMPLES), dtype=np.int16)\n                signal = raw_data / 32768.0\n                fft = sp.fft(signal)\n                spectrum = abs(fft)[:int(NUM_SAMPLES/2)]\n                power = spectrum**2\n                bins = simps(np.split(power, NUM_BINS))\n                data['values'] = signal, spectrum, bins\n            except:\n                continue\n\nexcept:\n    print()\n    print(\" *** Pyaudio package not installed, using synthesized audio data ***\")\n    print()\n\n    # These are basically picked out of a hat to show something vaguely interesting\n    _t = np.arange(0, NUM_SAMPLES/SAMPLING_RATE, 1.0/SAMPLING_RATE)\n    _f = 2000 + 3000*(2+np.sin(4*np.linspace(0, 2*np.pi, 500)))\n    _i = 0\n\n    def update_audio_data():\n        while True:\n            global _i\n            A = 0.3 + 0.05 * np.random.random()\n            signal = A*np.sin(2*np.pi*_f[_i]*_t + np.sin(2*np.pi*200*_t))\n\n            fft = sp.fft(signal)\n            spectrum = abs(fft)[:int(NUM_SAMPLES/2)]\n            power = spectrum**2\n            bins = simps(np.split(power, NUM_BINS))\n            data['values'] = signal, spectrum, bins\n            _i = (_i + 1) % len(_f)\n            sleep(1.0/12)\n", "path": "examples/app/spectrogram/audio.py"}, {"content": "from threading import Thread\n\nimport audio\n\ndef on_server_loaded(server_context):\n    t = Thread(target=audio.update_audio_data, args=())\n    t.setDaemon(True)\n    t.start()\n", "path": "examples/app/spectrogram/server_lifecycle.py"}]}