Guest User

Untitled

a guest
Oct 22nd, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #ifdef HAVE_CONFIG_H
  2. #include <config.h>
  3. #endif
  4.  
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #include <fcntl.h>
  10.  
  11. #include <pulse/simple.h>
  12. #include <pulse/error.h>
  13. #include <pulse/gccmacro.h>
  14. #include <pulse/pulseaudio.h>
  15.  
  16. #define BUFSIZE 1024
  17. #define UINT32_MAX  (0xffffffff)
  18.  
  19. struct pa_simple {
  20.     pa_threaded_mainloop *mainloop;
  21.     pa_context *context;
  22.     pa_stream *stream;
  23.     pa_stream_direction_t direction;
  24.  
  25.     const void *read_data;
  26.     size_t read_index, read_length;
  27.  
  28.     int operation_success;
  29. };
  30. int indexV = -1;
  31.  
  32. static void pm_default_sink_info_callback (pa_context *c,const pa_sink_info *info,int eol,void *userdata) {
  33.     if (eol > 0)
  34.         return;
  35.     else {
  36.         indexV = info->index;
  37.         printf("#  set default sink to %d\n",indexV);
  38.     }
  39. }
  40.  
  41. static void pm_server_info_callback (pa_context *c,const pa_server_info *info,void *userdata) {
  42.     printf("#  server info \n");
  43.     pa_operation *operation;
  44.     if (info == NULL) {
  45.         printf("No PulseAudio Server\n");
  46.         return;
  47.     }
  48.     if (!(operation = pa_context_get_sink_info_by_name (c,info->default_sink_name,pm_default_sink_info_callback,userdata) )) {
  49.     }
  50. }
  51.  
  52. int setVolume(float volume){
  53.     static pa_sample_spec ss;
  54.         ss.format = PA_SAMPLE_S16LE;
  55.         ss.rate = 44100;
  56.         ss.channels = 2;
  57.     int error;
  58.     pa_simple *s = NULL;
  59.     if (!(s = pa_simple_new(NULL, "thinta", PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) {
  60.         fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
  61.         exit(-1);
  62.     }
  63.     printf("*  connected to server\n");
  64.    
  65.     printf("*  requesting server info\n");
  66.     pa_context_get_server_info (s->context, pm_server_info_callback, NULL);
  67.     printf("*  waiting for response\n");
  68.     while(indexV == -1){usleep(100);} //waiting for callback to set default-sink-index
  69.    
  70.     pa_cvolume vol;
  71.     printf("*  Linear Volume: %f\n",volume);
  72.     pa_cvolume_set(&vol, ss.channels,pa_sw_volume_from_linear(volume));
  73.    
  74.     printf("-> got default sink.. changing volume!\n");
  75.     pa_context_set_sink_volume_by_index(s->context, indexV, &vol, NULL, NULL);
  76.    
  77.     indexV = -1;
  78.     pa_simple_free(s);
  79.     return 0;
  80. }
  81.  
  82. int main(){ setVolume(0.1); }
Add Comment
Please, Sign In to add comment