Guest User

Untitled

a guest
Jan 17th, 2024
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | Software | 0 0
  1.  
  2. import sounddevice as sd
  3. import numpy as np
  4.  
  5.  
  6. def play_audio_file(file_name, output_file_name, sample_rate):
  7.     try:
  8.         with open(file_name, 'rb') as f:
  9.             audio_data = f.read()
  10.  
  11.         audio_array_int8 = np.frombuffer(audio_data, dtype=np.uint8)
  12.         # Преобразование из 8 в 16 бит с добавлением нулей в старший байт
  13.         audio_array_uint16 = audio_array_int8.astype(np.uint16) * 256
  14.  
  15.         # Преобразование из 8 в 16 бит с добавлением нулей в старший байт
  16.         audio_data_int16 = (audio_array_int8.astype(np.int16) - 128) * 256
  17.  
  18.        
  19.         audio_array_normalized = audio_array_int8 / np.max(np.abs(audio_array_int8))
  20.         # with open(output_file_name, 'wb') as f1:
  21.         #     f1.write(audio_array_normalized)
  22.         # Проигрываем данные
  23.         sd.play(audio_array_normalized, samplerate=sample_rate)
  24.         # sd.play(audio_array, samplerate=sample_rate)
  25.         sd.wait()
  26.  
  27.     except Exception as e:
  28.         print(f"Error: {e}")
  29.  
  30.  
  31. if __name__ == "__main__":
  32.     file_name = 'audio_data4.raw'
  33.     output_file_name = 'norm_audio_data.raw'
  34.     sample_rate = 7000  # awaited sample rate
  35.     play_audio_file(file_name, output_file_name, sample_rate)
  36.  
Advertisement
Add Comment
Please, Sign In to add comment