Skip to content

Commit

Permalink
#165.
Browse files Browse the repository at this point in the history
  • Loading branch information
gogins committed Jul 9, 2021
1 parent d0356ed commit 3b91aa8
Show file tree
Hide file tree
Showing 4 changed files with 852 additions and 11 deletions.
2 changes: 2 additions & 0 deletions playpen/playpen.glade
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@
<object class="GtkToolButton" id="edit_gui_button">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Edit user-defined controls with Glade...</property>
<property name="label" translatable="yes">Edit GUI...</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-edit</property>
</object>
<packing>
<property name="expand">False</property>
Expand Down
127 changes: 118 additions & 9 deletions playpen/playpen.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import datetime
import math
import os.path
import random
import string
import subprocess
import sys
import time
Expand Down Expand Up @@ -31,6 +33,7 @@
'''

import gi
from gi.repository import Gdk
from gi.repository import GObject
from gi.repository import GLib
gi.require_version("Gtk", "3.0")
Expand All @@ -46,7 +49,6 @@
from gi.repository import GtkSource
import ctcsound


csound = ctcsound.Csound()

## Gst.init(sys.argv)
Expand All @@ -60,7 +62,11 @@
filename = ""
piece = ""

controls_template = '''
glade_controls_template = '''
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.2 -->
<interface>
</interface>
'''

def print_(text):
Expand Down Expand Up @@ -101,8 +107,10 @@ def on_new_button_clicked(button):
Gtk.ResponseType.OK)
file_chooser_dialog.run()
filename = file_chooser_dialog.get_filename()
glade_file = glade_filename_(filename)
with open(glade_file, 'w') as file:
file.write(glade_controls_template)
print_(filename)
# TODO: Create template glade file.
except:
print_(traceback.format_exc())

Expand Down Expand Up @@ -142,12 +150,16 @@ def connect_controls(container, handler):
pass
if issubclass(type(child), Gtk.Container):
connect_controls(child, handler)
def load_glade(filename):

def glade_filename_(filename):
basename = os.path.basename(filename)
filename_ = os.path.splitext(basename)[0]
glade_file = filename_ + ".glade"
print_("glade_file: {}".format(glade_file))
return glade_file

def load_glade(filename):
glade_file = glade_filename_(filename)
if os.path.exists(glade_file) == True:
try:
with open(glade_file, "r") as file:
Expand Down Expand Up @@ -246,15 +258,102 @@ def on_play_audio_button_clicked(button):
csound.stop()
csound.cleanup()
csound.reset()
# Post-process and edit here.
post_process()
except:
print_(traceback.format_exc())

def render():
pass

def post_process():
pass
try:
print_("Post-processing...")
cwd = os.getcwd()
print_('cwd: ', cwd)
filepath = sys.argv[1]
author = 'Michael Gogins'
year = '2019'
license = 'ASCAP'
publisher = 'Irreducible Productions, ASCAP'
notes = 'Electroacoustic Music'

