<gh_stars>0
import wave
import numpy as np
import time
from base import *
from waves import *

canon_1_2_2_3 = [
# https://www.everyonepiano.com/Number-31-1-Canon-Pachelbels-Canon-Numbered-Musical-Notation-Preview-1.html
'''
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
+3 0 0 0+2 0 0 0  +1 0 0 0 7 0 0 0   6 0 0 0 5 0 0 0   6 0 0 0 7 0 0 0
+3 0 0 0+2 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   6 0 0 0 5 0 0 0
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   6 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   6 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
+3 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
+3 0 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-1-4-6-1-3-5 1  _4-1-4-6_5-2-5-7
-1-3-5 1_5-2-5-7  _6-3-6 1_3-3-5-7  _4-1-4-6-1-3-5 1  _4-1-4-6_5-2-5-7
-1-5 1 0_5-2-7 0  _6-3 1 0_3-3-5 0  _4-1-6 0-1-5 1 0  _4-1-6 0_5-2-7 0
-1-5 1 0_5-2-7 0  _6-3 1 0_3-3-5 0  _4-1-6 0-1-5 1 0  _4-1-6 0_5-2-7 0
-1-5 1 0_5-2-7 0  _6-3 1 0_3-3-5 0  _4-1-6 0-1-5 1 0  _4-1-6 0_5-2-7 0
-1-5 1 3_5-2-5-7  _6-3-6 1_3-3-5-7  _4-1-4-6-1-5 1 3  _4-1-4-6_5-2-5-7
-1-3-5 3_5-2-5-7  _6-3-6 1_3-3-5-7  _4-1-4-6-1-5 1 3  _4-1-4-6_5-2-5-7
-1-3-5 3_5-2-5-7  _6-3-6 1_3-3-5-7  _4-1-4-6-1-5 1 3  _4-1-4-6_5-2-5-7
-1-3-5 3_5-2-5-7  _6-3-6 1_3-3-5-7  _4-1-4-6-1-5 1 3  _4-1-4-6_5-2-5-7
-1-3-5 3_5-2-5-7  _6-3-6 1_3-3-5-7  _4-1-4-6-1-5 1 3  _4-1-4-6_5-2-5-7
-1-3-5 3_5-2-5-7  _6-3-6 1_3-3-5-7  _4-1-4-6-1-5 1 3  _4-1-4-6_5-2-5-7
-1-3-5 1_7-2-5-7  _6-3-6 1_3-3-5-7  _4-1-4-6-1-3-5 1  _4-1-4-6_5-2-5-7
-1-3-5 1_7-2-5-7  _6-3-6 1_3-3-5-7  _4-1-4-6-1-3-5 1  _4-1-4-6_5-2-5-7
-1 0 0 0 0 0 0 0
'''
]

BASE_FREQUENCY = 261.626
BASE_AMPLITUDE = 5000
STRIDE_TIME = 0.25
FRAMERATE = 44100

if 1: tune_map = {'1':0, '2':2, '3':4, '4':5, '5':7, '6':9, '7':11}
else: tune_map = {'1':0, '2':2, '3':3, '4':5, '5':7, '6':8, '7':10}

x1, x21, x22, x3 = [tuneToLogFrequency(tune_seq, tune_map) for tune_seq in canon_1_2_2_3]

def addBlank(seq):
    res = [None] * (len(seq) * 2)
    res[::2] = seq
    return res

def join(seq1, seq2):
    assert(len(seq1) == len(seq2))
    res = [None] * (len(seq1) * 2)
    res[::2] = seq1
    res[1::2] = seq2
    return res

if __name__ == '__main__':
    print('running...')
    start = time.time()

    log_frequencys_1_2_3 = [addBlank(x1), join(x21, x22), addBlank(x3)]
    durations = [STRIDE_TIME * 10] * len(log_frequencys_1_2_3[0])

    wavs = [logFrequencysToWave(log_frequencys, durations, BASE_FREQUENCY, BASE_AMPLITUDE, STRIDE_TIME, FRAMERATE
            , waveKalimba) for log_frequencys in log_frequencys_1_2_3]
    wav = sum(wavs[1:], wavs[0])

    assert(np.max(wav) < 32768)
    assert(np.min(wav) >= -32768)
    wav_short = wav.astype('<i2') # short

    with wave.open("canon_kalimba.wav", 'wb') as wf:
        wf.setnchannels(1)
        wf.setframerate(FRAMERATE)
        wf.setsampwidth(2) # short
        wf.writeframes(wav_short.tobytes())

    print(f'finished, time consumed: {time.time() - start} seconds')

