当我浏览到我的本地磁盘,并选择音频.WAV(或任何其他音频类型)文件,并点击提交我没有得到的音频输出。
我想与分配的文件/连写的“if语句”,它告诉我的程序时所选择的文件被赋给变量“文件名”,然后重新加载程序。
我做了一些尝试,例如if语句,但未能由于缺乏编码技能。
PS我提到的“if语句”的解决方法,因为我认为这是解决我的问题,否则,我意识到,我可能是200%错误的。
如果可以,请你帮助我。 谢谢。
我的代码:
from __future__ import print_function
from gnuradio import analog
from gnuradio import audio
from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from optparse import OptionParser
class TopBlock22(gr.top_block):
def getfile(self, filename):
gr.top_block.__init__(self, "Top Block 22")
print('[TopBlock22] getfile: filename:', filename)
filename = filename.encode('utf-8')
self.blocks_file_source_0 = blocks.file_source(gr.sizeof_float*1, filename, True)
self.audio_sink = audio.sink(32000, '', True)
##################################################
# Connections
##################################################
self.connect((self.blocks_file_source_0, 0), (self.audio_sink, 0))
# -----------------------------------------------------------------------------
from flask import Flask, flash, request, redirect, jsonify, render_template_string
app = Flask(__name__)
tb = TopBlock22()
tb.start()
@app.route("/")
def index():
args_filename = request.args.get('filename', '')
return render_template_string('''<!DOCTYPE html>
<html>
<body>
<!-- Previous filename: {{ filename }} -->
<form action="getfile" method="POST" enctype="multipart/form-data">
Project file path: <input type="file" name="file"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>''', filename=args_filename)
@app.route('/getfile', methods=['GET','POST'])
def getfile():
result = request.files.get('file')
print('[/getfile] result:', result.filename)
if result:
result.save(result.filename)
tb.getfile(result.filename)
return redirect('/')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
回溯输出(没有错误:
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 138-727-058
127.0.0.1 - - [23/Aug/2019 13:36:47] "GET / HTTP/1.1" 200 -
[/getfile] result: song.wav
[TopBlock22] getfile: filename: song.wav
gr::log :INFO: audio source - Audio sink arch: alsa
127.0.0.1 - - [23/Aug/2019 13:36:52] "POST /getfile HTTP/1.1" 302 -
127.0.0.1 - - [23/Aug/2019 13:36:52] "GET / HTTP/1.1" 200 -
所有的例子不使用烧瓶更容易测试问题。
若要从文件中听到声音我要运行getfile()
之前start()
from __future__ import print_function
from gnuradio import gr
from gnuradio import audio
from gnuradio import blocks
class TopBlock22(gr.top_block):
#def __init__(self):
# gr.top_block.__init__(self, "Top Block 22")
def getfile(self, filename):
print('[TopBlock22] getfile: filename:', filename)
filename = filename.encode('utf-8')
self.blocks_file_source_0 = blocks.file_source(gr.sizeof_float*1, filename, True)
self.audio_sink = audio.sink(32000, '', True)
self.connect((self.blocks_file_source_0, 0), (self.audio_sink, 0))
# -----------------------------------------------------------------------------
tb = TopBlock22()
tb.getfile('song.wav')
tb.start()
tb.wait()
要使用getfile()
后start()
和听到声音它必须stop()
和start()
重新它。
from __future__ import print_function
from gnuradio import gr
from gnuradio import audio
from gnuradio import blocks
class TopBlock22(gr.top_block):
#def __init__(self):
# gr.top_block.__init__(self, "Top Block 22")
def getfile(self, filename):
print('[TopBlock22] getfile: filename:', filename)
filename = filename.encode('utf-8')
self.blocks_file_source_0 = blocks.file_source(gr.sizeof_float*1, filename, True)
self.audio_sink = audio.sink(32000, '', True)
self.connect((self.blocks_file_source_0, 0), (self.audio_sink, 0))
self.stop()
self.start()
# -----------------------------------------------------------------------------
tb = TopBlock22()
tb.start()
tb.getfile('song.wav')
tb.wait()
或者,它必须使用lock()
unlock()
from __future__ import print_function
from gnuradio import gr
from gnuradio import audio
from gnuradio import blocks
class TopBlock22(gr.top_block):
#def __init__(self):
# gr.top_block.__init__(self, "Top Block 22")
def getfile(self, filename):
print('[TopBlock22] getfile: filename:', filename)
filename = filename.encode('utf-8')
self.blocks_file_source_0 = blocks.file_source(gr.sizeof_float*1, filename, True)
self.audio_sink = audio.sink(32000, '', True)
self.lock()
self.connect((self.blocks_file_source_0, 0), (self.audio_sink, 0))
self.unlock()
# -----------------------------------------------------------------------------
tb = TopBlock22()
tb.start()
tb.getfile('song.wav')
tb.wait()
但是当你加载新文件,这不会断开以前的声音。 它必须remeber如果其他文件被加载并使用disconnect()
停止以前的声音。
from __future__ import print_function
from gnuradio import gr
from gnuradio import audio
from gnuradio import blocks
class TopBlock22(gr.top_block):
def __init__(self):
gr.top_block.__init__(self, "Top Block 22")
# at star file is not loaded and not connected
self.file_source_connected = False
def getfile(self, filename):
print('[TopBlock22] getfile: filename:', filename)
filename = filename.encode('utf-8')
##################################################
# Connections
##################################################
self.lock()
# disconnect previous file
if self.file_source_connected:
self.disconnect((self.blocks_file_source_0, 0), (self.audio_sink, 0))
# use new file
self.blocks_file_source_0 = blocks.file_source(gr.sizeof_float*1, filename, True)
self.audio_sink = audio.sink(32000, '', True)
self.connect((self.blocks_file_source_0, 0), (self.audio_sink, 0))
self.unlock()
# remember that file is connected
self.file_source_connected = True
# -----------------------------------------------------------------------------
import time
tb = TopBlock22()
tb.start()
tb.getfile('song.wav') # use one file
time.sleep(3)
tb.getfile('another_song.wav') # use another file later
tb.wait()