#include #include #include #include #include #include #include #include #include /* #define SAMPLE_RATE (17932) // Test failure to open with this value. */ #define SAMPLE_RATE (44100) #define FRAMES_PER_BUFFER (1024) #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 int running = 1; void signalHandler(int sig) { running = 0; } /*******************************************************************/ int main(void); int main(void) { printf("Parallel Flasher\n"); printf("----------------\n\n"); printf("Initializing PortAudio...\n"); PaStreamParameters inputParameters, outputParameters; PaStream *stream; PaError err; SAMPLE *recordedSamples; int i; int maxFrames; int numSamples; int numBytes; SAMPLE max, average, val; // Set ctrl-c handler signal(SIGINT, signalHandler); printf("patest_read_record.c\n"); fflush(stdout); //totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */ maxFrames = SAMPLE_RATE*1; numSamples = maxFrames * NUM_CHANNELS; numBytes = numSamples * sizeof(SAMPLE); recordedSamples = (SAMPLE *) malloc( numBytes ); if( recordedSamples == NULL ) { printf("Could not allocate record array.\n"); exit(1); } for( i=0; idefaultLowInputLatency; inputParameters.hostApiSpecificStreamInfo = NULL; /* Record some audio. -------------------------------------------- */ err = Pa_OpenStream( &stream, &inputParameters, NULL, /* &outputParameters, */ SAMPLE_RATE, 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; printf("Initializing parallel port...\n"); int fd; int result; // Set ctrl-c handler signal(SIGINT, signalHandler); // Open the parallel port for reading and writing fd = open("/dev/parport0", O_RDWR); if (fd == -1) { perror("Could not open parallel port"); return 1; } // Try to claim port if (ioctl(fd, PPCLAIM, NULL)) { perror("Could not claim parallel port"); close(fd); return 1; } // Set the Mode int mode = IEEE1284_MODE_BYTE; if (ioctl(fd, PPSETMODE, &mode)) { perror("Could not set mode"); ioctl(fd, PPRELEASE); close(fd); return 1; } // Set data pins to output int dir = 0x00; if (ioctl(fd, PPDATADIR, &dir)) { perror("Could not set parallel port direction"); ioctl(fd, PPRELEASE); close(fd); return 1; } char dataH = 0xFF; char dataL = 0x00; printf("Starting!\n\n"); err = Pa_StartStream( stream ); if( err != paNoError ) goto error; Pa_ReadStream(stream, recordedSamples, maxFrames); while (running) { long toRead = Pa_GetStreamReadAvailable(stream); if (toRead > maxFrames) toRead = maxFrames; err = Pa_ReadStream(stream, recordedSamples, toRead); if( err != paNoError ) goto error; for (i = 0; i < toRead; i++) { if (recordedSamples[i] > 0.003) { printf("!! FLASH !!\n"); //usleep(1000*100); // Output some data // Note that data is passed by a pointer ioctl(fd, PPWDATA, &dataH); usleep(1000*100); ioctl(fd, PPWDATA, &dataL); printf("Waiting 20 seconds...\n"); sleep(20); Pa_StopStream(stream); Pa_StartStream(stream); printf("Listening...\n"); toRead = i; } } } printf("Shutting down parllel port...\n"); // Release and close the parallel port ioctl(fd, PPRELEASE); close(fd); printf("and PortAudio...\n"); err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; 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; }