import wave
import numpy as np
import time
from base import *
from waves import *

canon_1_2_2_3 = [
# https://www.everyonepiano.com/Number-31-1-Canon-Pachelbels-Canon-Numbered-Musical-Notation-Preview-1.html
'''
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
+3 0 0 0+2 0 0 0  +1 0 0 0 7 0 0 0   6 0 0 0 5 0 0 0   6 0 0 0 7 0 0 0
+3 0 0 0+2 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   6 0 0 0 5 0 0 0
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0   0 0 0 0 0 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-6 1_3-3-5-7  _4-1-4-6-1-5 1 3  _4-1-4-6_5-2-5-7
-1-3-5 3_5-2-5-7  _6-3-6 1_3-3-5-7  _4-1-4-6-1-5 1 3  _4-1-4-6_5-2-5-7
-1-3-5 3_5-2-5-7  _6-3-6 1_3-3-5-7  _4-1-4-6-1-5 1 3  _4-1-4-6_5-2-5-7
-1-3-5 3_5-2-5-7  _6-3-6 1_3-3-5-7  _4-1-4-6-1-5 1 3  _4-1-4-6_5-2-5-7
-1-3-5 1_7-2-5-7  _6-3-6 1_3-3-5-7  _4-1-4-6-1-3-5 1  _4-1-4-6_5-2-5-7
-1-3-5 1_7-2-5-7  _6-3-6 1_3-3-5-7  _4-1-4-6-1-3-5 1  _4-1-4-6_5-2-5-7
-1 0 0 0 0 0 0 0
'''
]

BASE_FREQUENCY = 261.626
BASE_AMPLITUDE = 5000
STRIDE_TIME = 0.25
FRAMERATE = 44100

if 1: tune_map = {'1':0, '2':2, '3':4, '4':5, '5':7, '6':9, '7':11}
else: tune_map = {'1':0, '2':2, '3':3, '4':5, '5':7, '6':8, '7':10}

x1, x21, x22, x3 = [tuneToLogFrequency(tune_seq, tune_map) for tune_seq in canon_1_2_2_3]

def addBlank(seq):
    res = [None] * (len(seq) * 2)
    res[::2] = seq
    return res

def join(seq1, seq2):
    assert(len(seq1) == len(seq2))
    res = [None] * (len(seq1) * 2)
    res[::2] = seq1
    res[1::2] = seq2
    return res

if __name__ == '__main__':
    print('running...')
    start = time.time()

    log_frequencys_1_2_3 = [addBlank(x1), join(x21, x22), addBlank(x3)]
    durations = [STRIDE_TIME * 10] * len(log_frequencys_1_2_3[0])

    wavs = [logFrequencysToWave(log_frequencys, durations, BASE_FREQUENCY, BASE_AMPLITUDE, STRIDE_TIME, FRAMERATE
            , waveKalimba) for log_frequencys in log_frequencys_1_2_3]
    wav = sum(wavs[1:], wavs[0])

    assert(np.max(wav) < 32768)
    assert(np.min(wav) >= -32768)
    wav_short = wav.astype('<i2') # short

    with wave.open("canon_kalimba.wav", 'wb') as wf:
        wf.setnchannels(1)
        wf.setframerate(FRAMERATE)
        wf.setsampwidth(2) # short
        wf.writeframes(wav_short.tobytes())

    print(f'finished, time consumed: {time.time() - start} seconds')
# https://www.everyonepiano.com/Number-31-1-Canon-Pachelbels-Canon-Numbered-Musical-Notation-Preview-1.html 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 +3 0 0 0+2 0 0 0  +1 0 0 0 7 0 0 0   6 0 0 0 5 0 0 0   6 0 0 0 7 0 0 0 +3 0 0 0+2 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   6 0 0 0 5 0 0 0  0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0  0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0  0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   6 0 0 0 0 0 0 0  0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   6 0 0 0 0 0 0 0  0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0  0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0  0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0  0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0  0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0  0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 +3 0 0 0 0 0 0 0   0 0 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