Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added cnlabtest/CN/A3/q2-audio/a.out
Binary file not shown.
138 changes: 138 additions & 0 deletions cnlabtest/CN/A3/q2-audio/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <portaudio.h>
#include <sndfile.h>

#define FRAMES_PER_BUFFER 1024

typedef struct
{
uint16_t formatType;
uint8_t numberOfChannels;
uint32_t sampleRate;
size_t size;
float *recordedSamples;
} AudioData;

typedef struct
{
float *snippet;
size_t size;
} AudioSnippet;

AudioData initAudioData(uint32_t sampleRate, uint16_t channels, int type)
{
AudioData data;
data.formatType = type;
data.numberOfChannels = channels;
data.sampleRate = sampleRate;
data.size = 0;
data.recordedSamples = NULL;
return data;
}

float avg(float *data, size_t length)
{
float sum = 0;
for (size_t i = 0; i < length; i++)
{
sum += fabs(*(data + i));
}
return (float) sum / length;
}

long storeFLAC(AudioData *data, const char *fileName)
{

uint8_t err = SF_ERR_NO_ERROR;
SF_INFO sfinfo =
{
.channels = data->numberOfChannels,
.samplerate = data->sampleRate,
.format = SF_FORMAT_FLAC | SF_FORMAT_PCM_16
};

SNDFILE *outfile = sf_open(fileName, SFM_WRITE, &sfinfo);
if (!outfile) return -1;

// Write the entire buffer to the file
long wr = sf_writef_float(outfile, data->recordedSamples, data->size / sizeof(float));
err = data->size - wr;

// Force write to disk and close file
sf_write_sync(outfile);
sf_close(outfile);
puts("Wrote to file!!!!");
return err;
}

int main(void)
{
PaError err = paNoError;
if((err = Pa_Initialize())) goto done;
const PaDeviceInfo *info = Pa_GetDeviceInfo(Pa_GetDefaultInputDevice());
AudioData data = initAudioData(44100, info->maxInputChannels, paFloat32);
AudioSnippet sampleBlock =
{
.snippet = NULL,
.size = FRAMES_PER_BUFFER * sizeof(float) * data.numberOfChannels
};
PaStream *stream = NULL;
sampleBlock.snippet = malloc(sampleBlock.size);
time_t talking = 0;
time_t silence = 0;
PaStreamParameters inputParameters =
{
.device = Pa_GetDefaultInputDevice(),
.channelCount = data.numberOfChannels,
.sampleFormat = data.formatType,
.suggestedLatency = info->defaultHighInputLatency,
.hostApiSpecificStreamInfo = NULL
};

if((err = Pa_OpenStream(&stream, &inputParameters, NULL, data.sampleRate, FRAMES_PER_BUFFER, paClipOff, NULL, NULL))) goto done;
if((err = Pa_StartStream(stream))) goto done;
for(int i = 0;;)
{
err = Pa_ReadStream(stream, sampleBlock.snippet, FRAMES_PER_BUFFER);
if (err) goto done;
else if(avg(sampleBlock.snippet, FRAMES_PER_BUFFER) > 0.000550 && i < 1000) // talking
{
printf("You're talking! %d\n", i);
i++;

time(&talking);
data.recordedSamples = realloc(data.recordedSamples, sampleBlock.size * i);
data.size = sampleBlock.size * i;
if (data.recordedSamples) memcpy((char*)data.recordedSamples + ((i - 1) * sampleBlock.size), sampleBlock.snippet, sampleBlock.size);
else
{
free(data.recordedSamples);
data.recordedSamples = NULL;
data.size = 0;
}
}
else //silence
{
double test = difftime(time(&silence), talking);
if (test >= 1.5 && test <= 10)
{
char buffer[100];
snprintf(buffer, 100, "file:%d.flac", i);
storeFLAC(&data, buffer);
talking = 0;
free(data.recordedSamples);
data.recordedSamples = NULL;
data.size = 0;
}
}
}

done:
free(sampleBlock.snippet);
Pa_Terminate();
return err;
}
Binary file added cnlabtest/CN/A3/q2/client
Binary file not shown.
89 changes: 89 additions & 0 deletions cnlabtest/CN/A3/q2/client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>


