-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncodeSignalFSK.m
More file actions
724 lines (435 loc) · 20.2 KB
/
Copy pathEncodeSignalFSK.m
File metadata and controls
724 lines (435 loc) · 20.2 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//
// iR Control
//
// Created by Omar & Alejandro . @osharim
// Copyright (c) 2015 Bentel Mexico. All rights reserved.
// www.bentel.mx
#import "EncodeSignalFSK.h"
@implementation EncodeSignalFSK{
float amplitude ;
float phaseShiftInPi;
const char* bit0;
const char* bit1 ;
NSString *heading ;
NSString *tailing;
NSString *_KeyValue;
float lowFreq;
float highFreq;
}
const double sampleRate = 44100;
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
- (void)initAudioSignal{
//__bridge transfers a pointer between Objective-C and Core Foundation with no transfer of ownership.
OSStatus result = AudioSessionInitialize(NULL, NULL, ToneInterruptionListener, (__bridge void *)(self));
if (result == kAudioSessionNoError)
{
//way to allow sounds to be played even if the ringer switch is set to off is like so
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
}
}
/*
Configuramos el protocolo y codigo a enviar
*/
- (void)SetProtocol:(NSString*)protocol AndSetKeyPressed:(NSString*)keyPressed{
_Protocol = protocol;
_KeyValue = keyPressed;
}
/*
CONFIGURACION DE PULSOS */
- (void)ConfigurePulseLeaderWithDataInMs:(NSString *)miliseconds{
if([miliseconds isEqualToString:@"1"]){
// CONFIGURACION PARA BIT 0
amplitude = 1;
phaseShiftInPi = 1.99;
heading = @"^";
tailing = @"_________________";
lowFreq = 559;
highFreq = 210;
}
if([miliseconds isEqualToString:@"2"]){
// CONFIGURACION PARA BIT 1
amplitude = 1;
phaseShiftInPi = 0;
heading = @"_^^^^^^^^^^^^^";
tailing = @"^";
lowFreq = 110;
highFreq = 565;
}
}
/* Enviamos los datos */
- (void)SendDataToEmiter{
if (toneUnit){
status = 0;
AudioOutputUnitStop(toneUnit);
AudioUnitUninitialize(toneUnit);
AudioComponentInstanceDispose(toneUnit);
toneUnit = nil;
if(dataQueue != NULL){
free(dataQueue);
dataQueue = NULL;
}
}else{
[self initAudioSignal];
//EN USO
status = 1;
qIndex = 0;
bit0 = [@"_" UTF8String];
bit1 = [@"^" UTF8String];
[self ConfigurePulseLeaderWithDataInMs:@"1"];
//Configuracion frecuencia
int FrecuencyLowData = round(sampleRate/lowFreq);
int lowSize =FrecuencyLowData*sizeof(double);
int FrecuencyHighData = round(sampleRate/highFreq);
int highSize = FrecuencyHighData*sizeof(double);
//Creamos array con el tamaño de lowSteps
double BufferForLowData[FrecuencyLowData];
double BufferForHighData[FrecuencyHighData];
//calcula FSK y guarda el pulso en alto en el buffer asignado BufferForLowData | BufferForHighData
BOOL changeFreq = false;
int type_bit = 0;
GenerateFSKEncoding(BufferForLowData, lowFreq, FrecuencyLowData, amplitude, phaseShiftInPi,changeFreq,type_bit);
GenerateFSKEncoding(BufferForHighData, highFreq, FrecuencyHighData, amplitude, phaseShiftInPi,changeFreq, type_bit);
//en este punto tenemos guardado en cada buffer el tamaño de la señal en bajo y alto.
// BIS SE ENVIARAN PARA PODER PARAR CUANDO SE TERMINE DE ENVIAR
for (int i=0; i<heading.length; ++i) {
NSString* h = [heading substringWithRange:NSMakeRange(i, 1)];
if([h isEqualToString:@"_"]) {
qSteps+=FrecuencyLowData;
} else if([h isEqualToString:@"^"]) {
qSteps+=FrecuencyHighData;
}
}
// CONTAMOS CUANTOS BIS SE ENVIARAN PARA PODER PARA CUANDO SE TERMINE DE ENVIAR
for (int i=0; i<tailing.length; ++i) {
NSString* t = [tailing substringWithRange:NSMakeRange(i, 1)];
if([t isEqualToString:@"_"]) {
qSteps+=FrecuencyLowData;
} else if([t isEqualToString:@"^"]) {
qSteps+=FrecuencyHighData;
}
}
//****************************************************************************************
[self ConfigurePulseLeaderWithDataInMs:@"2"];
//Configuracion frecuencia
int FrecuencyLowData2 = round(sampleRate/lowFreq);
int lowSize2 =FrecuencyLowData2*sizeof(double);
int FrecuencyHighData2 = round(sampleRate/highFreq);
int highSize2 = FrecuencyHighData2*sizeof(double);
//Creamos array con el tamaño de lowSteps
double BufferForLowData2[FrecuencyLowData2];
double BufferForHighData2[FrecuencyHighData2];
changeFreq = true;
// tipo de bit
type_bit = 1;
//calcula FSK y guarda el pulso en alto en el buffer asignado BufferForLowData | BufferForHighData
GenerateFSKEncoding(BufferForLowData2, lowFreq, FrecuencyLowData2, amplitude, phaseShiftInPi,changeFreq,type_bit);
GenerateFSKEncoding(BufferForHighData2, highFreq, FrecuencyHighData2, amplitude, phaseShiftInPi,changeFreq,type_bit);
//en este punto tenemos guardado en cada buffer el tamaño de la señal en bajo y alto.
// CONTAMOS CUANTOS BIS SE ENVIARAN PARA PODER PARAR CUANDO SE TERMINE DE ENVIAR
for (int i=0; i<heading.length; ++i) {
NSString* h = [heading substringWithRange:NSMakeRange(i, 1)];
if([h isEqualToString:@"_"]) {
qSteps+=FrecuencyLowData2;
} else if([h isEqualToString:@"^"]) {
qSteps+=FrecuencyHighData2;
}
}
// CONTAMOS CUANTOS BIS SE ENVIARAN PARA PODER PARA CUANDO SE TERMINE DE ENVIAR
for (int i=0; i<tailing.length; ++i) {
NSString* t = [tailing substringWithRange:NSMakeRange(i, 1)];
if([t isEqualToString:@"_"]) {
qSteps+=FrecuencyLowData2;
} else if([t isEqualToString:@"^"]) {
qSteps+=FrecuencyHighData2;
}
}
//bits se enviaran para poder parar la ejecucion cuando terminen.
dataQueue = (double*)malloc(sizeof(double)*qSteps);
PulseToSendInSignal = dataQueue;
if([_KeyValue isEqual:@"0"]){
Power(PulseToSendInSignal, BufferForHighData2, highSize2,BufferForLowData,lowSize, FrecuencyLowData,FrecuencyHighData2);
}
if([_KeyValue isEqual:@"1"]){
ChannelUp(PulseToSendInSignal, BufferForHighData2, highSize2,BufferForLowData,lowSize, FrecuencyLowData,FrecuencyHighData2);
}
if([_KeyValue isEqual:@"2"]){
ChannelDown(PulseToSendInSignal, BufferForHighData2, highSize2,BufferForLowData,lowSize, FrecuencyLowData,FrecuencyHighData2);
}
PulseToSendInSignal = dataQueue;
[self createToneUnit];
//cargamos el tono
// Stop changing parameters on the unit
OSErr err = AudioUnitInitialize(toneUnit);
NSAssert1(err == noErr, @"Error initializing unit: %d", err);
//empezamos a emitir en el canal.
// Start playback
err = AudioOutputUnitStart(toneUnit);
NSAssert1(err == noErr, @"Error starting unit: %d", err);
}
}
void ChannelUp(double* PulseToSendInSignal, double* BufferForHighData2, double highSize2,
double* BufferForLowData, double lowSize,int FrecuencyLowData, int FrecuencyHighData2){
memcpy(PulseToSendInSignal, BufferForHighData2, highSize2);
PulseToSendInSignal+=FrecuencyHighData2;
memcpy(PulseToSendInSignal, BufferForHighData2, highSize2);
PulseToSendInSignal+=FrecuencyHighData2;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForHighData2, highSize2);
PulseToSendInSignal+=FrecuencyHighData2;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
}
void ChannelDown(double* PulseToSendInSignal, double* BufferForHighData2, double highSize2,
double* BufferForLowData, double lowSize,int FrecuencyLowData, int FrecuencyHighData2){
// BITS CHANNEL DOWN 11000000100001
memcpy(PulseToSendInSignal, BufferForHighData2, highSize2);
PulseToSendInSignal+=FrecuencyHighData2;
memcpy(PulseToSendInSignal, BufferForHighData2, highSize2);
PulseToSendInSignal+=FrecuencyHighData2;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForHighData2, highSize2);
PulseToSendInSignal+=FrecuencyHighData2;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForHighData2, highSize2);
PulseToSendInSignal+=FrecuencyHighData2;
}
void Power(double* PulseToSendInSignal, double* BufferForHighData2, double highSize2,
double* BufferForLowData, double lowSize,int FrecuencyLowData, int FrecuencyHighData2){
// BITS CHANNEL DOWN 11000000001100
// BIT 1
memcpy(PulseToSendInSignal, BufferForHighData2, highSize2);
PulseToSendInSignal+=FrecuencyHighData2;
memcpy(PulseToSendInSignal, BufferForHighData2, highSize2);
PulseToSendInSignal+=FrecuencyHighData2;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForHighData2, highSize2);
PulseToSendInSignal+=FrecuencyHighData2;
memcpy(PulseToSendInSignal, BufferForHighData2, highSize2);
PulseToSendInSignal+=FrecuencyHighData2;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
memcpy(PulseToSendInSignal, BufferForLowData, lowSize);
PulseToSendInSignal+=FrecuencyLowData;
}
/**
Procesa y guarda todos los datos ingresados en forma de ondas a traves de FSK.
@summary Calculamos cuantos bits se enviaran en total a traves de los strings - o ^ que se ingresen
@param: buf double lowData[lowSteps|highSteps] es el apuntador array ;
@param: freq Frecuencia ingresada por el usuario en campos - lowSteps o highSteps
@param: steps Contiene el periodo de la onda dado por round(sampleRate/lowFreq|highFreq) ejem. 441000/900 = 45
@return: (void)
**/
void GenerateFSKEncoding(double* buf, int freq, int steps, float amplitude, float phaseShiftInPi,bool changeFreq, int type_bit){
double theta=0;
double theta_increment = 2.0 * M_PI * freq / sampleRate;
for (int i=0; i<steps; ++i) {
double result = (sin(theta+phaseShiftInPi*M_PI) * amplitude);
if(type_bit){
// bit == 1 en philips requiere señares de tipo ___|'''''| (inversa)
// solo estara en alto 24 periodos -> 24/44100 = 0.00045ms
if (i<=40) {
result = 0;
}else{
result = 99;
}
}else{
if(changeFreq && result > 0 ){
// solo estara en alto 24 periodos -> 24/44100 = 0.00045ms
if (i>=40) {
result = 0;
}else{
result = 99;
}
}
}
buf[i] = result;
theta+=theta_increment;
if (theta > 2.0 * M_PI)
{
theta -= 2.0 * M_PI;
}
}
}
/**
Duerme Al hilo
**/
- (void)stop
{
// NSLog(@"interrumpido");
if (toneUnit)
{
//[self sendData:[self sendButton]];
float interval_ = interval;
if (interval_>0) {
// Sleep for a while. This makes it easiest to test various problematic cases.
[NSThread sleepForTimeInterval:interval/1000];
}
}
//paramos de enviar la señal, pero no matamos el thread.
[self StopSendSignal];
}
/**
Summary : Callback cuando stop de emitir la señal.
**/
void ToneInterruptionListener(void *inClientData, UInt32 inInterruptionState)
{
EncodeSignalFSK *controller = (__bridge EncodeSignalFSK*)inClientData;
[controller stop];
}
/*
ATENCION, DEFINIR ESTA CLASE, ESTA SECCION DETIENE LA EJECICION DEL SONIDO
*/
- (void)StopSendSignal {
status = 0;
AudioOutputUnitStop(toneUnit);
AudioUnitUninitialize(toneUnit);
AudioComponentInstanceDispose(toneUnit);
toneUnit = nil;
if(dataQueue != NULL){
free(dataQueue);
dataQueue = NULL;
}
}
/*
// Configure the search parameters to find the default playback output unit
// (called the kAudioUnitSubType_RemoteIO on iOS but
// kAudioUnitSubType_DefaultOutput on Mac OS X)
*/
OSStatus RenderTone(
void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
// Get the tone parameters out
EncodeSignalFSK *controller = (__bridge EncodeSignalFSK*)inRefCon;
// This is a mono tone generator so we only need the first buffer
const int channel = 0;
Float32 *buffer = (Float32 *)ioData->mBuffers[channel].mData;
//printf("Start package.\n");
// Generate the samples
for (UInt32 frame = 0; frame < inNumberFrames; frame++)
{
if(controller->status){
buffer[frame] = *(controller->PulseToSendInSignal);
++controller->PulseToSendInSignal;
// si el indicque es mayor a los Steps que tenemos que hacer entonces qu pare.
if(++controller->qIndex >= controller->qSteps){
//Invokes a method of the receiver on the main thread using the default mode.
//printf("qIndex end:%lu qSteps:%lu\n", viewController->qIndex, viewController->qSteps);
[controller performSelectorOnMainThread:@selector(stop) withObject:nil waitUntilDone:NO];
controller->status = 0;
break;
}
}
}
return noErr;
}
- (void)createToneUnit
{
AudioComponentDescription defaultOutputDescription;
defaultOutputDescription.componentType = kAudioUnitType_Output;
defaultOutputDescription.componentSubType = kAudioUnitSubType_RemoteIO;
defaultOutputDescription.componentManufacturer = kAudioUnitManufacturer_Apple;
defaultOutputDescription.componentFlags = 0;
defaultOutputDescription.componentFlagsMask = 0;
// Get the default playback output unit
AudioComponent defaultOutput = AudioComponentFindNext(NULL, &defaultOutputDescription);
NSAssert(defaultOutput, @"Can't find default output");
// Create a new unit based on this that we'll use for output
OSErr err = AudioComponentInstanceNew(defaultOutput, &toneUnit);
NSAssert1(toneUnit, @"Error creating unit: %d", err);
// Set our tone rendering function on the unit
AURenderCallbackStruct input;
input.inputProc = RenderTone;
input.inputProcRefCon = (__bridge void *)(self);
err = AudioUnitSetProperty(toneUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input,
0,
&input,
sizeof(input));
NSAssert1(err == noErr, @"Error setting callback: %d", err);
// Set the format to 32 bit, single channel, floating point, linear PCM
const int four_bytes_per_float = 4;
const int eight_bits_per_byte = 8;
AudioStreamBasicDescription streamFormat;
streamFormat.mSampleRate = sampleRate;
streamFormat.mFormatID = kAudioFormatLinearPCM;
streamFormat.mFormatFlags =
kAudioFormatFlagsNativeFloatPacked | kAudioFormatFlagIsNonInterleaved;
streamFormat.mBytesPerPacket = four_bytes_per_float;
streamFormat.mFramesPerPacket = 1;
streamFormat.mBytesPerFrame = four_bytes_per_float;
streamFormat.mChannelsPerFrame = 1;
streamFormat.mBitsPerChannel = four_bytes_per_float * eight_bits_per_byte;
err = AudioUnitSetProperty (toneUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
0,
&streamFormat,
sizeof(AudioStreamBasicDescription));
NSAssert1(err == noErr, @"Error setting stream format: %d", err);
}
@end