ALSA API using in Matlab - buffer issue - c

being part of a lab course, I have to update the simulation about Pulse Coded Modulation. Initially, the simulation was written in 1998 using the OSS (open sound system) and was never updated thereafter. I have rewritten the entire code and ported it to ALSA.
The code itself is a bit long, that's why I haven't put it here but am providing a link.
Now to my issue: Whenever I want to play a vector of random length containing many samples, I start hearing weird periodic random noises. I have a feeling it's due to a buffer underrun. For a better understanding, I have recorded the output.
I believe it has to do something with the parameters I've set. Even though I tried out many cases, I didn't come to a solution.
Just take a look at the period size, buffer size, periods and the sbplay(..) function. PS.: My HW is set such that buffer size = period size * periods
I hope you can help me somehow! Thanks in advance
Code
Output WAV
BTW.: ALSA: buffer underrun on snd_pcm_writei call
didn't help me much...

Efe,
Why don't you try the audioplayer/audiorecorder functions in MATLAB. They use ALSO on Linux. If you want greater control over the latency try the dsp.AudioPlayer/AudioRecorder system objects.
Dinesh

Related

Constantly print volume of raw PCM audio stream to terminal as a number from 0 to 1?

Currently, I am trying to read the volume / gain of a raw 16 bit PCM audio stream. Basically, I'd like to be able to pipe a stream into an executable and print a number in a range from 0 (no sound) to 1 (at volume limit / 0db from clipping) followed by a new line. This should continue only when manually cancelled or when the audio source exits. It does not need to be very acurate, nor do the values require being smoothed heavily (like an RMS value; although a bit of smoothing would probably be better than none at all). The problem is that I understand very little of how to work with raw PCM, and when I found the math, I realized that I currently do not posses the skills required to do something like this. So my question would be: Could someone who's more familiar with the matter provide me with a working example code that could do the behaviour described above, idealy with an explaination of what exactly is done?
The language isn't too important, although something fast like Rust, C, C++, etc. would be preferred.

how to convert jpegs to video with fixed fps?

I have a series of jpegs,I would like to pack and compress them to a Video.
I use tool mpeg streamclip, but it double the whole play time.
If I have 300 jpegs, set fixed fps 30, I expect to get a video of 10s length . but using stream clip I get a 20s long video.
One answer is to get someone who understands programming. The programming APIs (application interfaces, the way client programs call libraries) to the lig libraries like ffmeg have ways in which frame rate can be controlled, and it's usually quite a simple matter to modify a program to produce fewer intermediate frames if you are creating a video from a list of JPEGs.
But the best answer is probably to find a tool that supports what you want to do. That's not a question to ask a programmer especially. Ask someone who knows about video editing. (It would take me about two days to write such a tool from scratch on top of my own JPEG codec and ffmpeg, so obviously I can't do it in response to this question, but that's roughly the level of work you're looking at).

Proccessing "JACK audio" data with C?

