From 436d30d5e127018ac5d1219b9fa8c2b770913d31 Mon Sep 17 00:00:00 2001 From: Gerolf Ziegenhain Date: Mon, 3 Oct 2016 17:47:10 +0200 Subject: [PATCH] raspi hack begin --- src/beep.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/beep.h | 4 +++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/beep.c b/src/beep.c index a49a1be..9ded1d3 100644 --- a/src/beep.c +++ b/src/beep.c @@ -3,8 +3,11 @@ #include #include // for usleep() #include + #include "beep.h" +// OSX Code +#ifdef __MACH__ // http://stackoverflow.com/questions/7678470/generating-sound-of-a-particular-frequency-using-gcc-in-ubuntu static PaStream *stream; @@ -84,7 +87,7 @@ int buzzer_start(void) err = Pa_Initialize(); if( err != paNoError ) goto error; - outputParameters.device = 1;//Pa_GetDefaultOutputDevice(); /* default output device */ + outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ outputParameters.channelCount = 1; /* stereo output */ outputParameters.sampleFormat = paUInt8; /* 32 bit floating point output */ outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; @@ -179,3 +182,74 @@ int beep_test(void) return 0; } +#endif + + + +// Raspi does not work with portaudio?! +#ifdef __ARM__ +/* + * This extra small demo sends sinusoidal samples to your speakers. + */ +#include +#include +#include + +static char *device = "hw:0,0"; /* playback device */ +snd_output_t *output = NULL; + +#define FRAMES 16384L + +int16_t buffer[FRAMES*2]; /* 16bit stereo sound samples */ +int main(void) +{ + int err; + double p1,p2,f1,f2; + unsigned int i,j; + snd_pcm_t *handle; + snd_pcm_sframes_t frames; + + if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { + printf("Playback open error: %s\n", snd_strerror(err)); + exit(EXIT_FAILURE); + } + if ((err = snd_pcm_set_params(handle, + SND_PCM_FORMAT_S16_LE, + SND_PCM_ACCESS_RW_INTERLEAVED, + 2, + 48000, + 1, + 500000)) < 0) { /* 0.5sec */ + printf("Playback open error: %s\n", snd_strerror(err)); + exit(EXIT_FAILURE); + } + f1 = 0.02; + f2 = 0.02; + p1 = p2 = 0.0; + for (i = 0; i < 64; i++) { + for (j = 0; j < FRAMES*2; j+=2) + { + buffer[j] = 30000.0 * sin(p1); + buffer[j+1] = 30000.0 * sin(p2); + p1 += f1; + p2 += f2; + } + f1 += 0.0001; + f2 += 0.00012; + + + frames = snd_pcm_writei(handle, buffer, FRAMES); + + if (frames < 0) + frames = snd_pcm_recover(handle, frames, 0); + if (frames < 0) { + printf("snd_pcm_writei failed: %s\n", snd_strerror(err)); + break; + } + if (frames > 0 && frames < FRAMES) + printf("Short write (expected %li, wrote %li)\n", FRAMES, (long)frames); + } + snd_pcm_close(handle); + return 0; +} +#endif diff --git a/src/beep.h b/src/beep.h index 9416409..3ba212f 100644 --- a/src/beep.h +++ b/src/beep.h @@ -10,6 +10,8 @@ #define M_PI (3.14159265) #endif +// OSX Code +#ifdef __MACH__ typedef struct { uint32_t total_count; @@ -32,6 +34,8 @@ int buzzer_start(void); int buzzer_stop(); void msleep(int d); int beep_test(void); +#endif + // compatibility to old interface int beep(double freq_hz, double duration_sec);