PyAudio is clipping the end of sound - python

I have written/am still playing with a software FSK modem written in python using PyAudio. At first I was modulating the sound into a *.wav file and then playing it at a later time however this is not a long term solution. I have put in place code which takes some given input, prepares the 8N1 data to be sent and then attempts to play it.
My problem is that the sound itself does not seem to play fully; I have verified this by noting that the demodulator produces the correct message sans the last few characters. Recording the wave output by the modulator also corroborates this observation.
I think that the problem has something to do with the frames_per_buffer however I don't know what is going on. I have tried to appeal to PyAudio itself by just sending all the data at once (in a stream.write(...)) however this fails. I have also tried sending it a chunk at a time (as in the case of http://people.csail.mit.edu/hubert/pyaudio/). I am not, however, playing from a file, rather from an array of samples.
What could the problem be?

Related

Recording audio blocks for musical looper doesn't function correctly

First I want to explain what does this little program is supposed to do. It is a musical looper, when not recording anything, it will basically bypass the incoming audio blocks to the output. When in recording mode (which is triggered by a button in tkinter gui) it will still bypass the input to the output, but it will also record incoming audio. When the recording is done, it will loop the recording. This is basically it.
The program consists of 3 files: https://gist.github.com/ramazanemreosmanoglu/ec64f51f101d9324612bcbcc597fabba
So this is what happens when I run the program:
(Nothing is played automatically in the video. When I record something and do nothing, nothing happens. At first, I thought I should add a midi monitor but we don't need it, just know that every sound is triggered by me.)
https://www.youtube.com/watch?v=PdBORfjHAgQ
What is wrong in this code? What is the correct way to record audio blocks on python with jack?

Detect audio coming from microphone

Ive seen other questions similar to this, but they all involve detecting audio and then recording. I'd like my microphone connected to my computer to always be recording, and any time audio is detected it runs another part of a script. What modules would I need? How would I do this? Im pretty sure this would involve seeing when the audio volume goes above a certain point and then activating the rest of the script, because obviously there will always be some sort of sound coming from the microphone.
This might help if you want them to say something and then if they say the right word or whatever run the script.
https://pypi.org/project/SpeechRecognition/

push-to-listen-into-the-past with pyaudio

Essentially, what I want to achieve is upon triggering, the previous two seconds and further audio should be recorded, until silence.
For this, of course, the microphone has to listen all the time and record to a circular buffer. Upon triggering, current audio shall be recorded until there is a long enough span of silence. In the end, both streams should be combined to a single audio file/stream.
Unfortunately, I can't seem to wrap my head around how to achieve this with pyaudio.
I would be great if you had some advice for me.

How to structure my Python code?

I apologize in advance for this being a bit vague, but I'm trying to figure out what the best way is to write my program from a high-level perspective. Here's an overview of what I'm trying to accomplish:
RasPi takes input from altitude sensor on serial port at 115000 baud.
Does some hex -> dec math and updates state variables (pitch, roll, heading, etc)
Uses pygame library to do some image manipulation based on the state variables on a simulated heads up display
Outputs the image to a projector at 30 fps.
Note that there's no user input (for now).
The issue I'm running into is the framerate. The framerate MUST be constant. I'd rather skip a data packet than drop a frame.
There's two ways I could see structuring this:
Write one function that, when called, grabs data from the serial bus and spits out the state variables as the output. Then write a pygame loop that calls this function from inside it. My concern with this is that if the serial port starts being read at the end of an attitude message, it'll have to pause and wait for the message to start again (fractions of a second, but could result in a dropped frame)
Write two separate modules, both to be running simultaneously. One continuously reads data from the serial port and updates the state variables as fast as possible. The other just does the image manipulation, and grabs the latest state variables when it needs them. However, I'm not actually sure how to write a multithreaded program like this, and I don't know how well the RasPi will handle such a program.
I don't think that RasPi would work that well running multithreaded programs. Try the first method, though it would be interesting to see the results of a multithreaded program.

Go Pro Hero 3 - Streaming video over wifi

I recently acquired a Go Pro Hero 3. Its working fine but when i attempt to stream live video/audio it gitches every now and then.
Initially i just used vlc to open the m3u8 file, however when that was glitchy i downloaded the android app and attempted to stream over that.
It was a little better on the app.
I used wireshark and i think the cause of it is its simply not transferring/buffering fast enough. Tried just to get everything with wget in loop, it got through 3 loops before it either: caught up (possible but i dont think so ... though i may double check that) or fell behind and hence timed out/hung.
There is also delay in the image, but i can live with that.
I have tried lowering the resolution/frame rate but im not sure if it is actually doing anything as i can't tell any difference. I think it may be just the settings for recording on the go pro. Either way, it didn't work.
Essentially i am looking for any possible methods for removing this 'glitchiness'
My current plan is to attempt writing something in python to get the files over UDP (no TCP overhead).
Ill just add a few more details/symptoms:
The Go Pro is using the Apple m3u8 streaming format.
At anyone time there are 16 .ts files in the folder. (26 Kb each)
These get overwritten in a loop (circular buffer)
When i stream on vlc:
Approx 1s delay - streams fine for ~0.5s, stops for a little less than that, then repeats.
What i think is happening is the file its trying to transfer gets overwritten which causes it to timeout.
Over the android App:
Less delay and shorter 'timeouts' but still there
I want to write a python script to try get a continuous image. The files are small enough that they should fit in a single UDP packet (i think ... 65Kb ish right?)
Is there anything i could change in terms of wifi setting on my laptop to improve it too?
Ie some how dedicate it to that?
Thanks,
Stephen
I've been working on creating a GoPro API recently for Node.js and found the device very glitchy too. Its much more stable after installing the latest gopro firmware (3.0.0).
As for streaming, I couldnt get around the wifi latency and went for a record and copy approach.

Categories