carykh / recordTrimEdit

Records audio, trims it, and allows you to edit it, all in one fell swoop.
MIT License
66 stars 9 forks source link

Issue with ValueError with Array Size on Windows #17

Open musahaydar opened 3 months ago

musahaydar commented 3 months ago

This issue is mentioned in #16:

On Windows, the following error may occur:

ValueError: cannot reshape array of size 1764 into shape (882,)

This can happen in two places: line 162, where the expected size of the array from the sound buffer is hardcoded as

audio_array.reshape((882))

as well as line 316, where the audio is combined into a single array:

audio_full[i*chunk_size:i*chunk_size+chunk_size] = np.frombuffer(sound_chunks[i], dtype=np.int16)

The reason this occurs is because on Windows, with allowed_changes=AUDIO_ALLOW_FORMAT_CHANGE in the audio device, the size of the chunks may change (in my case as with #16, it is double, from 882 to 1764).

Simply disabling this option (i.e. setting allowed_changes=0) causes the program to continue working in my WSL environment as well as to work properly on Windows. A more involved solution would be to allow audio format changes and then, instead of using the values of SAMPLE_RATE and CHUNK_RATE from the config file to determine the size of the chunks (used in both places above), to retrieve these values from the audio device after it is initalized.

havingfu commented 2 months ago

This issue is mentioned in #16:

On Windows, the following error may occur:

ValueError: cannot reshape array of size 1764 into shape (882,)

This can happen in two places: line 162, where the expected size of the array from the sound buffer is hardcoded as

audio_array.reshape((882))

as well as line 316, where the audio is combined into a single array:

audio_full[i*chunk_size:i*chunk_size+chunk_size] = np.frombuffer(sound_chunks[i], dtype=np.int16)

The reason this occurs is because on Windows, with allowed_changes=AUDIO_ALLOW_FORMAT_CHANGE in the audio device, the size of the chunks may change (in my case as with #16, it is double, from 882 to 1764).

Simply disabling this option (i.e. setting allowed_changes=0) causes the program to continue working in my WSL environment as well as to work properly on Windows. A more involved solution would be to allow audio format changes and then, instead of using the values of SAMPLE_RATE and CHUNK_RATE from the config file to determine the size of the chunks (used in both places above), to retrieve these values from the audio device after it is initalized.

Nice! That solved it for me. adding AUDIO_ALLOW_FORMAT_CHANGE = 0 after pygame._sdl2 import solves this.

Thanks🚀