forked from ieatlint/MagRead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magdecode.cpp
153 lines (116 loc) · 3.04 KB
/
magdecode.cpp
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
#include "magdecode.h"
#include <QDebug>
MagDecode::MagDecode(QObject *parent) : QIODevice(parent) {
silenceCount = 0;
noiseDetected = false;
captureAudio = true;
silenceThreshold = 300;
normOffsetFound = false;
normOffset = 0;
defaultTimeOut = 10; //default to 10
timeOut = defaultTimeOut;
algorithm = "walk";
}
void MagDecode::start() {
open( QIODevice::WriteOnly );
}
void MagDecode::stop() {
close();
}
qint64 MagDecode::writeData( const char *data, qint64 dataLen ) {
if( !captureAudio )
return dataLen;
if( timeOut > 0 ) {
timeOut--;
return dataLen;
}
const qint16 *pcmDataBlock = reinterpret_cast<const qint16 *>( data );
int blockLen = dataLen / sizeof( qint16 );
/* Roughly estimates what true 0 amplitude is */
if( !normOffsetFound ) {
for( int i = 0; i < blockLen; i++ ) {
normOffset += pcmDataBlock[ i ];
}
normOffset /= blockLen;
normOffsetFound = true;
qDebug() << "Norm offset is" << normOffset;
return dataLen;
}
for( int i = 0; i < blockLen; i++ ) {
if( qAbs( pcmDataBlock[ i ] ) - normOffset > silenceThreshold ) {
noiseDetected = true;
silenceCount = 0;
} else if( noiseDetected ) {
silenceCount++;
}
}
pcmData.append( pcmDataBlock, blockLen );
if( noiseDetected ) {
if( silenceCount > 200 ) {
captureAudio = false;
processSwipe();
} else if( pcmData.count() > 20000 ) {
pcmData.clear();
silenceCount = 0;
noiseDetected = 0;
}
} else
pcmData.clear();
return dataLen;
}
void MagDecode::processSwipe() {
bool valid;
timeOut = defaultTimeOut;
//Normalize the audio based on calculated 0 level
for( int i = 0; i < pcmData.count(); i++ )
pcmData[ i ] -= normOffset;
msData *ms = ms_create( pcmData.data(), pcmData.count() );
ms_set_peakThreshold( ms, silenceThreshold );
if( algorithm == "intersect" ) {
ms_peaks_find( ms );
qDebug() << "Intersection algorithm used";
} else {
ms_peaks_find_walk( ms );
qDebug() << "Walk algorithm used";
}
ms_peaks_filter_group( ms );
// ms_save( ms, "/tmp/swipe" );
ms_decode_peaks( ms );
valid = ( ms_decode_bits( ms ) == 0 );
MagCard card;
card.charStream = ms_get_charStream( ms );
card.bitStream = ms_get_bitStream( ms );
card.encoding = ms->dataType;
card.swipeValid = valid;
emit cardRead( card );
noiseDetected = false;
silenceCount = 0;
captureAudio = true;
// qDebug() << "Card Validity:" << valid;
// qDebug() << "Chars:" << ms_get_charStream( ms );
// qDebug() << "Bits:" << ms_get_bitStream( ms );
ms = ms_free( ms );
pcmData.clear();
}
qint64 MagDecode::readData( char *data, qint64 len ) {
Q_UNUSED( data );
Q_UNUSED( len );
return 0;
}
void MagDecode::setThreshold( int threshold ) {
silenceThreshold = threshold;
}
void MagDecode::setTimeOut( int _timeOut ) {
defaultTimeOut = _timeOut;
}
void MagDecode::setNorm( int _normOffset ) {
normOffsetFound = true;
normOffset = _normOffset;
}
void MagDecode::setAlgorithm( QString _algorithm ) {
qDebug() << "Settings algorithm to" << _algorithm;
if( _algorithm == "intersect" )
algorithm = _algorithm;
else
algorithm = "walk";
}