int main()
{
char buf[100];
int sock_fd, t;
struct sockaddr_in clientaddr;
fd_set n;
memset(&clientaddr,0,sizeof(clientaddr));
sock_fd = socket(AF_INET,SOCK_STREAM,0);
if (sock_fd==-1)
{
printf("Socket creation error\n");
return 0;
}

clientaddr.sin_family = AF_INET;
clientaddr.sin_addr.s_addr=INADDR_ANY;
clientaddr.sin_port=5005;

t = connect(sock_fd,(struct sockaddr *)&clientaddr,sizeof(clientaddr));
if (t==-1)
{
printf("Connection error\n");
return 0;
}


printf("Type your Message and press Enter\n");
while (1)
{
FD_ZERO(&n);
FD_SET(0,&n);
FD_SET(sock_fd,&n);
int ret=select(10,&n,0,0,0);
if (ret==-1)
{
printf("SELECT Error\n");
close(sock_fd);
return 0;
}
else if (FD_ISSET(0,&n))
{

fgets(buf,100,stdin);

t = send(sock_fd,buf,100,0);
if (t==-1)
{
printf("Message Send error\n");
return 0;
}
if (strncmp(buf,"bye",3)==0)
{
close(sock_fd);
return 0;
}

}
else if (FD_ISSET(sock_fd,&n))
{
t = recv(sock_fd,buf,100,0);
if (t==-1)
{
printf("Message Receive error\n");
close(sock_fd);
return 0;
}
if (strncmp(buf,"bye",3)==0)
{
close(sock_fd);
return 0;
}
printf("\nMessage from Server: %s\n",buf);
}
}
return 0;
}

//scp [remoteUserName]@[ip]:[path to file] [path where to copy in your comp.]
Binary file added cnlabtest/CN/A3/q2/server
Binary file not shown.
113 changes: 113 additions & 0 deletions cnlabtest/CN/A3/q2/server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>

int main()
{
int sock_fd, new_fd, t;
socklen_t len;
char buf[100];
fd_set n;
struct sockaddr_in serveraddr, clientaddr;

memset(&serveraddr,0,sizeof(serveraddr));
memset(&clientaddr,0,sizeof(clientaddr));

sock_fd = socket(AF_INET,SOCK_STREAM,0);
if (sock_fd==-1)
{
printf("Socket creation error\n");
return 0;
}


serveraddr.sin_family=AF_INET;
serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1");
serveraddr.sin_port=5005;

t = bind(sock_fd,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
if (t==-1)
{
printf("Bind error\n");
return 0;
}

t = listen(sock_fd,20);
if (t==-1)
{
printf("Listen error\n");
return 0;
}

len = sizeof(clientaddr);
new_fd = accept(sock_fd,(struct sockaddr *)&clientaddr, &len);
if (new_fd==-1)
{
printf("New_FD error\n");
return 0;
}
printf("Type your Message and press Enter\n");
while (1)
{
FD_ZERO(&n);
FD_SET(0,&n);
FD_SET(new_fd,&n);
int ret=select(10,&n,0,0,0);
if (ret==-1)
{
printf("SELECT Error\n");
close(new_fd);
close(sock_fd);
return 0;
}
else if (FD_ISSET(0,&n))
{
fgets(buf,100,stdin);

t = send(new_fd,buf,100,0);
if (t==-1)
{
printf("Message Send error\n");
return 0;
}
if (strncmp(buf,"bye",3)==0)
{
close(new_fd);
close(sock_fd);
return 0;
}

}
else if (FD_ISSET(new_fd,&n))
{
t = recv(new_fd,buf,100,0);
if (t==-1)
{
printf("Message Receive error\n");
close(new_fd);
close(sock_fd);
return 0;
}
if (strncmp(buf,"bye",3)==0)
{
close(new_fd);
close(sock_fd);
return 0;
}
printf("\nMessage from Client: %s\n",buf);
}
}
close(new_fd);
close(sock_fd);
return 0;
}




Loading