Advertisement
Guest User

ALC1220 bug reproduce

a guest
Nov 3rd, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <unistd.h>
  4. #include <pulse/simple.h>
  5. #include <pulse/error.h>
  6. #include <chrono>
  7.  
  8. constexpr int s_sample_rate = 48000;
  9. constexpr int s_sample_num = 256;
  10.  
  11. int main()
  12. {
  13.     printf("Running low-latency audio stream on PulseAudio Simple API...\n");
  14.    
  15.     struct pa_sample_spec ss;
  16.     ss.rate = s_sample_rate;   
  17.     ss.format = PA_SAMPLE_FLOAT32LE;
  18.     ss.channels = 2;
  19.    
  20.     struct pa_simple *pa;
  21.     pa = pa_simple_new(NULL, "pulsefail", PA_STREAM_PLAYBACK, NULL, "GAME", &ss, NULL, NULL, NULL);
  22.    
  23.     if (!pa)
  24.     {
  25.         fprintf(stderr, "pa_simple_new() failed\n");
  26.         return 1;
  27.     }  
  28.    
  29.     float src[2 * s_sample_num];
  30.     for (int i = 0; i < 2 * s_sample_num; i++)
  31.     {
  32.         src[i] = i & 128 ? -0.1f : 0.1f;   
  33.     }
  34.  
  35.     long long counter = 0;
  36.    
  37.     auto start = std::chrono::steady_clock::now();
  38.  
  39.     while (true)
  40.     {
  41.         auto _now = std::chrono::steady_clock::now();
  42.        
  43.         if (_now - start >= std::chrono::seconds{10})
  44.         {
  45.             break;
  46.         }
  47.    
  48.         auto expected_time = counter * s_sample_num * 1000000 / s_sample_rate;
  49.         auto current_time = std::chrono::duration_cast<std::chrono::microseconds>(_now - start).count();
  50.        
  51.         if (expected_time > current_time)
  52.         {
  53.             usleep(expected_time - current_time);
  54.             continue;
  55.         }
  56.        
  57.         if (pa_simple_write(pa, src, sizeof(src), NULL) < 0)
  58.         {
  59.             fprintf(stderr, "pa_simple_write() failed\n");
  60.             break;
  61.         }
  62.        
  63.         counter++;
  64.     }
  65.    
  66.     pa_simple_drain(pa, NULL);
  67.     auto end = std::chrono::steady_clock::now();
  68.     auto sec = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000000.;  
  69.     printf("Stream finished: %fs, %d/%d: %lld samples.\n", sec, s_sample_rate, s_sample_num, counter);
  70.     pa_simple_free(pa);
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement