bastibe / SoundCard

A Pure-Python Real-Time Audio Library
https://soundcard.readthedocs.io
BSD 3-Clause "New" or "Revised" License
689 stars 70 forks source link

write to file (.wav)of recorded data of speaker output does not work #79

Closed charlamenan closed 4 years ago

charlamenan commented 4 years ago

I have the below code which plays a .wav file in groove music (windows app)on speaker endpoint and recorded through speaker loop-back ,when i write this recorded data to a wav file then i could not play the .wav file and getting error (can't play ,the item was encoded in a format that's not supported ),i could listen if i just play the recorded data through soundcard speaker (last line of code)but not when i write the recorded data to file and play the file

import soundcard as sc import numpy as np import subprocess from scipy.io import wavfile

GROOVE_PLAYER_CMD = \ 'cmd /c start "explorer.exe shell:C:\Program Files\WindowsApps\Microsoft.' \ 'ZuneMusic_3.6.25021.0_x64__8wekyb3d8bbwe!Microsoft.ZuneMusic" '

headset = sc.get_speaker('Head') print("headset",headset)

mics = sc.all_microphones(include_loopback=True) loopback_speaker = mics[1] print("loopback_speaker:",loopback_speaker)

if loopback_speaker.isloopback == True: print("loopback is supported") else: print("loopback is not supported")

file_path = "file1.wav" fs_rate, signal = wavfile.read(file_path) total_time = int(signal.shape[0] / fs_rate) print("total_time",total_time) total_frames = total_time*fs_rate

print("total_frames:",total_frames)

with loopback_speaker.recorder(samplerate=fs_rate) as mic, \ headset.player(samplerate=fs_rate) as sp:

for _ in range(100):

groove_play = GROOVE_PLAYER_CMD+file_path cmd_exe = subprocess.Popen(groove_play, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE) print("Playback started in groove music player") print("Recording started") data = mic.record(numframes=(total_frames+2048)) #recoding more than the playback file totalframes print("Recording ended")

exec_out = cmd_exe.communicate()[0] print("Playback ended")

wavfile.write("recorded.wav", fs_rate, data) sp.play(data)

bastibe commented 4 years ago

Does it sound correctly? Then that's a wavfile problem, not a soundcard problem. You could try using SoundFile instead.

charlamenan commented 4 years ago

Yes it sounds correctly and with soundfile i could write to file and playing file also works ,thanks . I was looking for your contact after listening to the https://www.youtube.com/watch?v=mc8ru37dwf8

Another question ,not relented soundcard but if you know ? i am working with audio data ,I want to compare the recorded audio file of any audio format (.wav, .mp3 ,aac etc ) with the input file used for playback and say if both are same ,do not want byte wise comparison (bytes will not be same as a windows player is used for playback and there could be processed data )but to say recorded quality is good and has same sound (noise and glitch free) ,How do i do that ,can you help with python library or any input will unblock me

bastibe commented 4 years ago

Another question ,not relented soundcard but if you know ?

I wrote you a long answer to that question in https://github.com/bastibe/SoundCard/issues/78#issuecomment-603075075

TL;DR: it's hard.

I was looking for your contact after listening to the https://www.youtube.com/watch?v=mc8ru37dwf8

Cool to see that the talk is of some value to people!

If your question has been answered, please close this issue.