Created
June 10, 2015 21:03
-
-
Save 0x5742/6dad7f33abf98f9b7346 to your computer and use it in GitHub Desktop.
make some noise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # (works on both 2.x and 3.x) | |
| import math | |
| import struct | |
| import sys | |
| import ossaudiodev as oss | |
| SAMPLE_RATE = 48000 | |
| CHANNELS = 1 | |
| def sine(pos): | |
| return (math.sin(2 * math.pi * pos / SAMPLE_RATE)) | |
| def main(): | |
| a = 0 | |
| n = 0 | |
| freq = 55 | |
| dsp = oss.open('w') | |
| dsp.setparameters(oss.AFMT_S16_NE, CHANNELS, SAMPLE_RATE, True) | |
| if sys.stdout.isatty(): | |
| print('Press Ctrl-C to stop.') | |
| cond = lambda: True | |
| else: | |
| cond = lambda: a < SAMPLE_RATE * 10 | |
| try: | |
| while cond(): | |
| data = sine(a * sine(n)) * sine(n / 3.0) | |
| dsp.write(struct.pack('h', int(32767 * data))) | |
| a += 1 | |
| n += freq | |
| except KeyboardInterrupt: | |
| pass | |
| dsp.close() | |
| if __name__ == '__main__': main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment