本帖最后由 shanan 于 2017-3-25 23:51 编辑
#!/bin/python
#Example source code for 21ic
#Default runs in Python 3.5 Environment
#Author: zhanzr21
#Description: This Example demonstrates how to generate raw audio data.
#
import os
import math
import random
TEST_SAMPLE_RATE = 22050
TEST_SAMPLE_LEN_SEC = 1.5
TEST_SAMPLE_NUM = int(TEST_SAMPLE_RATE * TEST_SAMPLE_LEN_SEC)
CHAN_NO = 1
AUDIO_HZ = 600
AUDIO_CYCLE = (TEST_SAMPLE_RATE/AUDIO_HZ)
PULSE_HZ = 5
PULSE_CYCLE = (TEST_SAMPLE_RATE/PULSE_HZ)
test_amp_gain = 0.75
INT16_MAX = 32767
INT16_MIN = -32768
f=open('test.raw',mode='wb')
for i in range(0, TEST_SAMPLE_NUM):
# Sine Wave
#test_sample = int(INT16_MAX * test_amp_gain * (math.sin(math.pi*2*(i%AUDIO_CYCLE)/AUDIO_CYCLE)))
# Square Wave
#test_sample = int(test_amp_gain * (INT16_MAX if ((i%AUDIO_CYCLE)>(AUDIO_CYCLE/2)) else INT16_MIN))
# Saw wave
#test_sample = int(test_amp_gain * (INT16_MIN + (i%AUDIO_CYCLE)*((INT16_MAX-INT16_MIN)/AUDIO_CYCLE)))
# Triangle wave
#test_sample = int(test_amp_gain * (INT16_MIN + (i%AUDIO_CYCLE)*(2*(INT16_MAX-INT16_MIN)/AUDIO_CYCLE)) if ((i%AUDIO_CYCLE)<(AUDIO_CYCLE/2)) else (INT16_MAX - (i%AUDIO_CYCLE)*(2*(INT16_MAX-INT16_MIN)/AUDIO_CYCLE)))
# Random Number
#test_sample = random.randint(INT16_MIN, INT16_MAX)
# Pulse Sine Wave
#test_sample = int(INT16_MAX * test_amp_gain * (math.sin(math.pi*2*(i%AUDIO_CYCLE)/AUDIO_CYCLE))) if (0==(i//PULSE_CYCLE)%2) else 0
# Ladder Wave from 21ic ID: shanan
if ((i%AUDIO_CYCLE)>(AUDIO_CYCLE*0.8)):
test_sample = int(test_amp_gain * INT16_MAX)
elif ((i%AUDIO_CYCLE)>(AUDIO_CYCLE*0.6)):
test_sample = int(test_amp_gain * INT16_MAX*0.75)
elif ((i%AUDIO_CYCLE)>(AUDIO_CYCLE*0.4)):
test_sample = int(test_amp_gain * INT16_MAX*0.5)
elif ((i%AUDIO_CYCLE)>(AUDIO_CYCLE*0.2)):
test_sample = int(test_amp_gain * INT16_MAX*0.25)
elif ((i%AUDIO_CYCLE)>0):
test_sample = 0
else:
test_sample = 0
test_ba = bytearray()
test_ba.append(test_sample&0x00ff)
test_ba.append((test_sample>>8)&0x00ff)
f.write(test_ba)
f.close()
test.rar
(200 Bytes)
generate_raw_audio.rar
(787 Bytes)
|