Monday, June 27, 2011

iPhone Development

Adding an overlay to an image and saving the composite.

#import 
....
#pragma mark -
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{   
    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
    overlay.alpha = 0.6;  // Customize the opacity of the top image.
    [imageView addSubview:overlay];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    //UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // Save image
    UIImageWriteToSavedPhotosAlbum(imageView.image, self,                                    @selector(image:didFinishSavingWithError:contextInfo:), context);
    [picker dismissModalViewControllerAnimated:YES];
}



Tuesday, April 5, 2011

Sunday, April 3, 2011

The Art of cyWar

http://www.nytimes.com/2010/02/23/books/23watchers.html

operations like Able Danger, a data-mining program at the Pentagon that became briefly notorious because of the erroneous claim by a few military officials who worked on it that it had been able to identify Mohammed Atta, the 9/11 hijacker, as a possible threat before the attacks.


“The T.I.A. tools crashed. They were simply incapable of processing so much information in real time. Like balloons affixed to a fire hydrant, they burst.”
--
The Watchers tells the story of these programs and the bureaucratic conflicts that evolved as the Intelligence Community tried to deal with the terrorist threat. 
--
http://www.time.com/time/nation/article/0,8599,1973131,00.html
--

Reading materials will be identified throughout the course and made available via
hard copy or online reference.
Required reading includes The Watchers: The Rise of America’s Surveillance State,
by Shane Harris, ISBN 978-1-59420-245-2, Penguin Press, 2010.
Recommend reference text (not required): Introduction to Computer Security, Matt
Bishop, ISBN 0-321-24744-2, Addison-Wesley, 2005.
Recommend reference text (not required): Computer Networks, Andrew S.
Tanenbaum, ISBN 0130661023, Prentice Hall, 4th Edition, 2002.

Wednesday, March 16, 2011

Qt and Sound generation using libraries from audres.org

Qt supports sound in various ways and Nokia/Trolltech is currently working options that make sound processing easier to work with. One of the methods to do playback is to use the Phonon module. This module operation using the gstreamer libraries, but does not allow you to control individual channels.

Saturday, January 1, 2011

Tuesday, November 16, 2010

Audio Programming on Mac OS and Linux


Portaudio And Libsndfile WAV playback with channel selection

