|
| 1 | +""" |
| 2 | +This generates a 30 minute silent wav, and is capable of |
| 3 | +responding to Range requests. |
| 4 | +""" |
| 5 | +import time |
| 6 | +import re |
| 7 | +import struct |
| 8 | + |
| 9 | + |
| 10 | +def create_wav_header(sample_rate, bit_depth, channels, duration): |
| 11 | + bytes_per_sample = bit_depth / 8 |
| 12 | + block_align = bytes_per_sample * channels |
| 13 | + byte_rate = sample_rate * block_align |
| 14 | + sub_chunk_2_size = duration * byte_rate |
| 15 | + |
| 16 | + data = b'' |
| 17 | + # ChunkID |
| 18 | + data += b'RIFF' |
| 19 | + # ChunkSize |
| 20 | + data += struct.pack('<L', 36 + sub_chunk_2_size) |
| 21 | + # Format |
| 22 | + data += b'WAVE' |
| 23 | + # Subchunk1ID |
| 24 | + data += b'fmt ' |
| 25 | + # Subchunk1Size |
| 26 | + data += struct.pack('<L', 16) |
| 27 | + # AudioFormat |
| 28 | + data += struct.pack('<H', 1) |
| 29 | + # NumChannels |
| 30 | + data += struct.pack('<H', channels) |
| 31 | + # SampleRate |
| 32 | + data += struct.pack('<L', sample_rate) |
| 33 | + # ByteRate |
| 34 | + data += struct.pack('<L', byte_rate) |
| 35 | + # BlockAlign |
| 36 | + data += struct.pack('<H', block_align) |
| 37 | + # BitsPerSample |
| 38 | + data += struct.pack('<H', bit_depth) |
| 39 | + # Subchunk2ID |
| 40 | + data += b'data' |
| 41 | + # Subchunk2Size |
| 42 | + data += struct.pack('<L', sub_chunk_2_size) |
| 43 | + |
| 44 | + return data |
| 45 | + |
| 46 | + |
| 47 | +def main(request, response): |
| 48 | + response.headers.set("Content-Type", "audio/wav") |
| 49 | + response.headers.set("Accept-Ranges", "bytes") |
| 50 | + response.headers.set("Cache-Control", "no-cache") |
| 51 | + |
| 52 | + range_header = request.headers.get('Range', '') |
| 53 | + range_received_key = request.GET.first('range-received-key', '') |
| 54 | + |
| 55 | + if range_received_key and range_header: |
| 56 | + # This is later collected using stash-take.py |
| 57 | + request.stash.put(range_received_key, 'range-header-received', '/fetch/range/') |
| 58 | + |
| 59 | + # Audio details |
| 60 | + sample_rate = 8000 |
| 61 | + bit_depth = 8 |
| 62 | + channels = 1 |
| 63 | + duration = 60 * 5 |
| 64 | + |
| 65 | + total_length = (sample_rate * bit_depth * channels * duration) / 8 |
| 66 | + bytes_remaining_to_send = total_length |
| 67 | + initial_write = '' |
| 68 | + |
| 69 | + if range_header: |
| 70 | + response.status = 206 |
| 71 | + start, end = re.search(r'^bytes=(\d*)-(\d*)$', range_header).groups() |
| 72 | + |
| 73 | + start = int(start) |
| 74 | + end = int(end) if end else 0 |
| 75 | + |
| 76 | + if end: |
| 77 | + bytes_remaining_to_send = (end + 1) - start |
| 78 | + else: |
| 79 | + bytes_remaining_to_send = total_length - start |
| 80 | + |
| 81 | + wav_header = create_wav_header(sample_rate, bit_depth, channels, duration) |
| 82 | + |
| 83 | + if start < len(wav_header): |
| 84 | + initial_write = wav_header[start:] |
| 85 | + |
| 86 | + if bytes_remaining_to_send < len(initial_write): |
| 87 | + initial_write = initial_write[0:bytes_remaining_to_send] |
| 88 | + |
| 89 | + content_range = "bytes {}-{}/{}".format(start, end or total_length - 1, total_length) |
| 90 | + |
| 91 | + response.headers.set("Content-Range", content_range) |
| 92 | + else: |
| 93 | + initial_write = create_wav_header(sample_rate, bit_depth, channels, duration) |
| 94 | + |
| 95 | + response.headers.set("Content-Length", bytes_remaining_to_send) |
| 96 | + |
| 97 | + response.write_status_headers() |
| 98 | + response.writer.write(initial_write) |
| 99 | + |
| 100 | + bytes_remaining_to_send -= len(initial_write) |
| 101 | + |
| 102 | + while bytes_remaining_to_send > 0: |
| 103 | + if not response.writer.flush(): |
| 104 | + break |
| 105 | + |
| 106 | + to_send = b'\x00' * min(bytes_remaining_to_send, sample_rate) |
| 107 | + bytes_remaining_to_send -= len(to_send) |
| 108 | + |
| 109 | + response.writer.write(to_send) |
| 110 | + # Throttle the stream |
| 111 | + time.sleep(0.5) |
0 commit comments