My question is slightly abstract but with good grounds. I have successfully ran a JACK script written in C that loops the microphone audio data to the speaker, However I would like to know how to alter the stream of audio my self during playback, perhaps one thing I'd like to try is filter the high(or low) frequencies (CUT them completely off). From my understanding audio comes through as an analog signal and converted to a digital value (within a certain range).
I'm guessing I'm forced to go about this one of two ways, I think one way is to process each value and check if it below the frequency (or above the frequency) I don't want and then alter the value to 0(or the previous value from the last loop cycle to prevent blank spots in the audio during playback). The second way i'm guessing is that JACK presents the buffer with a full array of values that are assigned by frequency spectrum. How do I go about doing this? (In the future I want to do other things with the raw data but I think this is a great start to get familiar with raw audio processing)
Here is my simplified code: http://pastebin.com/Hmiumqkz
You can see that I tried printing the in value as its supposed to be a "float" I thought I might be able to filter frequencies from there but I'm not sure as I don't get anything printed in the console when i run this code it just loops back the mic to speaker but with out any printing to the console.....
NOTES: I have already successfully compiled and tested programs that used the Gstreamer, ALSA, NAudio, irrKang, and the Phonon libraries, they don't allow me to have the cross compatibility i need between OSs and the raw audio data I require for my project, all i ask is to please think twice before lazily report for me to use "other libraries" only for the sake of it being "easier" but I have already tried them and they all fail me.
You haven't really asked a question that can be answered here on SO, so I'll point you to some outside resources.
Here is a tutorial for designing EQs based on the popular RBJ filters:
http://blog.bjornroche.com/2012/08/basic-audio-eqs.html
Most of it is written in C-like psuedocode and will walk you through step-by-step.
Here is the correct answer (You'll notice a printf() function in the proccess(){} call back function) the for loop prints out the the current frames in the buffer(Frequency domain but the for() loop is printing over time so its the time-domain as well -- its both frequency and time)
http://pastebin.com/axDLw7cc

LabVIEW 2009 holding onto data when I don't want it to

I'm new to LabVIEW but have been building a signal analyser code that takes the required data and prints it out to text files after the data has been taken. The problem I'm having is that when it makes a new file it holds on to the data from the previous run and prints that too which is not what I want. I've attached the LabVIEW vi (ver.2009), and any help with this would be greatly appreciated.
Also if someone knows a better way of RMS-ing the data after each iteration than my mess of shift registers I'd be happy to see it.
frequency analyser (fixed).vi
To answer your main question: the part of the code that builds the string (for loop with a shift register) stores the previous data each time you re-run the vi. What you need is to initialise the shift register with an empty string :
Also a couple of notes/suggestions:
You could avoid using shift registers in this case. Divide the DAQ part of the code into say 3 parts: acquire data in the first for loop (store into array), modify the array (you could then perhaps use the build-in RMS vi), visualise on the UI
Build the code in smaller chunks, use subVi's
Keep the code small, nice and tidy (check coding standards), add comments - this will really help you later
Since you asked for advice on the RMS functionality you used I took a more detailed look of your code. And I may be harse, but it doesn't make sense (point by point):
You ask the end user for a number of runs, and then you subtract one. Why? I guess it's because the read data before the for loop. (remove that one).
The Frequency RMS function you use has support for avaraging, and has no limit of the number of averages. Specify the following configuration:
This will add RMS avaraging to you output data, and you can loose all your own calculation with shift registers.
The following code is just plain wrong:
You only shift the data, without actually changing the data. By incrementing the starting frequency you shift the FFT. So a signal that was detected at 55 Hz, no is plotted at 56 Hz. To your end user this is misleading.
One thing you need to be aware of in your code is that you don't have continious sampling. Each iteration of you for loop your data acquisition is started and stopped. You can verify this by plotting the t0's of the waveform that is captured. You'll notice they don't start at a constant interval.
A better aproach is to use the task created by the Express VI in the first iteration:
.
However you should then change the acquisition mode to 'continious samples':
Do not forget to close the task in the last iteration:
Instead of the shift register, you should work with an array which you empty before each run.

Windows equivalent to Linux's readahead syscall?

Is there a Windows equivalent to Linux's readahead syscall?
EDIT:
I would like a full function signature if possible, showing the equivalent offset/count parameters (or lower/upper).
Eg:
The Linux function signature is:
ssize_t readahead(int fd, off64_t *offset, size_t count);
and an example of it's use is
readahead(file, 100, 500);
Where "file" is a file descriptor previously set by a function like mmap. This call is reading 500 bytes at index 100.
EDIT 2:
Please read this if you are unsure what readahead does: http://linux.die.net/man/2/readahead
Yes. It is FileSystemControl FSCTL_FILE_PREFETCH.
It is used in Windows Vista and above for prefetching both when an application starts and at boot time.
It is also used by the SuperFetch technology that uses heuristics to load applications at approximately the times of day you generally use them.
FSCTL_FILE_PREFETCH itself is not documented on MSDN, but it is easy to figure out the parameter format by examining the DeviceIoControl calls made on app startup: Just start an application in the debugger that already has a .pf file in the c:\Windows\Prefetch directory and break on DeviceIoControl (or if you're using a kernel debugger, break when the NTFS driver receives its first FSCTL_FILE_PREFETCH). Examine the buffer passed in and compare it with the .pf file and the range actually used later. I did this once out of curiosity but didn't record the details.
In case you are unfamiliar with DeviceIoControl and IRP_MJ_FILESYSTEM_CONTROL, here are some links to look at:
FileSystemControl at the IRP level IRP_MJ_FILESYSTEM_CONTROL
DeviceIoControl, which is used to invoke FileSystemControl IRPs
Structure of IO Control Codes
As of Windows 8, there exists a more or less direct equivalent to madvise(MADV_WILLNEED), which is effectively the same thing (Windows has an unified VM/cache system).
Assuming that you have memory-mapped the file, you can thus use PrefetchVirtualMemory to prefetch it.
This is still slightly more complicated than you'd wish, but not nearly as harsh as DeviceIoControl. Also note that you can easily prefetch several independent, discontinuous ranges.
I am not sure if I understand correctly, in what you said 'Where "file" is a file descriptor previously set by a function like mmap. This call is reading 500 bytes at index 100.' That sounds suspiciously like seeking to the offset and read 500 bytes... but you want it to be pre-fetched ahead...
In C Code, it would look something like this:
fseek(fp, 100, SEEK_CUR);
fread(&data, 500, 1, fp);
But prefetching it, I guess, you would want to hook up some kind of events using waithandles, and when the event gets raised, the data gets stored somewhere in a buffer...
To be honest, I have not come across a such thing that does pre-fetching data...but Ray's answer surprised me, but then again it is only for Vista upwards, if you want to maintain compatibility...that's something to keep in mind... but the links below may be of help...
Ok, there was a blog discussing this, a library written in Delphi, the source code is here, browsing the code here, ok, it may not be what you want but it may help you point in the direction... Sorry if its not what you are looking for...
Hope this helps,
Best regards,
Tom.

Resources