I am just a new guy in android studio.This is my code for improving gain of a audio(mp3,wav)file.I don't know how to implement in android studio.I already stored a mp3 file in raw directory,I am using text box or seek bar for getting gain value from user and after it will play the improved gain audio.Can anyone know how to do this?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef struct header_file
{
char chunk_id[4];
int chunk_size;
char format[4];
char subchunk1_id[4];
int subchunk1_size;
short int audio_format;
short int num_channels;
int sample_rate;
int byte_rate;
short int block_align;
short int bits_per_sample;
char subchunk2_id[4];
int subchunk2_size;
} header;
typedef struct header_file* header_p;
typedef float float32_t;
typedef int uint32_t;
FILE *fr, *fw;
float fs;
#define FRAME 256
int main(int argc, char *argv[] )
{
char a[100];
strcpy(a,argv[1]);
int gain=atoi(a);
fr = fopen("sweeptest.wav", "rb");
fw = fopen( "out1.wav", "wb");
header_p meta = (header_p) malloc(sizeof(header));
fread(meta, 1, sizeof(header), fr);
fwrite(meta, 1, sizeof(header), fw);
fs = meta->sample_rate;
float src[FRAME] = { 0 };
float out1[FRAME] = { 0 };
float out[FRAME] = { 0 };
short Inbuf[256] = { 0 };
short outbuf[256] = { 0 };
int i=0;
float j = gain/20;
float gain_value = pow(10,j);
while (!feof(fr)) {
fread(Inbuf, sizeof(short int), FRAME, fr);
for ( i = 0; i < FRAME; i++)
{
src[i] = (float)(Inbuf[i] / 32767.f);
out1[i] = (src[i] * gain_value);
outbuf[i] =(short)( (out1[i]* 32767.f));
}
fwrite(outbuf, sizeof(short int), FRAME, fw);
}
printf("Program finished");
return 0;
}
Related
I am fairly new to C and i have some questions. The task was to read the input.txt file and to first determine the number of lines (which i succesfully did). However the next task is to read the file line by line and to save each lines information into a struct. As you can see the task is also focused on creating as many functions as possible and that is where i am struggling right now. However i only want help with the "void readInputData" part. If someone could explain to me where my mistake is (why nothing is being printed) and maybe explain how to call the function properly in the main function that would be great. the output file wasnt part of the task i created it for myself to find some errors.
Input.txt :
1. M 17 160 13.24
2. M 18 177 13.22
3. M 15 162 14.78
4. F 16 169 15.55
5. F 16 161 14.73
6. F 16 160 10.80
7. M 14 192 15.65
8. F 18 197 12.41
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int iNumberOfLines = 1;
typedef struct {
char ignore[2];
char Gender;
int Age;
int Height;
float Time;
} Student_t;
Student_t *pStudents;
int determineNumberOfLines(FILE *pInputFile);
void readInputData(FILE *pInputFile, FILE *output, int iNumberOfLines, Student_t *pStudents);
// void calculateAverages(double *dAvHeight, double *dAvAge, double *dAvTime, Student_t *pStudents, int iNumberOfLines);
// Student_t* searchFastest(Student_t *pStudents, int iNumberOfLines);
// void printToConsole(Student_t *pFastestStudent, double dAvHeight, double dAvAge, double dAvTime);
int main(int argc, char *argv[]) {
FILE *pInputFile = fopen ("resources/Input.txt", "r");
if (pInputFile == NULL){
printf("Fehler beim Öffnen");
return -1;
}
FILE *output = fopen ("resources/Output.txt", "w");
if (output == NULL){
printf("Fehler beim Öffnen");
return -1;
}
printf("LINES: %d\n",determineNumberOfLines(pInputFile));
Student_t *pStudents;
readInputData(pInputFile, output, iNumberOfLines, pStudents);
return 0;
}
int determineNumberOfLines(FILE *pInputFile){
int ch;
while(!feof(pInputFile))
{
ch = fgetc(pInputFile);
if(ch == '\n')
{
iNumberOfLines++;
}
}
return iNumberOfLines;
fclose(pInputFile);
}
void readInputData(FILE *pInputFile, FILE *output, int iNumberOfLines, Student_t *pStudents){
int i = 0;
char buffer [120];
while (fgets(buffer, 120, pInputFile) != 0) //ließt zeile und speichert in "buffer" als string
{
if (sscanf(buffer, "%2[^.]. %c %d %d %.2f", &pStudents[i].ignore, &pStudents[i].Gender, &pStudents[i].Age, &pStudents[i].Height, &pStudents[i].Time) !=1)
{
printf("%d",&pStudents[i].Age);
fprintf(output,"%c",pStudents[i].Gender);
i++;
}
}
fclose(pInputFile);
fclose(output);
}
Thanks in advance !
UPDATE !!!
I am now having trouble with the next function
void calculateAverages(double *dAvHeight, double *dAvAge, double *dAvTime, Student_t *pStudents, int iNumberOfLines);
I can only define dAvHeight dAvAge dAvTime as Ints, however later on i need to change them to float to calculate averages. Can anyone explain how i do that while keep using dAvHeight dAvAge dAvTime.
Thanks again
Here is the updated code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int iNumberOfLines = 1;
int dAvHeight = 0, dAvAge = 0, dAvTime = 0;
typedef struct {
int ignore;
char Gender;
int Age;
int Height;
float Time;
} Student_t;
int determineNumberOfLines(FILE *pInputFile);
void readInputData(FILE *pInputFile, int iNumberOfLines, Student_t *pStudents);
void calculateAverages(double *dAvHeight, double *dAvAge, double *dAvTime, Student_t *pStudents, int iNumberOfLines);
// Student_t* searchFastest(Student_t *pStudents, int iNumberOfLines);
// void printToConsole(Student_t *pFastestStudent, double dAvHeight, double dAvAge, double dAvTime);
int main(int argc, char *argv[]) {
FILE *pInputFile = fopen ("resources/Input.txt", "r");
if (pInputFile == NULL){
printf("Fehler beim Öffnen");
return -1;
}
determineNumberOfLines(pInputFile);
//printf("LINES: %d\n",determineNumberOfLines(pInputFile));
Student_t pStudents[iNumberOfLines];
//printf("%d", iNumberOfLines);
readInputData(pInputFile, iNumberOfLines, pStudents);
calculateAverages(dAvHeight, dAvAge, dAvTime, pStudents, iNumberOfLines);
return 0;
}
int determineNumberOfLines(FILE *pInputFile){
fopen ("resources/Input.txt", "r");
int ch;
while(!feof(pInputFile))
{
ch = fgetc(pInputFile);
if(ch == '\n')
{
iNumberOfLines++;
}
}
fclose(pInputFile);
return iNumberOfLines;
}
void readInputData(FILE *pInputFile, int iNumberOfLines, Student_t *pStudents){
fopen ("resources/Input.txt", "r");
int i = 0;
char buffer [120];
while (fgets(buffer, sizeof buffer, pInputFile) != 0)
{
if (sscanf(buffer, "%d. %c %d %d %f", &pStudents[i].ignore, &pStudents[i].Gender, &pStudents[i].Age, &pStudents[i].Height, &pStudents[i].Time) <= sizeof buffer)
{
i++;
}
}
fclose(pInputFile);
}
void calculateAverages(double *dAvHeight, double *dAvAge, double *dAvTime, Student_t *pStudents, int iNumberOfLines){
int i;
int sumHeight = 0;
int sumAge = 0;
float sumTime = 0;
for (i = 0; i <= (iNumberOfLines-1); i++){
sumHeight = sumHeight + pStudents[i].Height;
}
dAvHeight = sumHeight/ iNumberOfLines;
printf("Average Height is = %.2f \n", dAvHeight);
for (i = 0; i<= (iNumberOfLines-1); i++){
sumAge = sumAge + pStudents[i].Age;
}
//dAvAge = sumAge/iNumberOfLines;
printf("Average Age is = %.2f \n", dAvAge);
for (i = 0; i<= (iNumberOfLines-1); i++){
sumTime = sumTime + pStudents[i].Time;
}
//dAvTime = sumTime/iNumberOfLines;
printf("Average Time is = %.2f \n", dAvTime);
}
nothing is being printed from the readInputData Function
You try to read something into memory pointed to by the uninitialized Student_t *pStudents. After you correctly determined iNumberOfLines, you can define Student_t pStudents[iNumberOfLines]; instead.
I'm trying to encrypt/decrypt an AES key/iv using RSA encryption algorithm using openssl in C.
The decryption is working before storing the encrypted data into the file . But the decryption threw an error while decrypting the same encrypted data stored on the file.
Here is my code:
#include <stdio.h>
#include <stdbool.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <string.h>
int padding = RSA_PKCS1_PADDING;
#define RSA_KEY_Size 384;
#define AES_256_KEY_SIZE 32;
// testing encryption/decryption
int func(const char* pubkeyfile, unsigned char *key, unsigned char *iv)
{
unsigned char *encryptedkey = (unsigned char*)malloc(RSA_KEY_Size);
unsigned char *encryptediv = (unsigned char*)malloc(RSA_KEY_Size);
unsigned char *decryptedkey = (unsigned char*)malloc(AES_256_KEY_SIZE);
unsigned char *decryptediv = (unsigned char*)malloc(AES_256_KEY_SIZE);
int result1 = rsaEncrypt(key, pubkeyfile, encryptedkey);
int result2 = rsaEncrypt(iv, pubkeyfile, encryptediv);
serialize(encryptedkey, encryptediv);
///////tested here, this part working fine
int r1 = rsaDecrypt(encryptedkey, privkeyfile, decryptedkey);
int r2 = rsaDecrypt(encryptediv, privkeyfile, decryptediv);
///////////////////////////////
unsigned char *getkey = (unsigned char*)malloc(RSA_KEY_Size);;
unsigned char *getiv = (unsigned char*)malloc(RSA_KEY_Size);;
deserialize(getkey, getiv);
unsigned char *ikey = (unsigned char*)malloc(AES_256_KEY_SIZE);
unsigned char *iiv = (unsigned char*)malloc(AES_256_KEY_SIZE);
//////////tested here, failed to decrypt after taking encrypted data from a file/////////////////
int r22 = rsaDecrypt(getiv, privkeyfile, iiv);
int r21 = rsaDecrypt(getkey, privkeyfile, ikey);
//////////////////////////////////////////////////
return 0;
}
typedef struct item {
uint8_t keyivlen;
char keyiv[RSA_KEY_Size];
struct item *next;
} list;
int serialize(unsigned char* key, unsigned char* iv)
{
list *ptr;
char *buffer;
int listLength;
list first, second;
ptr = &first;
FILE *filePtr;
memcpy(first.keyiv, key, strlen(key));
first.keyivlen = strlen(first.keyiv);
first.next = &second;
memcpy(second.keyiv, iv, strlen(iv));
second.keyivlen = strlen(second.keyiv);
second.next = 0;
listLength = listSize(ptr);
buffer = (char *)malloc(listLength);
serializeList(ptr, buffer);
filePtr = fopen("example.data", "wb+");
fwrite(buffer, listLength, 1, filePtr);
fclose(filePtr);
free(buffer);
return 0;
}
int deserialize(unsigned char* key, unsigned char* iv)
{
FILE *filePtr;
int listLength = 0;
int done = 0;
uint8_t arrayLen;
unsigned char *buffer;
int i = 0;
listLength = fileSize("example.data");
filePtr = fopen("example.data", "rb");
while (done < listLength) {
fread(&arrayLen, 1, 1, filePtr);
buffer = (unsigned char *)malloc(arrayLen + 1);
fread(buffer, arrayLen, 1, filePtr);
buffer[arrayLen] = '\0';
if (i == 0)
{
memcpy(key, buffer, arrayLen + 1);
}
else
{
memcpy(iv, buffer, arrayLen + 1);
}
//addToList(arrayLen, buffer);
done += arrayLen + 1;
i++;
free(buffer);
}
//printList(start);
return 0;
}
Here is the error:
error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error
error:04065072:rsa routines:rsa_ossl_private_decrypt:padding check failed
And it fails in:
int r22 = rsaDecrypt(getiv, privkeyfile, iiv);
int r21 = rsaDecrypt(getkey, privkeyfile, ikey);
What could be the reason for this error?
arrayLen is only uint8_t which fits at most 255 , so your deserialised data is too short. You need RSA_KEY_Size bytes.
I am developing a OS X Swift app for parsing cvs files. It runs successfully in Objective-C. Then I changed to Swift and for performance improvements I developed the parse/import engine in C. It is 5 times faster as in Swift or Objective-C - nice. But I have trouble to exchange the data between C and Swift - especially with Struct:
BridgingHeader:
#include "ToolBoxC.h"
ToolBoxC.h:
void loadFile(const char *fileName, const char *delimiters, const char *xRegex, int xRegexColumn, int xColumn, int yColumn, int xRow, int yRowShift, bool collectStrings);
typedef struct {
char **headerArray;
int numberHeaderRows;
char **dateArray;
int numberDateRows;
int **valueArray;
char ***stringArray;
int numberValueRows;
int numberValueColums;
} FileStruct;
typedef struct {
FileStruct fileContent[10000];
} FilesStruct;
struct FilesStruct filesContent;
ToolBoxC.c:
struct FileStruct {
char **headerArray;
int numberHeaderRows;
char **dateArray;
int numberDateRows;
int **valueArray;
char ***stringArray;
int numberValueRows;
int numberValueColums;
};
struct FilesStruct {
struct FileStruct fileContent[10000];
};
void loadFile(const char *fileName, const char *delimiters, const char *xRegex, int xRegexColumn, int xColumn, int yColumn, int xRow, int yRowShift, bool collectStrings) {
// some stuff
struct FileStruct fileContent;
fileContent.headerArray = headerArray;
fileContent.numberHeaderRows = numberHeaderRows;
fileContent.dateArray = dateArray;
fileContent.numberDateRows = numberDateRows;
fileContent.valueArray = valueArray;
fileContent.stringArray = stringArray;
fileContent.numberValueRows = numberValueRows;
fileContent.numberValueColums = numberValueColumns;
filesContent.fileContent[numberFiles] = fileContent;
return;
}
All the parsed data are stored in struct FilesStruct filesContent. The parsing is started by calling the function loadFile() with parameters from Swift. That works fine. Also the parsing is OK. But how can I access to the data in struct FilesStruct filesContent from Swift?
Thanks, Matthias.
Try this:
ToolBoxC.h
#include <stdbool.h>
struct FileStruct {
char **headerArray;
int numberHeaderRows;
char **dateArray;
int numberDateRows;
int **valueArray;
char ***stringArray;
int numberValueRows;
int numberValueColums;
};
extern struct FileStruct **loadedFiles;
void loadFile(const char *fileName, const char *delimiters, const char *xRegex, int xRegexColumn, int xColumn, int yColumn, int xRow, int yRowShift, bool collectStrings);
ToolBoxC.c
#include <stdlib.h>
#include "ToolBoxC.h"
#define MaxFiles 10000
struct FileStruct **loadedFiles;
void loadFile(const char *fileName, const char *delimiters, const char *xRegex, int xRegexColumn, int xColumn, int yColumn, int xRow, int yRowShift, bool collectStrings) {
static int nextIndex = 0;
if (loadedFiles == 0)
loadedFiles = malloc(MaxFiles * sizeof(*loadedFiles));
struct FileStruct *file = malloc(sizeof(struct FileStruct));
file->numberDateRows = xRow;
loadedFiles[nextIndex++] = file;
}
Swift Test Method
func loadFilesTest() -> Void {
for var i:Int32 = 0; i < 10; ++i {
loadFile("", "", "", 0, 0, 0, i, 0, true)
}
for var j = 0; j < 10; ++j {
let pointer = UnsafePointer<FileStruct>(loadedFiles[j])
print("Number of date rows = \(pointer.memory.numberDateRows)")
}
}
I have to write a function that will read an array of structures of type Product with data from a binary file.This file contains the number of products - nr and a number of articles of type Product. What's wrong? Thank you in advance!
#define SIZE 30
typedef struc{
int id;
char[SIZE] name;
float price;
}Product;
void create(Product *p, FILE *fptr)
{
p = malloc(sizeof(Product));
fread(p, 1, sizeof(Product), fptr);
}
int main(int argc, char* argv[])
{
FILE *fptr = fopen(argv[1],"rb");
Product *p;
create(p, fptr);
return 0;
}
You have to modify it to something like this:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 30
typedef struct{
int id;
char name[SIZE];
float price;
}Product;
int readproducts(Product *p, FILE *fptr, int nr)
{
if(nr != fread(p, sizeof(Product), nr, fptr))
return -1;
return 0;
}
int main(int argc, char* argv[])
{
FILE *fptr = fopen(argv[1],"rb");
int nr = 0;
if(NULL == fptr) return -1;
// First read number of products from file
// Assuming this number is written as 4 byte integer - at the start of file
if(fread(&nr, 4, 1, fptr) != 1)
return -1;
// Now, read the products
Product *p = malloc(nr * sizeof(Product));
if(-1 == readproducts(p, fptr, nr))
return -1;
fclose(fptr);
return 0;
}
The way you had used malloc in your function was wrong, see here why.
PS. That said, binary writing/reading might not be portable across different computers.
C program that reads a disk image specified as a command
line argument and prints the paths for all files and directories in the
image.If the entry is a directory, print a forward slash at the end
of the path....This is my question in program and i done code as below....
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#define K 1024
typedef struct dir {
unsigned int my_node;
unsigned short my_length;
unsigned char my_namelen;
}DIRPATH;
unsigned int getint(int fd, int block, int byte);
unsigned short int getshort(int fd, int block, int byte);
int main (int argc , char *argv[])
{
int i;
int nameleng;
char name[K];
DIRPATH dh;
int argument;
int fd;
argument = (argc > 1) ? atoi(argv[1]) : 0;
fd=open(argv[1],O_RDONLY);
fprintf(stderr,"fd=%d\n",fd);
if(lseek(fd,K,SEEK_SET)<0)
{
fprintf(stderr,"unable to seek");
exit(0);
}
int itab = getint(fd,2,8);
unsigned int first_block = getint(fd,itab,inode_size + 40);
int length=0;
for(i=0;i<6;i++)
{
lseek(fd,first_block * K + length,SEEK_SET);
read(fd,&dh,sizeof(DIRPATH));
length += dh.my_length;
printf("",dh.my_namelen);
read(fd,name,dh.my_namelen);
name[dh.my_namelen] = 0;
printf("%s\n",name);
}
close(fd);
}
unsigned int getint(int fd, int block, int byte)
{
unsigned int x;
int check;
lseek(fd,block * K + byte, SEEK_SET);
check = read(fd, &x, 4);
if(check != 4){
fprintf(stderr,"error to read %d %d \n",block, byte);
exit(0);
}
return(x);
}
unsigned short int getshort(int fd, int block, int byte)
{
unsigned short int x;
int check;
lseek(fd,block * K + byte, SEEK_SET);
check = read(fd, &x, 4);
if(check != 2){
fprintf(stderr,"error to read %d %d \n",block, byte);
exit(0);
}
return(x);
}
I am getting the output but in output it is giving . and ... I have to remove those dots and add / to directories not files.