Package: voluxaudio
Volux Audio☠️EXPERIMENTAL:- Beware all ye who enter here ☠️Table of ContentsInstallationUsageVoluxAudioStream- SourceNon-buffered ExampleBuffered ExampleAudio-visualization ExampleTodo ListLinksInstallationpipinstallvoluxaudioUsageVoluxAudioStream- SourceNon-buffered ExampleThis Python code uses thevoluxaudiolibrary to open a new audio stream and calls theon_datafunction every time it gets a new chunk of audio data. Theon_datafunction simply prints a message indicating that a new chunk of audio data has been received, along with the current timestamp.TheCHUNK_SIZEvariable defines the size of each chunk of audio data in bytes, and theCHANNEL_COUNTvariable defines the number of audio channels.The code creates aVoluxAudioStreamobject with theon_datafunction,chunk_size, andchannelsas arguments. It then enters an infinite loop that keeps the audio stream open and callssleep(1)to wait for one second before checking for new audio data again.fromvoluxaudioimportVoluxAudioStreamfromtimeimportsleep,perf_counterCHUNK_SIZE=1024CHANNEL_COUNT=2defon_data(in_data,# audio data from this chunkframe_count,# number of samples chunktime_info,status_flags,buffer_read_dtype,# buffer's datatypechannel_count,# number of channelssample_rate,# number of samples collected per second):"""Do this every time we get a new chunk of data."""print(f"got a new chunk of audio data! (seconds:{perf_counter()})")withVoluxAudioStream(on_data=on_data,chunk_size=CHUNK_SIZE,channels=CHANNEL_COUNT)asaudio_stream:# keep stream open until script closedwhileTrue:sleep(1)Buffered ExampleThis Python code continuously prints the current buffer of audio data being read from an audio stream at a specified rate.fromvoluxaudioimportVoluxAudioStreamfromtimeimportsleepCHUNK_SIZE=1024CHANNEL_COUNT=2BUFFER_SIZE_IN_SECONDS=1BUFFER_READS_PER_SECOND=60withVoluxAudioStream(chunk_size=CHUNK_SIZE,channels=CHANNEL_COUNT,buffer_size_in_seconds=BUFFER_SIZE_IN_SECONDS)asaudio_stream:whileTrue:print(f"buffer:{audio_stream.buffer}")sleep(1/BUFFER_READS_PER_SECOND)Audio-visualization ExampleThis Python code opens an audio stream using thevoluxaudiolibrary with buffering enabled. It defines a functionon_data()that does nothing and is called every time new samples are gathered. Within thewhileloop, it calculates the weakly approximated amplitude of the audio stream by taking the last 2048 samples of the left and right channels and averaging their absolute values. It then prints a bar representing the amplitude by printing a number followed by a horizontal bar, where the length of the bar is proportional to the amplitude. The script waits for approximately 50 milliseconds before repeating the loop.This audio stream comes from your system's default recording device, which could either be a microphone or your computer/desktop audio.fromvoluxaudioimportVoluxAudioStreamimportnumpyasnpfromtimeimportsleep# call this every time new samples gathereddefon_data(*args,**kwargs):return# open an audio stream with buffering enabledwithVoluxAudioStream(on_data=on_data,chunk_size=2048,channels=2,buffer_size_in_seconds=2,)asaudio_stream:# repeat until script stoppedwhileTrue:# weakly approximate amplitudesamples=audio_stream.buffersample_count=len(samples)samples_per_channel=np.swapaxes(samples,0,1)L_channel_e=np.average(np.abs(samples_per_channel[0][-2048:]))/sample_countR_channel_e=np.average(np.abs(samples_per_channel[1][-2048:]))/sample_counte=(L_channel_e+R_channel_e)/2# print barprint(f"{e:<3.3f}"+int(e*100)*'|')# wait ~50mssleep(1/20)This is a simple example, but more complex audio processing is possible.A bar is printed every 50ms, changing length based on the approximated amplitude.The system volume may affect results, to test this dramatically increase/decrease your system volume to see if the bar length is affected. This applies to computer/desktop audio, not microphones.Buffering is used asbuffer_size_in_secondsis specified.Todo ListGet simpleVoluxAudioStreamclass workingMakeVoluxAudioStreamclass highly configurableAdd basic example to documentationAdd basic documentationAdd more examples to documentationAdd linksAdd more detailed documentationLinks📖Documentation🐍Latest Release🧰Source Code🐞Issue Tracker🐦 Twitter@DrTexx📨 Emaildenver.opensource@tutanota.com