/** @file pa_lsf_lessblocking.c
    @ingroup test_src
    @brief Read WAV file and playback on one channel if      stereo file. Implemented using the blocking API      (Pa_ReadStream(), Pa_WriteStream() ), but still      allowing you to stop the stream if using threads.
    @author Sal Aguinaga http://serpentinista.blogspot.com
    @author Phil Burk  http://www.softsynth.com
    @author Ross Bencina rossb@audiomulch.com
*/
#include
#include
#include "portaudio.h"
#include
/* #define SAMPLE_RATE  (17932) // Test failure to open with this value. */
#define SAMPLE_RATE  (44100)
#define FRAMES_PER_BUFFER (1024)
#define LATENCY_MSEC (2000) /* new */
#define NUM_SECONDS     (5)
#define NUM_CHANNELS    (2)
/* #define DITHER_FLAG     (paDitherOff)  */
#define DITHER_FLAG     (0) /**/
/* Select sample format. */
#if 1
#define PA_SAMPLE_TYPE  paFloat32
typedef float SAMPLE;
#define SAMPLE_SILENCE  (0.0f)
#define PRINTF_S_FORMAT "%.8f"
#elif 1
#define PA_SAMPLE_TYPE  paInt16
typedef short SAMPLE;
#define SAMPLE_SILENCE  (0)
#define PRINTF_S_FORMAT "%d"
#elif 0
#define PA_SAMPLE_TYPE  paInt8
typedef char SAMPLE;
#define SAMPLE_SILENCE  (0)
#define PRINTF_S_FORMAT "%d"
#else
#define PA_SAMPLE_TYPE  paUInt8
typedef unsigned char SAMPLE;
#define SAMPLE_SILENCE  (128)
#define PRINTF_S_FORMAT "%d"
#endif
/*******************************************************************/
//typedef struct  {
// float outputBuffer;
// int left_phase;
// int right_phase;
//} paTestData
/*******************************************************************/
int main(int argc, char *argv[]);
/*******************************************************************/
int main(int argc, char *argv[])
{
printf("libsnd portaudio playback\n"); fflush(stdout);
if (argc != 2) {
fprintf(stderr, "Expecting wav file as argument\n");
return 1;
}
    PaStreamParameters /*inputParameters,*/ outputParameters;
    PaStream *stream;
    PaError err;
    SAMPLE *recordedSamples;
    int i;
    int totalFrames;
    int numSamples;
    int numBytes;
    SAMPLE max, average, val;
float *buffer;
    
    // Open sound file
SF_INFO sndInfo;
SNDFILE *sndFile = sf_open(argv[1], SFM_READ, &sndInfo);
if (sndFile == NULL) {
fprintf(stderr, "Error reading source file '%s': %s\n", argv[1], sf_strerror(sndFile));
return 1;
}
    
// Check format - 16bit PCM
if (sndInfo.format != (SF_FORMAT_WAV | SF_FORMAT_PCM_16)) {
fprintf(stderr, "Input should be 16bit Wav\n");
sf_close(sndFile);
return 1;
}
// Check channels 
if (sndInfo.channels > 2) {
fprintf(stderr, "Wrong number of channels\n");
sf_close(sndFile);
return 1;
}
if (sndInfo.channels == 1) {
// Allocate memory
buffer = malloc(sndInfo.frames * sizeof(float));
//float *buffer = calloc(sndInfo.frames * sizeof(float));
if (buffer == NULL) {
fprintf(stderr, "Could not allocate memory for file\n");
sf_close(sndFile);
return 1;
}
// Load data
long numFrames = sf_readf_float(sndFile, buffer, sndInfo.frames);
// Check correct number of samples loaded
if (numFrames != sndInfo.frames) {
fprintf(stderr, "Did not read enough frames for source\n");
sf_close(sndFile);
free(buffer);
return 1;
}
// Output Info
printf(" Read %ld frames from %s,\n "
  "Channel(s):%d,\n "
  "Sample rate: %d,\n "
  "Length: %fs\n",
  numFrames, argv[1], sndInfo.channels, sndInfo.samplerate, (float)numFrames/sndInfo.samplerate);
// Setup for Output
totalFrames = (int)numFrames; /* */
numSamples = totalFrames * sndInfo.channels;
numBytes = numSamples * sizeof(SAMPLE);
recordedSamples = (SAMPLE *) malloc( numBytes );
if( recordedSamples == NULL )
{
printf("Could not allocate record array.\n");
exit(1);
}
for( i=0; i
sf_close(sndFile);
free(buffer);
} else { // 2 channels
    buffer = malloc(sndInfo.frames * 2 * sizeof(float));  // * 1 to put it on one channel.
if (buffer == NULL) {
            fprintf(stderr, "Could not allocate memory for file\n");
            sf_close(sndFile);
            return 1;
}
long numFrames = sf_readf_float(sndFile, buffer, sndInfo.frames);
if (numFrames != sndInfo.frames) {
            fprintf(stderr, "Did not read enough frames for source\n");
            sf_close(sndFile);
            free(buffer);
            return 1;
}
printf(" Read %ld frames from %s,\n "
  "Channel(s):%d,\n "
  "Sample rate: %d,\n "
  "Length: %fs\n",
  numFrames, argv[1], sndInfo.channels, sndInfo.samplerate, (float)numFrames/sndInfo.samplerate);
// Setup for Output
totalFrames = (int)numFrames; /* */
numSamples = totalFrames * sndInfo.channels;
// numSamples = totalFrames / 2; // changed it to 1 channel 
/*************/
// double duration = (double)numFrames/sndInfo.samplerate;
// double *singleChanOutput = (double *) malloc (numFrames * sizeof(doube));
// if (singleChanOutput == NULL) {
// fprintf(stderr, "Could not allocate buffer for output\n");
// }
/*************/
// numBytes = numSamples * sizeof(SAMPLE);
recordedSamples = (SAMPLE *) malloc( numSamples * sizeof(SAMPLE));
if( recordedSamples == NULL )
{
printf("Could not allocate record array.\n");
exit(1);
}
printf("numFrames = %d, numSamples =%d\n", (int)numFrames, numSamples);
for( i=0; i2; i++ ) {
recordedSamples[i*2] = buffer[i*2];
recordedSamples[i*2+1] = 0;
}
        sf_close(sndFile);
        free(buffer);
printf("totalFrames = %d\n"
  "numSamples = %d\n"
  ,totalFrames, numSamples);
}
    // totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */
//  numSamples = totalFrames * NUM_CHANNELS;
    err = Pa_Initialize();
    if( err != paNoError ) goto error;
    /* Measure maximum peak amplitude. */
    max = 0;
    average = 0;
    for( i=0; i
    {
        val = recordedSamples[i];
        if( val < 0 ) val = -val; /* ABS */
        if( val > max )
        {
            max = val;
        }
        average += val;
    }
    average = average / numSamples;
    printf("Sample max amplitude = "PRINTF_S_FORMAT"\n", max );
    printf("Sample average = "PRINTF_S_FORMAT"\n", average );
/*  Was as below. Better choose at compile time because this
    keeps generating compiler-warnings:
    if( PA_SAMPLE_TYPE == paFloat32 )
    {
        printf("sample max amplitude = %f\n", max );
        printf("sample average = %f\n", average );
    }
    else
    {
        printf("sample max amplitude = %d\n", max );
        printf("sample average = %d\n", average );
    }
*/
/*####################################################################
 * Playback recorded data
 ********************************************************************/
    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
    outputParameters.channelCount = sndInfo.channels;
    outputParameters.sampleFormat =  PA_SAMPLE_TYPE;
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
    printf("Begin playback.\n"); fflush(stdout);
    err = Pa_OpenStream(
              &stream,
              NULL, /* no input */
              &outputParameters,
              sndInfo.samplerate,
              FRAMES_PER_BUFFER,
              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
              NULL, /* no callback, use blocking API */
              NULL ); /* no callback, so no callback userData */
    if( err != paNoError ) goto error;
    if( stream )
    {
        err = Pa_StartStream( stream );
        if( err != paNoError ) goto error;
        printf("Waiting for playback to finish.\n"); fflush(stdout);
for (i=0; i
err = Pa_WriteStream( stream, &recordedSamples[i*sndInfo.channels], min(FRAMES_PER_BUFFER,totalFrames-i) );
if( err != paNoError ) {
goto error;
break;
}
}
        err = Pa_CloseStream( stream );
        if( err != paNoError ) goto error;
        printf("Done.\n"); fflush(stdout);
    }
    free( recordedSamples );
    Pa_Terminate();
    return 0;
error:
    Pa_Terminate();
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
    fprintf( stderr, "Error number: %d\n", err );
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
    return -1;
}

Tuesday, September 21, 2010

What the Phonon: How to select individual audio channels (left, right or both)

I've just searched the Trolltech site for: Does Phonon allows you to control Left or Right audio channels and there is no mention of this, so before I go building a custom WAV playing device using the example: qmusicplayer .... Does it? How?

The blog post by Garet: http://labs.qt.nokia.com/2010/05/10/low-level-audio-processing-with-qtmultimedia/ titled:
Low-level audio processing with QtMultimedia
mentions the QAudioFormat::setChannels() ... but is this for generating audio 'data' and not for selecting the physical audio channel of a given audio device?

I am using this in Mac OS, and the 'capabilities' example only returns: "Internal Speakers" for what is called Available audio devices. If I try this in Linux, will it give me more options? But as I write this I realize that these capabilities still don't give me the granularity of choosing output channels (left or right, for 2-channel audio device), right or am I in the dark about some new features?