Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

sound_gen4.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import numpy as np
  2. import pyaudio
  3. from numpy import linspace, sin, pi, int16
  4. import pylab as plt
  5. from scipy import signal
  6. def callback(in_data, frame_count, time_info, status):
  7. # data = wf.readframes(frame_count)
  8. global freq, length
  9. # freq = 300
  10. data = note(freq, length, amp=amp, rate=RATE)
  11. return (data, pyaudio.paContinue)
  12. def note(freq, t_len, amp=1, rate=44100, mode='sin'):
  13. n_sample = int(t_len * rate)
  14. factor = (float(freq) * np.pi * 2 / rate)
  15. data = []
  16. if mode == 'saw':
  17. data = signal.sawtooth(np.arange(n_sample) * factor, .25) * amp
  18. else:
  19. data = np.sin(np.arange(n_sample) * factor) * amp
  20. fade = int(n_sample/5)
  21. fade_in = np.arange(0., 1., 1/fade)
  22. fade_out = np.arange(1., 0., -1/fade)
  23. data[:fade] = np.multiply(data[:fade], fade_in)
  24. data[-fade:] = np.multiply(data[-fade:], fade_out)
  25. return data.astype(np.float32)
  26. RATE = 44100
  27. # RATE = 1000
  28. FREQ = 262
  29. freq = 440
  30. length = 0.5*freq/1000.
  31. amp = 10000
  32. pa = pyaudio.PyAudio()
  33. s = pa.open(output=True,
  34. channels=2,
  35. rate=RATE,
  36. format=pyaudio.paFloat32)
  37. # stream_callback=callback)
  38. freqs = [440, 50, 100, 200, 300, 230, 120, 500, 40, 125]
  39. # while freq<400:
  40. for ii in range(len(freqs)):
  41. length = .5# 200/ freq
  42. # length = 1.
  43. # freq = np.random.randint(50,250)
  44. freq = freqs[ii]
  45. print(ii, freq, length)
  46. tone = note(freq, length, amp=1, rate=RATE, mode='saw')
  47. print(f'tone length: {len(tone)}, in seconds: {len(tone)/2 / RATE}')
  48. tone = np.repeat(tone.reshape((-1, 1)), 2, axis=1).flatten() # need to copy, because we have 2 channels
  49. print(f'tg: {tone}, {np.min(tone)}, {np.max(tone)}, {np.amin(tone)}, {np.amax(tone)}')
  50. s.write(tone.tostring())
  51. # freq += 10
  52. # freq = np.random.randint(50,250)
  53. # amp = freq*20
  54. # freq += 10
  55. # time.sleep(length)
  56. # time.sleep(10)
  57. tone0 = tone
  58. dd = np.hstack((tone0, tone))
  59. plt.figure(1)
  60. plt.clf()
  61. xx1 = range(len(tone0))
  62. xx2 = range(xx1[-1], xx1[-1] + len(tone))
  63. plt.plot(xx1, tone0, 'b-o')
  64. plt.plot(xx2, tone, 'r-o')
  65. plt.show()
  66. s.stop_stream()
  67. s.close()
  68. # pa.terminate()​