viz.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:waves 作者: euniceylee 项目源码 文件源码
def createViz(pin):
    src = "audio.mp3"

    questions = {23: "What's your motto?", 24: "Give us your best laugh!", 25:"Make a wish!", 8:"What are you grateful for today?"}

    audio = AudioSegment.from_file(src)
    data = np.fromstring(audio._data, np.int16)
    fs = audio.frame_rate

    BARS = 500
    BAR_HEIGHT = 300
    LINE_WIDTH = 5

    length = len(data)
    RATIO = length/BARS

    count = 0
    maximum_item = 0
    max_array = []
    highest_line = 0

    for d in data:
        if count < RATIO:
            count = count + 1

            if abs(d) > maximum_item:
                maximum_item = abs(d)
        else:
            max_array.append(maximum_item)

            if maximum_item > highest_line:
                highest_line = maximum_item

            maximum_item = 0
            count = 1

    line_ratio = highest_line/BAR_HEIGHT

    fnt = ImageFont.truetype('vcr.ttf', 50)

    im = Image.new('RGB', (BARS * LINE_WIDTH, BAR_HEIGHT), (255, 255, 255))
    draw = ImageDraw.Draw(im)
    draw.text((0,0), questions[pin] , font=fnt, fill=(0,0,0,255))

    current_x = 1
    for item in max_array:
        item_height = item/line_ratio

        current_y = (BAR_HEIGHT - item_height)/2
        draw.line((current_x, current_y, current_x, current_y + item_height), fill=(0, 0, 0), width=4)

        current_x = current_x + LINE_WIDTH

    im.show()

    filename = "audioViz_" + strftime("%Y_%m_%d%H-%M-%S", gmtime()) + ".bmp"
    im.save("static/images/" + filename)

    call(["lpr","-o","fit-to-page","static/images/" + filename])
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号