Merge "tinycap: Modify capture_sample()for pcm data to frames"
diff --git a/tinyplay.c b/tinyplay.c
index 461a4ef..0354df6 100644
--- a/tinyplay.c
+++ b/tinyplay.c
@@ -32,6 +32,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <signal.h>
+#include <endian.h>
 
 #define ID_RIFF 0x46464952
 #define ID_WAVE 0x45564157
@@ -62,7 +63,7 @@
 
 void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels,
                  unsigned int rate, unsigned int bits, unsigned int period_size,
-                 unsigned int period_count);
+                 unsigned int period_count, uint32_t data_sz);
 
 void stream_close(int sig)
 {
@@ -118,6 +119,7 @@
         case ID_DATA:
             /* Stop looking for chunks */
             more_chunks = 0;
+            chunk_header.sz = le32toh(chunk_header.sz);
             break;
         default:
             /* Unknown chunk, skip bytes */
@@ -153,7 +155,7 @@
     }
 
     play_sample(file, card, device, chunk_fmt.num_channels, chunk_fmt.sample_rate,
-                chunk_fmt.bits_per_sample, period_size, period_count);
+                chunk_fmt.bits_per_sample, period_size, period_count, chunk_header.sz);
 
     fclose(file);
 
@@ -210,12 +212,12 @@
 
 void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels,
                  unsigned int rate, unsigned int bits, unsigned int period_size,
-                 unsigned int period_count)
+                 unsigned int period_count, uint32_t data_sz)
 {
     struct pcm_config config;
     struct pcm *pcm;
     char *buffer;
-    int size;
+    unsigned int size, read_sz;
     int num_read;
 
     memset(&config, 0, sizeof(config));
@@ -253,20 +255,22 @@
         return;
     }
 
-    printf("Playing sample: %u ch, %u hz, %u bit\n", channels, rate, bits);
+    printf("Playing sample: %u ch, %u hz, %u bit %u bytes\n", channels, rate, bits, data_sz);
 
     /* catch ctrl-c to shutdown cleanly */
     signal(SIGINT, stream_close);
 
     do {
-        num_read = fread(buffer, 1, size, file);
+        read_sz = size < data_sz ? size : data_sz;
+        num_read = fread(buffer, 1, read_sz, file);
         if (num_read > 0) {
             if (pcm_write(pcm, buffer, num_read)) {
                 fprintf(stderr, "Error playing sample\n");
                 break;
             }
+            data_sz -= num_read;
         }
-    } while (!close && num_read > 0);
+    } while (!close && num_read > 0 && data_sz > 0);
 
     free(buffer);
     pcm_close(pcm);