Showing posts with label sound. Show all posts
Showing posts with label sound. Show all posts

FFT library and plot

Since FFT was the subject, it seemed fitting to do some Fourier using the FFTW3 library on some audio. Using an example from the net as a starting point was a time saver in one way and not in another. Debugging code is always fun so ... I had used the AT&T voice server to get some audio for the expression "Segmentation fault" as an example and so that was the file to perform FFT with. Step one was to compile and it was necessary to add a few includes for memset and math to resolve some "implicit declaration warns" and it compiled fine. Next was running test and it printed out "Segmentation fault". It didn't seem there was a line to print the input wav file name, but it could have been overlooked. Since my mind is wandering it is an unusual thing that Spock used a Jewish ceremonial gesture as a "Vulcan" symbol of "live long and prosper".

So it took about one second to realize that it was a real SIGSEGV. It took another few minutes to scope out the fact that rplan was "NULL and VOID", which an assert would have been fitting if it was being proffered as something to be used by others, and the gnuplot code was a bit funky and that was fixed in about 30 seconds. Also it was using a "Magic" number to match the file data structure size, even though it is a given in the header of the .wav. Fixed and fixed. The code below shows the two methods of making the FFTW3 plan and the bottom will return NULL with the newer versions. Understandable, changing the spec is not likely to retrofit every code against an interface that is in flux. If the interface allows, somebody will cut that corner anyway so all in all it wasn't too much work to get the image above and left.



rplan = fftw_plan_r2r_1d(n, rdata, idata, FFTW_R2HC, FFTW_ESTIMATE); fftw_execute(rplan);

rplan = fftw_plan_r2r_1d(n, rdata, idata, FFTW_R2HC, FFTW_FORWARD); fftw_execute(rplan);

It does seem fitting that the wav file chosen was "Segmentation fault" by name and content, as if it was an omen of the future. It would have been more fun if SDL sound was included and "Segmentation fault" was spoken right before it crashed.

My fingers speak music from the ages

It has come a long way from Electric Dreams. When I can scarf a midi file of Bach Minuet in G from here, find out how to use a soundfont here and get lmms through the repositories. It is really nice and intuitive. These devs have done some beautiful work. Just get LMMS and try the demos and examples. It is really easy to use and it really rocks the house.

It seems that I could do some meta coding with this also.

<embed src="http://sites.google.com/site/moteyalpha/midibachdream2.ogg" width=250 height=100 autostart=false repeat=true loop=true> </embed>

I used kdenlive to create this and it is just a silly thing that changed instruments and I thought it sounded like a cat doing the Minuet in G. All of this is about 30 seconds of real work for me. The computer sweat for many billions of nanoseconds doing its job of converting midi to audio and the converting audio to mpg and then sending it out to the internet. It is just proof of concept and not intended to be art or even pleasant.

SDL simple example

It can't get much simpler than this. I did notice one odd thing, when I tried to create errs to test my case error handling, SDL_Init(value), doesn't give a rat's derriere what value is passed. I mixed the code up a little to make it look pretty here and so if you copy it, be warned it isn't some file.c that I have compiled. It is just reference to say, "this is pretty simple" and this much code is enough to make a sound play. I got most of this from Grubby Games and then tested it, modified it, compiled it, and now I am going to use the information gained to create my own application that is a bit more elegant. So thanks to them at Grubby Games, as their code actually works and that is not always true for some sites. I also got information from the SDL wiki, a German SDL site and many others. I like to investigate the breadth and depth of the issues and this looks like the way to go for sound. I could do all the stuff in buffers and then send it to dsp, but I don't want to reinvent fire today, maybe next week.

#include <stdlib.h> #include <stdio.h> #include "SDL/SDL.h" #include "SDL/SDL_audio.h" #include "SDL/SDL_mixer.h" #include "SDL/SDL_sound.h" int errPrint(int errno, char *namish); /*compile with [gcc -Wall sdlMix.c -lSDL -lSDL_mixer] Of course you would need several DEV headers and the libraries themselves, but this isn't a kindergarten Linux or C example.*/ int main(int argc,char * argv[]) { int audio_rate = 22050;Uint16 audio_format = AUDIO_S16SYS; int audio_channels = 2;int audio_buffers = 4096;int channel; char playName[]="res/applause.wav"; if (SDL_Init( SDL_INIT_AUDIO) != 0) {errPrint(1,playName)} if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0) {errPrint(2,playName);} Mix_Chunk *sound = NULL; sound = Mix_LoadWAV(playName); if(sound == NULL) {errPrint(3,playName);} channel = Mix_PlayChannel(-1, sound, 0); if(channel == -1) {errPrint(4,playName);} while(Mix_Playing(channel) != 0); Mix_FreeChunk(sound); Mix_CloseAudio(); SDL_Quit();return 0;} int errPrint(int errno, char *namish) { char errString[200]; switch (errno) { case 1: sprintf(errString, "Unable to initialize SDL: [%s]\n", Mix_GetError());break; case 2: sprintf(errString, "Unable to initialize audio: [%s]\n", Mix_GetError());break; case 3: sprintf(errString, "Unable to load WAV file: %s SDL err = [ %s ]\n",namish, Mix_GetError()); break; case 4: sprintf(errString, "Unable to play WAV file: %s SDL err = [%s]\n", Mix_GetError());break; default : sprintf(errString, "Unable to even make a simple err display work! DOH! \n");break;} fprintf(stderr,errString); exit(1);return 0;}

SDL sound

I added sound to the puzzle game and it makes a big difference in the experience. I am considering adding a running theme song and some special effects with sound. I recently got a BOSE sound system that contained a woofer and base amplifier which was broken. I fixed it for something to do. I was never a big sound or music person. When I tested the amplifier with some music that included the 1812 overture, I was pleasantly surprised at the quality and now enjoy the music more.

Here is a link to some SDL examples and explanation of how SDL is used. I tried out "simplemixer.c" and it is functional. I got the zip, unpacked it, did "make" and then ran it. It is written in C and so I will look through this and add the methods to my game test program. I was testing with OpenAL, but SDL seems to be a better idea and maintains compatibility across platforms. I will integrate the methods and report anything interesting I encounter.

I also decided to add some animation to the puzzle solution which flips the tiles like on "Wheel of Fortune" with a little sound. It is simple to do with OpenGL and I just change the "Y-axis" size from the size of an empty square to the size of the texture in timed steps and it seems like the object is rotating. The timing requires some real time work with the system and so that can be interesting when dealing with multi-tasking. I suppose I will use a rubber banding technique so that the steps match the real-time state. The problem is that it will jerk if some other task interferes, but that is better than delaying the response. There is no perfect way to deal with delays due to some critical event. On Windeeze it might be very odd, but I think that Linux maintains a good time slicing and so I assume it will be fine, unless I run into some delay due to some system critical activity.

Contributors

Automated Intelligence

Automated Intelligence
Auftrag der unendlichen LOL katzen