Advertisement
Guest User

SailfishOS libaudioresource<->python bridge

a guest
Feb 7th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. from ctypes import *
  2. import time
  3.  
  4. a = cdll.LoadLibrary("/usr/lib/libaudioresource.so.1")
  5. StatCB = CFUNCTYPE(None, c_void_p, c_bool, c_void_p)
  6. init = a.audioresource_init
  7. init.argtypes = [c_int, StatCB, c_void_p]
  8. init.restype = c_void_p
  9.  
  10. CALLBACK = None
  11. AUDIO_RESOURCE_MEDIA = 2
  12.  
  13. inited = False
  14. is_acquired = False
  15. resource = None
  16.  
  17. def _callback(resource, acquired, data):
  18.     global is_acquired
  19.     CALLBACK(acquired)
  20.     if acquired:
  21.         is_acquired = True
  22.  
  23. callbackref = StatCB(_callback)
  24.  
  25.  
  26. def acquire(callback=None):
  27.     global CALLBACK
  28.     global resource
  29.     global inited
  30.     if not inited:
  31.         if not callable(callback):
  32.             raise TypeError(str(type(callback)) + " is not callable")
  33.         CALLBACK = callback
  34.         resource = init (AUDIO_RESOURCE_MEDIA, callbackref, None)
  35.         inited = True
  36.     a.audioresource_acquire(resource)
  37.     while not is_acquired:
  38.         a.g_main_context_iteration(None, False)
  39.         time.sleep(0.01)
  40.  
  41. def release():
  42.     a.audioresource_release(resource)
  43.     is_acquired = False
  44.     while is_acquired:
  45.         a.g_main_context_iteration(None, False)
  46.         time.sleep(0.01)
  47.  
  48. def free():
  49.     if is_acquired:
  50.         release()
  51.     a.audioresource_free(resource)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement