0

I have got the following function which generates a tone and writes it to a wav file

import struct
import numpy as np
from scipy import signal as sg
import wave
import math

# Parameters
sampleRate = 44100
frequency = 400
duration = 1


# define wave file
file = wave.open('test.wav', 'w')
file.setnchannels(1)
file.setsampwidth(2)
file.setframerate(sampleRate)



def generate(output, freq, dur):
    # create sine wave
    n_cycles = math.floor(freq*dur)  # number of cycle
    dur2 = n_cycles /freq            # adjusted duration
    samples = dur2*sampleRate
    x = np.arange(samples+1)         # want inclusive range
    sineWave = (32767.0*np.sin(2 * np.pi * freq * x / sampleRate))

    for i in sineWave:
        value = i
        data = struct.pack('<h', int(value))
        output.writeframesraw(data);


for x in range(1):
    generate(file, 1153.1, 1.0)


file.close();

Now I want to stream it with flask.

What I want to do is playing random "Beep" Sounds which are generated from my python script. I also want to give the user the option to start and stop the Stream.

How can I do that? Is it possible with Flask?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Luxaaa
  • 119
  • 1
  • 4
  • Can you expand on your example to show how this function is run? Specifically what's `output`. How does this create a wav? – v25 Jan 17 '20 at 23:53
  • 1
    I pasted the rest of my current code – Luxaaa Jan 18 '20 at 00:08
  • I'll hopefully come back to this with an answer, but the solution is probably somewhere between [this gist](https://gist.github.com/hosackm/289814198f43976aff9b) and [this so answer](https://stackoverflow.com/questions/51079338/audio-livestreaming-with-python-flask). – v25 Jan 18 '20 at 00:52

0 Answers0