EN code二进制音频蟒蛇或C [英] encode binary to audio python or C

查看:172
本文介绍了EN code二进制音频蟒蛇或C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C或Python(Python的preferred),我将如何连接codeA二进制文件,音频,然后虽然耳机插孔输出,还我将如何去code中的音频回二进制使用从麦克风插孔输入,到目前为止,我已经学会了如何隐蔽的文本文件使用Python二进制,这将是类似于RTTY沟通。

这是为了让我可以记录数据到磁带。

 进口binasciiA =开放('/用户/凯尔/桌面/命名文件夹/ UNIX commands.txt中','R')
F =开放('/用户/凯尔/桌面/ file_test.txt','W')
C = a.read()
B =斌(INT(binascii.hexlify(C),16))
f.write(二)
f.close()


解决方案

从你的意见,你要处理由位二进制数据位,把每一位进入高或低的声音。

您还需要准确地决定什么那些高和低的声音是,多长时间为每一个声音(以及是否有一个空隙之间,依此类推)。如果你让它慢,就像每一个声音1/4秒,那么你把他们当作笔记。如果你把它非​​常快,像第二的44100分之1,你把他们当作样品。人耳无法听到的第二个44100不同的声音;相反,它在高达22050Hz的听到一个声音。

一旦你做出这些决定,有两个部分,以您的问题。

首先,你必须生成样本 - 例如流,44100每秒钟16位整数流。对于非常简单的事情,比如打在44K 16位单声道格式的原始PCM文件的一大块,或产生方波,这是微不足道的。对于更复杂的情况下,如播放MP3文件的块或合成的声音出来正弦波和过滤器,你需要一些帮助。该 audioop 模块,和其他一些在STDLIB,可以给你的基本知识;除此之外,你需要寻找的PyPI为相应的模块。

其次,你必须到样品流发送到耳机插孔。有在Python对此没有内置支持。在某些平台上,你可以通过打开一个特殊的文件,并写入它做到这一点。不过,更普遍的,你需要找到PyPI上的第三方库。

的简单模块一种特定类型的音频系统的工作。 Mac和Windows各有各的标准,而Linux有半打不同的。也有聊到更高级别的包装一些Python模块;您可能必须安装并设置了包装,但一旦你这样做,你的code将任何系统上运行。


所以,让我们通过一个非常简单的例子工作。比方说,你已经有了PortAudio设置您的系统上,并且您已经安装了 PyAudio 谈谈吧。这code将扮演的方波441Hz和220.5Hz(仅高于中央C,低C)为第二(只是因为那真的很容易)。

的不到1/4

 进口binasciiA =开放('/用户/凯尔/桌面/命名文件夹/ UNIX commands.txt中','R')
C = a.read()
B =斌(INT(binascii.hexlify(C),16))sample_stream = []
high_note =(B'\\ XFF'* 100 + B'\\ 0'* 100)* 50
low_note =(B'\\ XFF'* 50 + B'\\ 0'* 50)* 100
您在B位[2:]:
    如果位==1:
        sample_stream.extend(high_note)
    其他:
        sample_stream.extend(low_note)sample_buffer = B''。加入(sample_stream)P = pyaudio.PyAudio()
流= p.open(格式= p.get_format_from_width(8)
                频道= 1,
                率= 44100,
                输出= TRUE)
stream.write(sample_buffer)

using C or python (python preferred), How would i encode a binary file to audio that is then outputted though the headphone jack, also how would i decode the audio back to binary using input from the microphone jack, so far i have learned how to covert a text file to binary using python, It would be similar to RTTY communication.

This is so that i can record data onto a cassette tape.

import binascii

a = open('/Users/kyle/Desktop/untitled folder/unix commands.txt', 'r')
f = open('/Users/kyle/Desktop/file_test.txt', 'w')
c = a.read()
b = bin(int(binascii.hexlify(c), 16))
f.write(b)
f.close()

解决方案

From your comments, you want to process the binary data bit by bit, turning each bit into a high or low sound.

You still need to decide exactly what those high and low sounds are, and how long each one sounds for (and whether there's a gap in between, and so on). If you make it slow, like 1/4 of a second per sound, then you're treating them as notes. If you make it very fast, like 1/44100 of a second, you're treating them as samples. The human ear can't hear 44100 different sounds in a second; instead, it hears a single sound at up to 22050Hz.

Once you've made those decisions, there are two parts to your problem.

First, you have to generate a stream of samples—for example, a stream of 44100 16-bit integers for every second. For really simple things, like playing a chunk of a raw PCM file in 44k 16-bit mono format, or generating a square wave, this is trivial. For more complex cases, like playing a chunk of an MP3 file or synthesizing a sound out of sine waves and filters, you'll need some help. The audioop module, and a few others in the stdlib, can give you the basics; beyond that, you'll need to search PyPI for appropriate modules.

Second, you have to send that sample stream to the headphone jack. There's no built-in support for this in Python. On some platforms, you can do this just by opening a special file and writing to it. But, more generally, you will need to find a third-party library on PyPI.

The simpler modules work for one particular type of audio system. Mac and Windows each have their own standards, and Linux has a half dozen different ones. There are also some Python modules that talk to higher-level wrappers; you may have to install and set up the wrapper, but once you do that, your code will work on any system.


So, let's work through one really simple example. Let's say you've got PortAudio set up on your system, and you've installed PyAudio to talk to it. This code will play square waves of 441Hz and 220.5Hz (just above middle C and low C) for just under 1/4th of a second (just because that's really easy).

import binascii

a = open('/Users/kyle/Desktop/untitled folder/unix commands.txt', 'r')
c = a.read()
b = bin(int(binascii.hexlify(c), 16))

sample_stream = []
high_note = (b'\xFF'*100 + b'\0'*100) * 50
low_note = (b'\xFF'*50 + b'\0'*50) * 100
for bit in b[2:]:
    if bit == '1':
        sample_stream.extend(high_note)
    else:
        sample_stream.extend(low_note)

sample_buffer = b''.join(sample_stream)

p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(8),
                channels=1,
                rate=44100,
                output=True)
stream.write(sample_buffer)

这篇关于EN code二进制音频蟒蛇或C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