Guest User

Untitled

a guest
Mar 8th, 2012
161
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Imports (Global)
  5. from ctypes import *
  6. from os import system
  7. from sys import platform, version_info
  8. from signal import signal, SIGINT, SIGTERM
  9. from time import sleep
  10.  
  11. # --------------------------------------------------
  12. # Test for python 3.x
  13. if (version_info >= (3,0)):
  14. PYTHON3 = True
  15. else:
  16. PYTHON3 = False
  17.  
  18. # --------------------------------------------------
  19. # Global loop check
  20. global doRun
  21. doRun = True
  22.  
  23. # --------------------------------------------------
  24. # Global JACK variables
  25. sample_rate = 44100
  26. buffer_size = 1024
  27.  
  28. # --------------------------------------------------
  29. # JACK ctypes implementation
  30. jacklib = cdll.LoadLibrary("libjack.so.0")
  31.  
  32. class jack_client_t(Structure):
  33. _fields_ = []
  34.  
  35. jack_nframes_t = c_uint32
  36. JackBufferSizeCallback = CFUNCTYPE(c_int, jack_nframes_t, c_void_p)
  37.  
  38. def jack_client_open(client_name, options, status):
  39. if (PYTHON3): client_name = client_name.encode("ascii")
  40. jacklib.jack_client_open.argtypes = [c_char_p, c_int, POINTER(c_int)]
  41. jacklib.jack_client_open.restype = POINTER(jack_client_t)
  42. return jacklib.jack_client_open(client_name, options, status)
  43.  
  44. def jack_client_close(client):
  45. jacklib.jack_client_close.argtypes = [POINTER(jack_client_t)]
  46. jacklib.jack_client_close.restype = c_int
  47. return jacklib.jack_client_close(client)
  48.  
  49. def jack_activate(client):
  50. jacklib.jack_activate.argtypes = [POINTER(jack_client_t)]
  51. jacklib.jack_activate.restype = c_int
  52. return jacklib.jack_activate(client)
  53.  
  54. def jack_deactivate(client):
  55. jacklib.jack_deactivate.argtypes = [POINTER(jack_client_t)]
  56. jacklib.jack_deactivate.restype = c_int
  57. return jacklib.jack_deactivate(client)
  58.  
  59. def jack_connect(client, source_port, destination_port):
  60. if (PYTHON3): source_port = source_port.encode("ascii")
  61. if (PYTHON3): destination_port = destination_port.encode("ascii")
  62. jacklib.jack_connect.argtypes = [POINTER(jack_client_t), c_char_p, c_char_p]
  63. jacklib.jack_connect.restype = c_int
  64. return jacklib.jack_connect(client, source_port, destination_port)
  65.  
  66. def jack_get_sample_rate(client):
  67. jacklib.jack_get_sample_rate.argtypes = [POINTER(jack_client_t)]
  68. jacklib.jack_get_sample_rate.restype = jack_nframes_t
  69. return jacklib.jack_get_sample_rate(client)
  70.  
  71. def jack_get_buffer_size(client):
  72. jacklib.jack_get_buffer_size.argtypes = [POINTER(jack_client_t)]
  73. jacklib.jack_get_buffer_size.restype = jack_nframes_t
  74. return jacklib.jack_get_buffer_size(client)
  75.  
  76. def jack_set_buffer_size_callback(client, bufsize_callback, arg):
  77. global _bufsize_callback
  78. _bufsize_callback = JackBufferSizeCallback(bufsize_callback)
  79. jacklib.jack_set_buffer_size_callback.argtypes = [POINTER(jack_client_t), JackBufferSizeCallback, c_void_p]
  80. jacklib.jack_set_buffer_size_callback.restype = c_int
  81. return jacklib.jack_set_buffer_size_callback(client, _bufsize_callback, arg)
  82.  
  83. # --------------------------------------------------
  84. # quit on SIGINT or SIGTERM
  85. def signal_handler(sig, frame=0):
  86. global doRun
  87. doRun = False
  88.  
  89. # --------------------------------------------------
  90. # run alsa_in and alsa_out
  91. def run_alsa_bridge(x_sample_rate, x_buffer_size):
  92. system("killall alsa_in alsa_out pulseaudio")
  93. system("env JACK_SAMPLE_RATE=%i JACK_PERIOD_SIZE=%i alsa_in -j alsa_in -dcloop -q 1 2>&1 1> /dev/null &" % (x_sample_rate, x_buffer_size))
  94. system("env JACK_SAMPLE_RATE=%i JACK_PERIOD_SIZE=%i alsa_out -j alsa_out -dploop -q 1 2>&1 1> /dev/null &" % (x_sample_rate, x_buffer_size))
  95.  
  96. # --------------------------------------------------
  97. # listen to jack buffer size changes
  98. def buffer_size_callback(new_buffer_size, arg):
  99. buffer_size = new_buffer_size
  100. run_alsa_bridge(sample_rate, new_buffer_size)
  101. return 0
  102.  
  103. #--------------- main ------------------
  104. if __name__ == '__main__':
  105.  
  106. # Init JACK client
  107. client = jack_client_open("jack-aloop-daemon", 0, None)
  108.  
  109. if (not client):
  110. quit()
  111.  
  112. jack_set_buffer_size_callback(client, buffer_size_callback, None)
  113. jack_activate(client)
  114.  
  115. # Quit when requested
  116. signal(SIGINT, signal_handler)
  117. signal(SIGTERM, signal_handler)
  118.  
  119. # Get initial values
  120. sample_rate = jack_get_sample_rate(client)
  121. buffer_size = jack_get_buffer_size(client)
  122.  
  123. # Activate alsa_in/out
  124. run_alsa_bridge(sample_rate, buffer_size)
  125.  
  126. # Pause it for a bit, and connect to the proper system ports
  127. sleep(1)
  128. jack_connect(client, "alsa_in:capture_1", "system:playback_1")
  129. jack_connect(client, "alsa_in:capture_2", "system:playback_2")
  130. jack_connect(client, "system:capture_1", "alsa_out:playback_1")
  131. jack_connect(client, "system:capture_2", "alsa_out:playback_2")
  132.  
  133. # Keep running until told otherwise
  134. while (doRun):
  135. sleep(1)
  136.  
  137. # Close JACK client
  138. jack_deactivate(client)
  139. jack_client_close(client)
RAW Paste Data