directory, basename = os.path.split(filepath)
rootname = os.path.splitext(basename)[0].split('.')[0]
title = rootname.replace("-", " ").replace("_", " ")
label = '%s -- %s' % (author, title)
master_filename = '%s.normalized.wav' % label
spectrogram_filename = '%s.png' % label
cd_quality_filename = '%s.cd.wav' % label
mp3_filename = '%s.mp3' % label
mp4_filename = '%s.mp4' % label
flac_filename = '%s.flac' % label
print_('Original file: ', filepath)
print_('Basename: ', basename)
print_('Author: ', author)
print_('Title: ', title)
print_('Year: ', year)
str_copyright = 'Copyright %s by %s' % (year, author)
print_('Copyright: ', str_copyright)
print_('Licence: ', license)
print_('Publisher: ', publisher)
print_('Notes: ', notes)
print_('Master filename: ', master_filename)
print_('Spectrogram filename: ', spectrogram_filename)
print_('CD quality filename: ', cd_quality_filename)
print_('MP3 filename: ', mp3_filename)
print_('MP4 filename: ', mp4_filename)
print_('FLAC filename: ', flac_filename)
bext_description = notes
bext_originator = author
bext_orig_ref = basename
#bext_umid = xxx
#bext_orig_date = xxx
#bext_orig_time = xxx
#bext_coding_hist = xxx
#bext_time_ref = xxx
str_comment = notes
str_title = title
str_artist = author
str_date = year
str_license = license
sox_normalize_command = '''sox -S "%s" "%s" gain -n -3''' % (filepath, master_filename + 'untagged.wav')
print_('sox_normalize command: ', sox_normalize_command)
os.system(sox_normalize_command)
tag_wav_command = '''sndfile-metadata-set "%s" --bext-description "%s" --bext-originator "%s" --bext-orig-ref "%s" --str-comment "%s" --str-title "%s" --str-copyright "%s" --str-artist "%s" --str-date "%s" --str-license "%s" "%s"''' % (master_filename + 'untagged.wav', bext_description, bext_originator, bext_orig_ref, str_comment, str_title, str_copyright, str_artist, str_date, str_license, master_filename)
print_('tag_wav_command: ', tag_wav_command)
os.system(tag_wav_command)
sox_spectrogram_command = '''sox -S "%s" -n spectrogram -o "%s" -t"%s" -c"%s"''' % (master_filename, spectrogram_filename, label, str_copyright + ' (%s' % publisher)
print_('sox_spectrogram_command:', sox_spectrogram_command)
os.system(sox_spectrogram_command)
sox_cd_command = '''sox -S "%s" -b 16 -r 44100 "%s"''' % (master_filename, cd_quality_filename + 'untagged.wav')
print_('sox_cd_command: ', sox_cd_command)
os.system(sox_cd_command)
tag_wav_command = '''sndfile-metadata-set "%s" --bext-description "%s" --bext-originator "%s" --bext-orig-ref "%s" --str-comment "%s" --str-title "%s" --str-copyright "%s" --str-artist "%s" --str-date "%s" --str-license "%s" "%s"''' % (cd_quality_filename + 'untagged.wav', bext_description, bext_originator, bext_orig_ref, str_comment, str_title, str_copyright, str_artist, str_date, str_license, cd_quality_filename)
print_('tag_wav_command: ', tag_wav_command)
os.system(tag_wav_command)
mp3_command = '''lame --add-id3v2 --tt "%s" --ta "%s" --ty "%s" --tn "%s" --tg "%s" "%s" "%s"''' % (title, "Michael Gogins", year, notes, "Electroacoustic", master_filename, mp3_filename)
print_('mp3_command: ', mp3_command)
os.system(mp3_command)
sox_flac_command = '''sox -S "%s" "%s"''' % (master_filename, flac_filename)
print_('sox_flac_command: ', sox_flac_command)
os.system(sox_flac_command)
mp4_command = '''%s -r 1 -i "%s" -i "%s" -codec:a aac -strict -2 -b:a 384k -c:v libx264 -b:v 500k "%s"''' % ('ffmpeg', os.path.join(cwd, spectrogram_filename), os.path.join(cwd, master_filename), os.path.join(cwd, mp4_filename))
mp4_metadata = '-metadata title="%s" ' % title
mp4_metadata += '-metadata date="%s" ' % year
mp4_metadata += '-metadata genre="%s" ' % notes
mp4_metadata += '-metadata copyright="%s" ' % str_copyright
mp4_metadata += '-metadata composer="%s" ' % author
mp4_metadata += '-metadata artist="%s" ' % author
mp4_metadata += '-metadata publisher="%s" ' % publisher
mp4_command = '''"%s" -y -loop 1 -framerate 2 -i "%s" -i "%s" -c:v libx264 -preset medium -tune stillimage -crf 18 -codec:a aac -strict -2 -b:a 384k -r:a 48000 -shortest -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" %s "%s"''' % ('ffmpeg', os.path.join(cwd, spectrogram_filename), os.path.join(cwd, master_filename), mp4_metadata, os.path.join(cwd, mp4_filename))
mp4_command = mp4_command.replace('\\', '/')
print_('mp4_command: ', mp4_command)
os.system(mp4_command)
os.system('del *wavuntagged.wav')
os.system('audacity "%s"' % master_filename)
print_()
except:
print_(traceback.format_exc())

def on_render_soundfile_button_clicked(button):
try:
Expand Down Expand Up @@ -293,7 +392,7 @@ def on_edit_gui_button_clicked(button):
print_(button.get_label())
basename = os.path.basename(filename)
filename_ = os.path.splitext(basename)[0]
glade_file = filename_ + ".glade"
glade_file = glade_filename_(filename)
print_("glade_file: {}".format(glade_file))
subprocess.run("glade {}".format(glade_file), shell=True)
print_("Finished editing {}.".format(glade_file))
Expand All @@ -314,6 +413,13 @@ def on_stop_button_clicked(button):
except:
print_(traceback.format_exc())

'''
Widget classes for Csound channel messages:
- Button
- Range
- Entry
'''

def get_widget_value(widget):
print(type(widget))
try:
Expand All @@ -331,7 +437,7 @@ def get_widget_value(widget):
def on_control_change(control):
name = control.get_name()
value = get_widget_value(control)
print_("on_control_change: {}: {}".format(name, value))
print_("on_control_change: {}: {} {}".format(name, value))
if type(value) != str:
csound.setControlChannel(control.get_name(), value)
else:
Expand All @@ -349,8 +455,11 @@ def on_destroy(source):
main_window.connect("destroy", on_destroy)
html_window = builder.get_object("html_window")
code_editor = builder.get_object("code_editor")
code_editor.override_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(255/255, 160/255, 122/255, 1.0))
controls_layout = builder.get_object("controls_layout")
messages_text_view = builder.get_object("messages_text_view")
messages_text_view.override_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(0.0, 0.8, 0.0, 1.0))
messages_text_view.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(0.1, 0.1, 0.1, 1.0))
messages_text_buffer = messages_text_view.get_buffer()
webview = WebKit2.WebView()
webview.load_uri("http://csound.com")
Expand Down
Loading

0 comments on commit 3b91aa8

Please sign in to comment.