Create dummy audio file in iPhone - iphone

I woud like to programatically create and save to disk a dummy audio file to use in unit tests involving AVAudioPlayer.
What's the simplest way to do this in iOS?

You can either record some random noise for some duration using AVAudioRecord.
Alternatively, you can create one wave file from scratch using details given in following link : http://www.iphonedevsdk.com/forum/iphone-sdk-development/45613-creating-audio-programmatically.html
Edit: link is dead. Try this https://web.archive.org/web/20120701191443/http://www.iphonedevsdk.com/forum/iphone-sdk-development/45613-creating-audio-programmatically.html
NSString *path = [[NSBundle mainBundle] pathForResource:#"WavHeader" ofType:#"wav"];
NSMutableData *audioData = [[NSMutableData alloc] initWithContentsOfFile:path];
int samples = 22050;
uint8_t dataToRecord[samples];
int x;
for(x = 0; x < samples; x++) {
// populate the "dataToRecord" array with audio data. //
}
[audioData appendBytes:(const void *)dataToRecord length:samples];
// Get path to documents folder and set a file name //
[audioData writeToFile:pathToFile atomically:YES];

A colleague suggested an alternative solution: create the smallest audio file possible with Audacitiy, get its bytes, and copy them into the code.
This is the result:
const char bytes[] = {0x52, 0x49, 0x46, 0x46, 0x26, 0x0, 0x0, 0x0, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1, 0x0, 0x44, 0xac, 0x0, 0x0, 0x88, 0x58, 0x1, 0x0, 0x2, 0x0, 0x10, 0x0, 0x64, 0x61, 0x74, 0x61, 0x2, 0x0, 0x0, 0x0, 0xfc, 0xff};
NSData* data = [NSData dataWithBytes:bytes length:sizeof(bytes)];
NSString* filePath = [path_ stringByAppendingPathComponent:#"test.wav"];
[data writeToFile:filePath atomically:YES];
audioURL_ = [NSURL fileURLWithPath:filePath];

Related

How to extract program from ".ucf" configuration file generated by unico?

In my project i am using LSM6SOX sensor to generate the interrupt for every 10 degree incrementation on X axis.
I found this link...
https://github.com/STMicroelectronics/STMems_Standard_C_drivers/blob/master/lsm6dsox_STdC/examples/lsm6dsox_fsm_raw.c
it worked well! Similar way of "/Program: glance/"
static const uint8_t lsm6so_prg_glance[] = {
0xb2, 0x10, 0x24, 0x20, 0x17, 0x17, 0x66, 0x32,
0x66, 0x3c, 0x20, 0x20, 0x02, 0x02, 0x08, 0x08,
0x00, 0x04, 0x0c, 0x00, 0xc7, 0x66, 0x33, 0x73,
0x77, 0x64, 0x88, 0x75, 0x99, 0x66, 0x33, 0x53,
0x44, 0xf5, 0x22, 0x00,
};
I am try to generate .ucf.. using this link...
https://github.com/STMicroelectronics/STMems_Finite_State_Machine/tree/master/configuration_examples/example_0_fsm
`const ucf_line_t Free_fall[] = {
{.address = 0x10, .data = 0x00,},
{.address = 0x11, .data = 0x00,},
{.address = 0x01, .data = 0x80,},
{.address = 0x04, .data = 0x00,},
{.address = 0x05, .data = 0x00,},
{.address = 0x5F, .data = 0x4B,},
{.address = 0x46, .data = 0x01,},
{.address = 0x47, .data = 0x00,},
{.address = 0x0A, .data = 0x00,},
{.address = 0x0B, .data = 0x01,},
{.address = 0x0C, .data = 0x00,},
{.address = 0x0E, .data = 0x00,},
{.address = 0x0F, .data = 0x00,},
{.address = 0x10, .data = 0x00,},
{.address = 0x17, .data = 0x40,},
{.address = 0x02, .data = 0x11,},
{.address = 0x08, .data = 0x7A,},
{.address = 0x09, .data = 0x00,},
{.address = 0x09, .data = 0x00,},
{.address = 0x09, .data = 0x01,},
{.address = 0x09, .data = 0x01,},
{.address = 0x09, .data = 0x00,},
{.address = 0x09, .data = 0x04,},
{.address = 0x02, .data = 0x41,},
{.address = 0x08, .data = 0x00,},
{.address = 0x09, .data = 0x61,},
{.address = 0x09, .data = 0x10,},
{.address = 0x09, .data = 0x1A,},
{.address = 0x09, .data = 0x00,},
{.address = 0x09, .data = 0x0F,},
{.address = 0x09, .data = 0x00,},
{.address = 0x09, .data = 0xCD,},
{.address = 0x09, .data = 0x34,},
{.address = 0x09, .data = 0xAB,},
{.address = 0x09, .data = 0x00,},
{.address = 0x09, .data = 0x00,},
{.address = 0x09, .data = 0x00,},
{.address = 0x09, .data = 0x00,},
{.address = 0x09, .data = 0x01,},
{.address = 0x09, .data = 0x00,},
{.address = 0x09, .data = 0xF5,},
{.address = 0x09, .data = 0x77,},
{.address = 0x09, .data = 0x99,},
{.address = 0x09, .data = 0x12,},
{.address = 0x09, .data = 0x66,},
{.address = 0x09, .data = 0x53,},
{.address = 0x09, .data = 0xC7,},
{.address = 0x09, .data = 0x66,},
{.address = 0x09, .data = 0x99,},
{.address = 0x09, .data = 0x50,},
{.address = 0x09, .data = 0x00,},
{.address = 0x04, .data = 0x00,},
{.address = 0x05, .data = 0x01,},
{.address = 0x17, .data = 0x00,},
{.address = 0x01, .data = 0x00,},
{.address = 0x5E, .data = 0x02,},
{.address = 0x10, .data = 0x20,},
{.address = 0x11, .data = 0x60,}
};'`
I am able to generate above file which is different from above /* Program: glance / why it is different and How to generate same way (/ Program: glance */ ) and don't get any ideal to generate interrupt at every 10 degree..
I am using STEVAL-MKI197V1
Thanks
Deepak Aagri

How to convert Hexadecimal NSData to Decimal array?

I have an array with hexadecimal value like below:
const UInt8 request_MifareID[] = {
0x00, 0xCA, 0x01, 0x0A, 0x00
};
then i transfer it to NSData for communication:
NSData *data = [[NSData alloc] initWithBytesNoCopy:(UInt8 *)request_MifareID length:5];
Sent "data" to server and get the server response:
Printing description of resp:
<ee97562d 02280400 00000000 21074400 9000>
NSUInteger len = [resp length];
Printing description of len:
(NSUInteger) len = 18
the "resp" is also a Hexadecimal format NSData
now, i have to transfer the "resp" to Decimal array
how should i do?
------------------------------------------------------Edit
Ok, first
i have to transfer the NSData which server response(it is not String, just numbers):
ee97562d 02280400 00000000 21074400 9000
into what kind of object?
Byte?
when i use
NSString * str = [[NSString alloc] initWithData: resp
encoding:NSUTF8StringEncoding];
it returns nil
int number = 0xe; // or 0x3, 0x6, 0x8, 0x2
NSString * decimalString = [NSString stringWithFormat:#"%d", number];
NSString * hexString = [NSString stringWithFormat:#"%x", number];
**NSData *data = [NSData dataWithBytes:"FF3C" length:4];
NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
string = [#"0x" stringByAppendingString:string];
unsigned short value;
sscanf([string UTF8String], "%hx", &value);
NSLog(#"%d", value)**

How to encrypt/decrypt chunks/blocks of large files using AES, CCrypt on iOS?

I am using AES 256 for Encryption Decryption, I have bigger files about 1 - 2 GB it should go through Encryption and Decryption Process , while doing encryption of downloaded files I receives the memory warning and app crashes. So I want to know how do I do encryption decryption chunk by chunk to reduce the memory usage.
Following is the code snip which I am using:-
- (NSData *)AES256EncryptWithKey:(NSString *)key
{
char keyPtr[kCCKeySizeAES256 + 1];
bzero( keyPtr, sizeof( keyPtr ) );
// fetch key data
[key getCString:keyPtr maxLength:sizeof( keyPtr ) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc( bufferSize );
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL ,
[self bytes], dataLength,
buffer, bufferSize,
&numBytesEncrypted );
if( cryptStatus == kCCSuccess )
{
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free( buffer );
return nil;
}
- (NSData *)AES256DecryptWithKey:(NSString *)key
{
char keyPtr[kCCKeySizeAES256+1];
bzero( keyPtr, sizeof( keyPtr ) );
// fetch key data
[key getCString:keyPtr maxLength:sizeof( keyPtr ) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc( bufferSize );
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt( kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL ,
[self bytes], dataLength,
buffer, bufferSize,
&numBytesDecrypted );
if( cryptStatus == kCCSuccess )
{
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free( buffer );
return nil;
}
Thanking You,
Rohit Jankar
I too came across this situation in one of my project.
Approach I have followed:
I encrypt only few bytes from the file may be only the header/first 100 bytes etc.While decrypting I decrypt only the specific number of bytes. Thus avoiding to encrypt & decrypt complete file. If the file is theft by any means it cannot be viewed as header/begin part of the file is encrypted.Though the remaining range of the file can be decrypted, it is useless & incomplete.
Moreover it depends on the type of file & client as well. I also heard one of my colleague saying that we can load bytes in chunks and encrypt them. I am not sure how exactly in terms of coding.
This is just to give you an idea of what i have done which may match your scenario as well.
TNQ
Actual reason of crash is you are loading large amount of data in memory. You are doing one shot encryption i.e CCCrypt is a oneshot encryption function. Instead of this you have to use stream or some other way like divide your data into small chunk and encrypt data chunk by chunk.
Here You can find more about implementation : Encrypt big file

iphone cbc encryption returning null

I have the following code inside the NSData+AES256 class. I am trying AES CBC encryption to a NSString with the following code. I have a key and a iv. But getting null in result. Can not find out what's wrong. This is what I tried-
NSString *initV= #"somekey*********";
NSData *input= [initV dataUsingEncoding:NSUTF8StringEncoding];
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCModeCBC,
keyPtr, kCCKeySizeAES256,
[input bytes] /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);
This is the code that worked for me finally-
-(NSData *)AES256EncryptWithKey:(NSString *)key {
NSUInteger dataLength = [self length];
NSData *keyGiven= [key dataUsingEncoding:NSUTF8StringEncoding];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
NSString *initV= #"***************";
NSData *input= [initV dataUsingEncoding:NSUTF8StringEncoding];
size_t numBytesEncrypted = 0;
CCCryptorRef ccRef;
CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES128, 0, (const void *)[keyGiven bytes], kCCKeySizeAES256, (const void *)[input bytes], &ccRef);
CCCryptorStatus cryptStatus = CCCryptorUpdate(ccRef, [self bytes], dataLength, buffer, bufferSize, &numBytesEncrypted);
CCCryptorRelease(ccRef);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer); //free the buffer;
return nil;
}
I am converting an NSString to NSData and passing it for encryption. The NSString must be of 16 characters. If it is of less than 16 characters I make it 16 by appending spaces.
I don't know if this is going to help anybody. But I just thought I should share. Thanks.

iPhone AES encryption issue

I use following code to encrypt using AES.
- (NSData*)AES256EncryptWithKey:(NSString*)key theMsg:(NSData *)myMessage {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [myMessage length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void* buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[myMessage bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess)
{
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer); //free the buffer;
return nil;
}
However the following code chunk returns null if I tried to print the encryptmessage variable. Same thing applies to decryption as well. What am I doing wrong here?
NSData *encrData = [self AES256EncryptWithKey:theKey theMsg:myMessage];
NSString *encryptmessage = [[NSString alloc] initWithData:encrData encoding:NSUTF8StringEncoding];
Thank you
try using
size_t bufferSize= dataLength + kCCBlockSizeAES256;
instead of
size_t bufferSize = dataLength + kCCBlockSizeAES128;