2 This file is part of MagRead.
4 MagRead is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 MagRead is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with MagRead. If not, see <http://www.gnu.org/licenses/>.
17 Written by Jeffrey Malone <ieatlint@tehinterweb.com>
18 http://blog.tehinterweb.com
20 #include "audioinput.h"
27 AudioInput::AudioInput(QObject *parent) : QThread(parent) {
28 paSpec.format = PA_SAMPLE_S16LE;
39 AudioInput::~AudioInput() {
41 pa_simple_free( paServer );
44 void AudioInput::run() {
46 qDebug() << "AudioInput::run() start";
49 paServer = pa_simple_new( NULL, "MagRead", PA_STREAM_RECORD, "source.hw0", "Recording", &paSpec, NULL, NULL, &paError );
51 QString err = "Failed to open PulseAudio server:\n";
52 err.append( pa_strerror( paError ) );
60 qint16 pcmDataBlock[ BUF_SIZE ];
64 while( captureAudio ) {
65 if( pa_simple_read( paServer, pcmDataBlock, ( sizeof( qint16 ) * BUF_SIZE ), &paError ) ) {
66 QString err = "Failed to read a block of data:\n";
67 err.append( pa_strerror( paError ) );
73 for( i = 0; i < BUF_SIZE; i++ ) {
74 if( qAbs( pcmDataBlock[ i ] ) > silenceThresh ) {
83 pcmData.append( pcmDataBlock, BUF_SIZE );
86 if( noise && silence > 200 ) {
88 } else if( noise && ( pcmData.count() / BUF_SIZE ) > 30 ) {
89 /* Stream is too fucking long, we're clearing and restarting */
96 pa_simple_free( paServer );
103 qDebug() << "AudioInput::run() stop";
107 void AudioInput::stop() {
109 qDebug() << "AudioInput::stop() ";
111 captureAudio = false;
114 void AudioInput::processSwipe() {
118 qDebug() << "AudioInput::processSwipe() start";
124 ms = ms_create( pcmData.data(), pcmData.count() );
126 qDebug() << "AudioInput::processSwipe() 1";
129 //ms_peaks_find( ms );
130 ms_peaks_find_walk( ms );
131 ms_peaks_filter_group( ms );
133 qDebug() << "AudioInput::processSwipe() 2";
136 ms_decode_peaks( ms );
138 qDebug() << "AudioInput::processSwipe() 3";
141 retval = ( ms_decode_bits( ms ) == 0 ) ;
143 card.charStream = ms_get_charStream( ms );
144 if( !card.charStream.isEmpty() ) {
145 card.encoding = ms->dataType;
146 card.swipeValid = retval;
148 emit cardRead( card );