def text_to_song_file(tweet_text, tweet_id=None, song_name=None): if tweet_id is None: tweet_id = time.time() tweet_text = clean_tweet(tweet_text) words = BREAK.findall(tweet_text) print words words_syls = [(word, nsyl(word)) for word in words if word] beatmap = BEATMAPS.get(song_name) if beatmap is None: beatmap = random.choice(BEATMAPS.values()) with tempfile.NamedTemporaryFile() as xml_file: xml_file.write(XML_HEADER) bm_iter = iter(beatmap) for word, syl in words_syls: durs = [] notes = [] for _ in range(syl): try: next_bm = bm_iter.next() except StopIteration as e: break durs.append(next_bm[0]) notes.append(next_bm[1]) else: cleaned_word = word.replace('@', '').replace('#', '') xml_file.write('<DURATION BEATS="%s"><PITCH NOTE="%s">%s</PITCH></DURATION>\n' % (','.join([str(x) for x in durs]), ','.join(notes), cleaned_word)) xml_file.write(XML_FOOTER) xml_file.flush() with open(xml_file.name) as f: print f.read() outfile_name = '/tmp/%s.wav' % tweet_id subprocess.call(['/home/tweetsong/festival/bin/text2wave', '-mode', 'singing', xml_file.name, '-o', outfile_name]) return outfile_name
def sing_text(): if request.method == 'POST': song_name = request.form['song_name'] if song_name is 'Random': song_name = None song_file = text_to_song_file(request.form['text'], song_name=song_name) base = os.path.basename(song_file) new_path = os.path.join(os.path.abspath('.'), 'static', base) os.rename(song_file, new_path) return render_template('sing.html', song='/static/%s' % base) else: return render_template('sing.html', song=None, beatmaps=BEATMAPS.keys())