LTVA1 3 лет назад
Родитель
Сommit
01bbd99b07
1 измененных файлов с 69 добавлено и 23 удалено
  1. 69 23
      wav_player.c

+ 69 - 23
wav_player.c

@@ -127,42 +127,88 @@ static void app_free(WavPlayerApp* app) {
 }
 }
 
 
 // TODO: that works only with 8-bit 2ch audio
 // TODO: that works only with 8-bit 2ch audio
-static bool fill_data(WavPlayerApp* app, size_t index) {
-    uint16_t* sample_buffer_start = &app->sample_buffer[index];
-    size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
+static bool fill_data(WavPlayerApp* app, size_t index)
+{
+    if(app->num_channels == 1)
+    {
+        uint16_t* sample_buffer_start = &app->sample_buffer[index];
+        size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count_half);
+
+        for(size_t i = count; i < app->samples_count_half; i++) {
+            app->tmp_buffer[i] = 0;
+        }
+
+        //for(size_t i = 0; i < app->samples_count; i += 2)
+        for(size_t i = 0; i < app->samples_count_half; i++) //now works with mono!
+        {
+            float data = app->tmp_buffer[i];
+            data -= UINT8_MAX / 2; // to signed
+            data /= UINT8_MAX / 2; // scale -1..1
+
+            data *= app->volume; // volume
+            data = tanhf(data); // hyperbolic tangent limiter
+
+            data *= UINT8_MAX / 2; // scale -128..127
+            data += UINT8_MAX / 2; // to unsigned
 
 
-    for(size_t i = count; i < app->samples_count; i++) {
-        app->tmp_buffer[i] = 0;
+            if(data < 0) {
+                data = 0;
+            }
+
+            if(data > 255) {
+                data = 255;
+            }
+
+            //uint8_t data = app->tmp_buffer[i];
+
+            sample_buffer_start[i] = data;
+        }
+
+        wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
+
+        return count != app->samples_count_half;
     }
     }
 
 
-    for(size_t i = 0; i < app->samples_count; i += 2) 
+    if(app->num_channels == 2)
     {
     {
-        float data = app->tmp_buffer[i];
-        data -= UINT8_MAX / 2; // to signed
-        data /= UINT8_MAX / 2; // scale -1..1
+        uint16_t* sample_buffer_start = &app->sample_buffer[index];
+        size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
+
+        for(size_t i = count; i < app->samples_count; i++) {
+            app->tmp_buffer[i] = 0;
+        }
 
 
-        data *= app->volume; // volume
-        data = tanhf(data); // hyperbolic tangent limiter
+        for(size_t i = 0; i < app->samples_count; i += 2) 
+        {
+            float data = (app->tmp_buffer[i] + app->tmp_buffer[i + 1]) / 2; // (L + R) / 2
+            data -= UINT8_MAX / 2; // to signed
+            data /= UINT8_MAX / 2; // scale -1..1
 
 
-        data *= UINT8_MAX / 2; // scale -128..127
-        data += UINT8_MAX / 2; // to unsigned
+            data *= app->volume; // volume
+            data = tanhf(data); // hyperbolic tangent limiter
 
 
-        if(data < 0) {
-            data = 0;
-        }
+            data *= UINT8_MAX / 2; // scale -128..127
+            data += UINT8_MAX / 2; // to unsigned
 
 
-        if(data > 255) {
-            data = 255;
+            if(data < 0) {
+                data = 0;
+            }
+
+            if(data > 255) {
+                data = 255;
+            }
+
+            //uint8_t data = app->tmp_buffer[i];
+
+            sample_buffer_start[i / 2] = data;
         }
         }
 
 
-        //uint8_t data = app->tmp_buffer[i];
+        wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
 
 
-        sample_buffer_start[i / 2] = data;
+        return count != app->samples_count;
     }
     }
 
 
-    wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
-
-    return count != app->samples_count;
+    return true;
 }
 }
 
 
 static void ctrl_callback(WavPlayerCtrl ctrl, void* ctx) {
 static void ctrl_callback(WavPlayerCtrl ctrl, void* ctx) {