Python Forum
How to control frequency sampling rate my dial_tone through AJAX to flask server?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to control frequency sampling rate my dial_tone through AJAX to flask server?
#1
I am trying to control the frequency sampling rate of my dial_tone python code through a AJAX for to flask server.

The problem is I don't have a control over the sampling rate value. Meaning, when I start running the program and start sliding/dragging the round slider to change the freq samp value it doesn't have any impact! It doesn't change the 'tone' sound. The output 'tone' or audio starts only when I end the process (hit ctrl + c).

Any help would be highly appreciated.

This is the Python code:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
##################################################
# GNU Radio Python Flow Graph
# Title: Top Block 22
# Generated: Tue Jul 23 15:54:16 2019
##################################################


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
from flask import Flask, render_template, jsonify, request, redirect, url_for
from random import randint


class top_block_22(gr.top_block):

    def __init__(self):
        gr.top_block.__init__(self, "Top Block 22")

        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 32000

        ##################################################
        # Blocks
        ##################################################
        self.blocks_add_xx = blocks.add_vff(1)
        self.audio_sink = audio.sink(32000, '', True)
        self.analog_sig_source_x_1 = analog.sig_source_f(samp_rate, analog.GR_COS_WAVE, 440, 0.4, 0)
        self.analog_sig_source_x_0 = analog.sig_source_f(samp_rate, analog.GR_COS_WAVE, 350, 0.4, 0)
        self.analog_noise_source_x_0 = analog.noise_source_f(analog.GR_GAUSSIAN, 0.005, -42)

        ##################################################
        # Connections
        ##################################################
        self.connect((self.analog_noise_source_x_0, 0), (self.blocks_add_xx, 2))
        self.connect((self.analog_sig_source_x_0, 0), (self.blocks_add_xx, 0))
        self.connect((self.analog_sig_source_x_1, 0), (self.blocks_add_xx, 1))
        self.connect((self.blocks_add_xx, 0), (self.audio_sink, 0))

app = Flask(__name__)

@app.route('/')
def hex_color():
        return render_template("index.html")

@app.route('/valueofslider')
def samp_rate():
        samp_rate_val = request.args.get('samp_rate_val')
        print(samp_rate_val)
        return samp_rate_val
app.run(debug=True)

def main(top_block_cls=top_block_22, options=None):



    tb = top_block_cls()
    tb.start()
    try:
        raw_input('Press Enter to quit: ')
    except EOFError:
        pass
    tb.stop()
    tb.wait()

if __name__ == '__main__':
    main()
And this is the html script:


<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>jQuery roundSlider - JS Bin</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/roundSlider/1.3.2/roundslider.min.css" rel="stylesheet" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/roundSlider/1.3.2/roundslider.min.js"></script>
</head>
<body>
  <!-- Only html needed   -->
  <div id="slider"></div>

  <script>
    var val;
    $("#slider").roundSlider({
      radius: 215,
        min: 0,
        max: 40000,

      change: function () {
        var obj1 = $("#slider").data("roundSlider");
        val = obj1.getValue();
        value: 1
        $.getJSON('/valueofslider', {
          samp_rate_val: val
        });
      }
    });
  </script>
</body>
Reply
#2
Try this:
$("#slider").roundSlider({
  radius: 215,
  min: 0,
  max: 40000,
  change: function (args) {
    console.log(args.value);
    $.getJSON('/valueofslider', {
      samp_rate_val: args.value
    });
  }
});
I put the console.log(args.value) just for DEBUG purpose.
Reply
#3
(Jul-24-2019, 08:41 PM)gontajones Wrote: console.log(args.value

Hi gontajones,

I tried it but nothing changed! The slider does work, as before, but no sound untill I hit ctrl + c then I start to hear the tone (at sample rate of 32K)
[Image: nhBhsA.png]
Reply
#4
Hi did see PM,i don't have much time now to look at this further.
Do reference your original Thread so it's not so confusing if other look at this.

Here some point that important,when shall use slide value in other code.
See that can now use freq value in main_code,but still have to return slide_val out or get error.
from flask import Flask, render_template, jsonify,request,redirect,url_for
from random import randint

app = Flask(__name__)
@app.route('/')
def slide_func():
    return render_template("index.html")

@app.route('/valueofslider')
def slide():
    slide_val = request.args.get('slide_val')
    return main_code(slide_val)   

def main_code(slide_val):
    '''Code outside of route'''
    freq = int(slide_val) + 100
    print(freq)
    return(slide_val) # Still need to return or get TypeError   

if __name__ == '__main__':
    app.run(debug=True)
Reply
#5
(Jul-25-2019, 09:27 PM)snippsat Wrote: Here some point that important,when shall use slide value in other code.
Thank you so much for your help snippsat. I did what you told me and got a new error message that I posted here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Modify an Energy Model to account for year dependent interest rate rather than only t giovanniandrean 0 398 Oct-10-2023, 07:00 AM
Last Post: giovanniandrean
  Sampling frohr 7 2,126 Mar-22-2022, 08:21 AM
Last Post: Larz60+
  time setup for realtime plotting of serial datas at high sampling rate alice93 6 3,645 Jan-07-2022, 05:41 PM
Last Post: deanhystad
  How to take the tar backup files form remote server to local server sivareddy 0 1,871 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  Setting up multiple flask apps in the same server leoramirez 0 2,050 May-04-2021, 08:10 PM
Last Post: leoramirez
  How to Calculate CPU, Disk, Memory and Network utilization rate skvivekanand 1 1,996 Jun-16-2020, 08:53 PM
Last Post: jefsummers
  Creating a simple rate limiter. DreamingInsanity 0 1,688 May-28-2020, 11:08 AM
Last Post: DreamingInsanity
  Running Ajax multiple times on button click? CaptainCsaba 1 2,068 Mar-11-2020, 02:17 PM
Last Post: CaptainCsaba
  Stock Rate of Change (based on open and current price) Not Working firebird 1 2,335 Jul-29-2019, 07:10 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020