airtime-test-soundcard.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import subprocess
  2. import os
  3. import pwd
  4. import grp
  5. import sys
  6. import getopt
  7. """
  8. we need to run the program as non-root because Liquidsoap refuses to run as root.
  9. It is possible to run change the effective user id (seteuid) before calling Liquidsoap
  10. but this introduces other problems (fake root user is not part of audio group after calling seteuid)
  11. """
  12. if os.geteuid() == 0:
  13. print "Please run this program as non-root"
  14. sys.exit(1)
  15. def printUsage():
  16. print "airtime-test-soundcard [-v] [-o alsa | ao | oss | portaudio | pulseaudio ] [-h]"
  17. print " Where: "
  18. print " -v verbose mode"
  19. print " -o Linux Sound API (default: alsa)"
  20. print " -h show help menu "
  21. def find_liquidsoap_binary():
  22. """
  23. Starting with Airtime 2.0, we don't know the exact location of the Liquidsoap
  24. binary because it may have been installed through a debian package. Let's find
  25. the location of this binary.
  26. """
  27. rv = subprocess.call("which airtime-liquidsoap > /dev/null", shell=True)
  28. if rv == 0:
  29. return "airtime-liquidsoap"
  30. return None
  31. try:
  32. optlist, args = getopt.getopt(sys.argv[1:], 'hvo:')
  33. except getopt.GetoptError, g:
  34. printUsage()
  35. sys.exit(1)
  36. sound_api_types = set(["alsa", "ao", "oss", "portaudio", "pulseaudio"])
  37. verbose = False
  38. sound_api = "alsa"
  39. for o, a in optlist:
  40. if "-v" == o:
  41. verbose = True
  42. if "-o" == o:
  43. if a.lower() in sound_api_types:
  44. sound_api = a.lower()
  45. else:
  46. print "Unknown sound api type\n"
  47. printUsage()
  48. sys.exit(1)
  49. if "-h" == o and len(optlist) == 1:
  50. printUsage()
  51. sys.exit(0)
  52. try:
  53. print "Sound API: %s" % sound_api
  54. print "Outputting to soundcard. You should be able to hear a monotonous tone. Press ctrl-c to quit."
  55. liquidsoap_exe = find_liquidsoap_binary()
  56. if liquidsoap_exe is None:
  57. raise Exception("Liquidsoap not found!")
  58. command = "%s 'output.%s(sine())'" % (liquidsoap_exe, sound_api)
  59. if not verbose:
  60. command += " > /dev/null"
  61. #print command
  62. rv = subprocess.call(command, shell=True)
  63. #if we reach this point, it means that our subprocess exited without the user
  64. #doing a keyboard interrupt. This means there was a problem outputting to the
  65. #soundcard. Print appropriate message.
  66. print "There was an error using the selected sound API. Please select a different API " + \
  67. "and run this program again. Use the -h option for help"
  68. except KeyboardInterrupt, ki:
  69. print "\nExiting"
  70. except Exception, e:
  71. raise