forked from ieatlint/SWipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagdecode.cpp
More file actions
115 lines (85 loc) · 2.28 KB
/
magdecode.cpp
File metadata and controls
115 lines (85 loc) · 2.28 KB
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
#include "magdecode.h"
#include <QDebug>
MagDecode::MagDecode(QObject *parent) : QIODevice(parent) {
silenceCount = 0;
noiseDetected = false;
captureAudio = true;
silenceThreshold = 300;
normOffset = 0;
}
void MagDecode::start() {
open( QIODevice::WriteOnly );
}
void MagDecode::stop() {
close();
}
qint64 MagDecode::writeData( const char *data, qint64 dataLen ) {
if( !captureAudio )
return dataLen;
const qint16 *pcmDataBlock = reinterpret_cast<const qint16 *>( data );
int blockLen = dataLen / sizeof( qint16 );
/* Roughly estimates what true 0 amplitude is */
if( normOffset == 0 ) {
for( int i = 0; i < blockLen; i++ ) {
normOffset += pcmDataBlock[ i ];
}
normOffset /= blockLen;
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;
//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 );
ms_peaks_find_walk( ms );
ms_peaks_filter_group( ms );
ms_save( ms, "/home/ieatlint/code/SWipe/tests/x" );
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;
}