Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NebiyouTen committed Mar 20, 2019
1 parent b425830 commit 051483e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
Binary file modified PCM_Wav_Play
Binary file not shown.
33 changes: 27 additions & 6 deletions PCM_Wav_Play.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include "wave_play.h"

#define SECOND 1000000
#define file_name "sample_wav.wav"

#define file_name "test.wav"
#define SAMPLE_RATE 8000

/*
* @main program. It opens a PCM devices, initializes paramters and
Expand All @@ -30,16 +30,32 @@ int main(){
// Structure used to configure the PCM device
snd_pcm_hw_params_t *params;
// name of the device, default for now
char * device_name = "default";
char * device_name = "plug:default";
// Variable to store return values of functions
int return_val;
// frames used to determine size of a period
snd_pcm_uframes_t frames;
// Buffer to store data to be captured or played
char *buffer;
//char * file_name = "sample_wav.wav";
unsigned int size, sample_rate, period_time, loop_time, num_loops;


// Get list of devie names
char **hints;
return_val = snd_device_name_hint(-1, "pcm", (void***)&hints);
char ** n = hints;
while (*n != NULL) {

char *name = snd_device_name_get_hint(*n, "NAME");
printf("Devide found : %s \n", name);
if (name != NULL && 0 != strcmp("null", name)) {
//Copy name to another buffer and then free it
free(name);
}
n++;
}//End of while
// end list of devie name retrival

// open PCM device
if ( (return_val = snd_pcm_open(&handle, device_name,
SND_PCM_STREAM_PLAYBACK, 0)) < 0){
Expand All @@ -48,9 +64,11 @@ int main(){
, snd_strerror(return_val));
exit(1);
}

fprintf(stdout, "PCM Device \"%s\" successfully opened\n" \
, device_name );


// Allocate params
snd_pcm_hw_params_alloca(&params);
// Initialize with default values
Expand Down Expand Up @@ -80,13 +98,16 @@ int main(){
}
// sample rate of sample wav file is 256000 bits/second
// Invalid argument error if Sample rate is higher than 200,000
sample_rate = 8000;
sample_rate = SAMPLE_RATE;
if (snd_pcm_hw_params_set_rate_near(handle, params,
&sample_rate, 0) < 0){
fprintf(stderr,
"unable to set sample rate to %d ",sample_rate);
exit(1);
}
}
snd_pcm_hw_params_set_rate(handle, params,
sample_rate, 0);

// set period size to 32 frames. A frame has 2 samples, left and
// right samples. A sample has 2 bytes, most and least significant.
frames = 64;
Expand Down
Binary file added test.wav
Binary file not shown.
12 changes: 6 additions & 6 deletions wave_play.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int read_wave_and_play(char *file_name, snd_pcm_t *handle,
char * data_temp_buffer;


fprintf(stdout,"Wave file reading started \n");
fprintf(stdout,"Wave file reading started \n\n\n");

if ( (file_ptr = fopen(file_name, "rb") ) == NULL){
printf("error in opening file");
Expand All @@ -65,15 +65,15 @@ int read_wave_and_play(char *file_name, snd_pcm_t *handle,

// read header data
ret = fread(wave_header.riff, sizeof(wave_header.riff),1,file_ptr);
printf("RIFF header values %s .\n",wave_header.riff);
printf("RIFF header values %s.\n",wave_header.riff);
// read file size
ret = fread(temp_buffer, sizeof(temp_buffer), 1, file_ptr);
wave_header.file_size = temp_buffer[0] | (temp_buffer[1] << 8) | \
(temp_buffer[2] << 16) | (temp_buffer[3] << 24);
printf("Wav file size %u KB.\n",wave_header.file_size / 1024);
printf("Wav file size %u KB.\n",wave_header.file_size );
// get wav header
ret = fread(wave_header.wave, sizeof(wave_header.wave),1, file_ptr);
printf("Wave marker is %s .\n",wave_header.wave);
printf("Wave marker is %s.\n",wave_header.wave);
// fmt marker
ret = fread(wave_header.fmt_chunk_marker, \
sizeof(wave_header.fmt_chunk_marker), 1, file_ptr);
Expand Down Expand Up @@ -162,8 +162,8 @@ int read_wave_and_play(char *file_name, snd_pcm_t *handle,
for (i=0; i< number_of_samples; i++){

ret = fread(data_temp_buffer, NUM_BYTES ,frames, file_ptr);
printf("Reading sample %d of %d %lu samples \n",i,\
number_of_samples , sizeof(data_temp_buffer));
//printf("Reading sample %d/%d\n",i,\
// number_of_samples );
if (ret == 0) {
fprintf(stderr, "file reading error \n");
break;
Expand Down

0 comments on commit 051483e

Please sign in to comment.