1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
/* $Id: hw_audio.c,v 5.1 2003/01/26 21:44:40 lirc Exp $ */
/****************************************************************************
** hw_audio.c **************************************************************
****************************************************************************
*
* routines for using a IR receiver in microphone input using portaudio library
*
* Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
* Copyright (C) 2001, 2002 Pavel Machek <pavel@ucw.cz>
* Copyright (C) 2002 Matthias Ringwald <ringwald@inf.ethz.ch>
*
* Distribute under GPL version 2 or later.
*
* Using ... hardware ...
*
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <termios.h>
#ifdef __APPLE__
#include <util.h>
#else
#include <pty.h>
#endif
#include "hardware.h"
#include "ir_remote.h"
#include "lircd.h"
#include "receive.h"
#include "transmit.h"
#include "hw_default.h"
static int ptyfd; /* the pty */
/* PortAudio Includes */
#include <portaudio.h>
#define SAMPLE_RATE (44100)
#define NUM_CHANNELS (2)
#define DITHER_FLAG (0) /**/
/* Select sample format. */
#define PA_SAMPLE_TYPE paUInt8
typedef unsigned char SAMPLE;
typedef struct
{
int lastFrames[3];
int lastSign;
int pulseSign;
unsigned int lastCount;
}
paTestData;
PortAudioStream *stream;
extern struct ir_remote *repeat_remote;
extern struct rbuf rec_buffer;
char ptyName[256];
int master;
int myfd = -1;
void addCode( lirc_t data)
{
write( master, &data, sizeof( lirc_t ) );
}
/* This routine will be called by the PortAudio engine when audio is needed.
** It may be called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
*/
static int recordCallback( void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
PaTimestamp outTime, void *userData )
{
paTestData *data = (paTestData*)userData;
SAMPLE *rptr = (SAMPLE*)inputBuffer;
long i;
SAMPLE *myPtr = rptr;
unsigned int time;
int samplerate = SAMPLE_RATE;
int diff;
(void) outputBuffer; /* Prevent unused variable warnings. */
(void) outTime;
for ( i=0; i < framesPerBuffer; i++, myPtr++)
{
/* New Algo */
diff = abs(data->lastFrames[0] - *myPtr);
if ( diff > 100)
{
if (data->pulseSign == 0)
{
// we got the first signal, this is a PULSE
if ( *myPtr > data->lastFrames[0] )
{
data->pulseSign = 1;
}
else
{
data->pulseSign = -1;
}
}
if (data->lastCount > 0)
{
if ( *myPtr > data->lastFrames[0] && data->lastSign <= 0)
{
// printf("CHANGE ++ ");
data->lastSign = 1;
time = data->lastCount * 1000000 / samplerate;
if (data->lastSign == data->pulseSign)
{
addCode( time );
// printf("Pause: %d us, %d \n", time, data->lastCount);
}
else
{
addCode( time | PULSE_BIT );
// printf("Pulse: %d us, %d \n", time, data->lastCount);
}
data->lastCount = 0;
}
else if ( *myPtr < data->lastFrames[0] && data->lastSign >= 0)
{
// printf("CHANGE -- ");
data->lastSign = -1;
time = data->lastCount * 1000000 / samplerate;
if (data->lastSign == data->pulseSign)
{
// printf("Pause: %d us, %d \n", time, data->lastCount);
addCode( time );
}
else
{
// printf("Pulse: %d us, %d \n", time, data->lastCount);
addCode( time | PULSE_BIT);
}data->lastCount = 0;
}
}
}
if ( data->lastCount < 100000 )
{
data->lastCount++;
}
data->lastFrames[0] = data->lastFrames[1];
data->lastFrames[1] = *myPtr;
// skip 2. channel
if (NUM_CHANNELS == 2)
myPtr++;
}
return 0;
}
/*
decoding stuff
*/
#define BUFSIZE 20
#define SAMPLE 47999
lirc_t audio_readdata(lirc_t timeout)
{
lirc_t data;
int ret;
if (!waitfordata((long) timeout))
return 0;
ret=read(hw.fd,&data,sizeof(data));
if(ret!=sizeof(data))
{
LOGPRINTF(1,"error reading from lirc");
LOGPERROR(1,NULL);
dosigterm(SIGTERM);
}
return(data);
}
/*
interface functions
*/
paTestData data;
int audio_init()
{
PaError err;
int flags;
struct termios t;
LOGPRINTF(1,"hw_audio_init()");
//
logprintf(LOG_INFO,"Initializing %s...",hw.device);
init_rec_buffer();
rewind_rec_buffer();
// new
data.lastFrames[0] = 128;
data.lastFrames[1] = 128;
data.lastFrames[2] = 128;
data.lastSign = 0;
data.lastCount = 0;
data.pulseSign = 0;
err = Pa_Initialize();
if( err != paNoError ) goto error;
// Record some audio. --------------------------------------------
err = Pa_OpenStream
(
&stream,
Pa_GetDefaultInputDeviceID(),
NUM_CHANNELS, // stereo input
PA_SAMPLE_TYPE,
NULL,
paNoDevice,
0,
PA_SAMPLE_TYPE,
NULL,
SAMPLE_RATE,
512, // frames per buffer
0, // number of buffers, if zero then use default minimum
0, // flags
recordCallback,
&data );
if( err != paNoError ) goto error;
// open pty
if ( openpty( &master, &ptyfd, ptyName, 0, 0) == -1)
{
logprintf(LOG_ERR,"openpty failed");
logperror(LOG_ERR,"openpty()");
goto error;
}
// regular device file
if( tcgetattr( master, &t ) < 0 ) {
logprintf(LOG_ERR,"tcgetattr failed");
logperror(LOG_ERR,"tcgetattr()");
}
cfmakeraw( &t );
// apply file descriptor options
if( tcsetattr( master, TCSANOW, &t ) < 0) {
logprintf(LOG_ERR,"tcsetattr failed");
logperror(LOG_ERR,"tcsetattr()");
}
flags=fcntl(ptyfd,F_GETFL,0);
if(flags!=-1)
{
fcntl(ptyfd,F_SETFL,flags|O_NONBLOCK);
}
LOGPRINTF(LOG_INFO,"PTY name: %s", ptyName);
hw.fd=ptyfd;
err = Pa_StartStream( stream );
if( err != paNoError ) goto error;
return(1);
error:
Pa_Terminate();
logprintf(LOG_ERR, "an error occured while using the portaudio stream" );
logprintf(LOG_ERR, "error number: %d", err );
logprintf(LOG_ERR, "error message: %s", Pa_GetErrorText( err ) );
return (0);
}
int audio_deinit(void)
{
PaError err;
LOGPRINTF(1,"hw_audio_deinit()");
// close port audio
err = Pa_CloseStream( stream );
if( err != paNoError ) goto error;
Pa_Terminate();
// wair for terminaton
usleep(20000);
// close pty
close(master);
close(ptyfd);
return 1;
error:
Pa_Terminate();
logprintf(LOG_ERR,
"an error occured while using the portaudio stream");
logprintf(LOG_ERR,"error number: %d",err);
logprintf(LOG_ERR,"eError message: %s",Pa_GetErrorText(err));
return 0;
}
char *audio_rec(struct ir_remote *remotes)
{
if(!clear_rec_buffer()) return(NULL);
return(decode_all(remotes));
}
int audio_decode(struct ir_remote *remote,
ir_code *prep,ir_code *codep,ir_code *postp,
int *repeat_flagp,lirc_t *remaining_gapp)
{
return(receive_decode(remote,prep,codep,postp,
repeat_flagp,remaining_gapp));
}
struct hardware hw_audio=
{
"pty", /* simple device */
-1, /* fd */
LIRC_CAN_REC_MODE2, /* features */
0, /* send_mode */
LIRC_MODE_MODE2, /* rec_mode */
0, /* code_length */
audio_init, /* init_func */
NULL, /* config_func */
audio_deinit, /* deinit_func */
NULL, /* send_func */
audio_rec, /* rec_func */
audio_decode, /* decode_func */
audio_readdata,
"audio"
};
|