04_recorder.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python
  2. #version : 2023.04.04
  3. #language : en
  4. from maix import display
  5. from maix import image
  6. from maix import camera
  7. import pyaudio
  8. import wave
  9. import time
  10. import sys
  11. sys.path.append('/root/')
  12. #按键初始化
  13. from CocoPi import BUTTON
  14. key_C = BUTTON(13)
  15. buttonState=0
  16. newState=0
  17. oldState=0
  18. def buttonDetect():
  19. global buttonState,newState,oldState,key_C
  20. newState=key_C.is_pressed()
  21. if newState == True and oldState == False:
  22. buttonState=1
  23. elif newState == False and oldState == True:
  24. buttonState=2
  25. else:
  26. buttonState=0
  27. oldState=newState
  28. def timeStampConvert():
  29. timeListOutput=""
  30. timeListOutput = str(time.localtime().tm_year) + \
  31. str(time.localtime().tm_mon)+str(time.localtime().tm_mday) + \
  32. str(time.localtime().tm_hour)+str(time.localtime().tm_min) + \
  33. str(time.localtime().tm_sec)
  34. return timeListOutput
  35. #音频初始化
  36. CHUNK = 1024
  37. FORMAT = pyaudio.paInt16
  38. CHANNELS = 2
  39. RATE = 44100
  40. #屏幕初始化
  41. canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
  42. image.load_freetype("/root/preset/fonts/CascadiaCodePL-Italic.ttf")
  43. startPointer=0
  44. startTime=0
  45. global deltaTime
  46. deltaTime=0
  47. # 初始化
  48. startTime = time.time()
  49. oldState=0
  50. newState=0
  51. savePathHead="/root/user/audio/"
  52. savePathTail=".wav"
  53. recordState=0
  54. canvas.draw_string(12, 18, "Tip : Press KEY-C to start/stop", 1, color=(0, 255, 0))
  55. canvas.draw_string(58, 48, "recording!", 1, color=(0, 255, 0))
  56. canvas.draw_image(image.open("/root/preset/img/recording.png"),288 , 8,alpha=1)
  57. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  58. display.show(canvas)
  59. while True:
  60. buttonDetect()
  61. if buttonState==2:
  62. recordState=1-recordState
  63. print("recordState",recordState)
  64. if recordState==1:
  65. if startPointer==0:
  66. canvas.clear()
  67. canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
  68. canvas.draw_string(12, 18, "Tip : Press KEY-C to start/stop", 1, color=(0, 255, 0))
  69. canvas.draw_string(58, 48, "recording!", 1, color=(0, 255, 0))
  70. canvas.draw_image(image.open("/root/preset/img/recording.png"),288 , 8,alpha=1)
  71. canvas.draw_string(24, 108, "Recording start!", 1, color=(0, 255, 0))
  72. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  73. display.show(canvas)
  74. print("Recording start!")
  75. startTime = round(time.time(), 2)
  76. startPointer=1
  77. p = pyaudio.PyAudio()
  78. stream = p.open(format=FORMAT,
  79. channels=CHANNELS,
  80. rate=RATE,
  81. input=True,
  82. frames_per_buffer=CHUNK)
  83. frames = []
  84. else:
  85. data = stream.read(CHUNK,exception_on_overflow = False)
  86. frames.append(data)
  87. elif recordState==0:
  88. if startPointer==1:
  89. stream.stop_stream()
  90. stream.close()
  91. p.terminate()
  92. WAVE_OUTPUT_FILENAME=savePathHead+timeStampConvert()+savePathTail
  93. wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
  94. wf.setnchannels(CHANNELS)
  95. wf.setsampwidth(p.get_sample_size(FORMAT))
  96. wf.setframerate(RATE)
  97. wf.writeframes(b''.join(frames))
  98. wf.close()
  99. deltaTime = round(time.time() - startTime, 2)
  100. canvas.clear()
  101. canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
  102. canvas.draw_string(12, 18, "Tip : Press KEY-C to start/stop", 1, color=(0, 255, 0))
  103. canvas.draw_string(58, 48, "recording!", 1, color=(0, 255, 0))
  104. canvas.draw_image(image.open("/root/preset/img/recording.png"),288 , 8,alpha=1)
  105. canvas.draw_string(24, 108, "Recording completed!", 1, color=(0, 255, 0))
  106. canvas.draw_string(24, 128, "Lasttime:"+str(deltaTime), 1, color=(0, 255, 0))
  107. canvas.draw_string(24, 148, "File Save Path:", 1, color=(0, 255, 0))
  108. canvas.draw_string(24, 168, WAVE_OUTPUT_FILENAME, 1, color=(0, 255, 0))
  109. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  110. display.show(canvas)
  111. print(deltaTime)
  112. startPointer=0
  113. else:
  114. startPointer=0
  115. else:
  116. pass