Last active
December 24, 2019 17:05
-
-
Save rlskoeser/cc7a7b22012dbe5ea7572a298c78acd7 to your computer and use it in GitHub Desktop.
script to sonify references from Derrida's Of Grammatology
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Based on sample script from https://programminghistorian.org/en/lessons/sonification#miditime | |
# currently written for python2; miditime only goes up to python 3.4 | |
# pip install miditime | |
import csv | |
from collections import defaultdict | |
from miditime.miditime import MIDITime | |
# Instantiate the class with a tempo (120bpm is the default) | |
# and an output file destination. | |
mymidi = MIDITime(360, 'derrida-references.mid') # 240 a bit slow | |
# List of notes. Each note is a list: [time, pitch, attack, duration] | |
midinotes = [] | |
# http://www.electronics.dit.ie/staff/tscarff/Music_technology/midi/midi_note_numbers_for_octaves.htm | |
# ref type | |
notes_for_type = { | |
'Citation': 60, # C | |
'Quotation': 62, # D | |
'Epigraph': 69, # A | |
'Footnote': 52 # lower E | |
} | |
# headings id,page,type_id,type | |
refs = defaultdict(dict) | |
with open('de-la-grammatologie_references.csv') as csvfile: | |
reader = csv.DictReader(csvfile) | |
for row in reader: | |
# use page number for time offset, | |
# citation type for note | |
# group to get total count for page/type | |
page = int(row['page']) | |
reftype = row['type'] | |
if reftype not in refs[page]: | |
refs[page][reftype] = 0 | |
refs[page][reftype] += 1 | |
# determine max number of single type of ref per page, | |
# to scale volume | |
max_count = max([count for info in refs.itervalues() | |
for count in info.itervalues()]) | |
def volume(val): | |
# convert number of references to volume | |
# https://stackoverflow.com/questions/929103/convert-a-number-range-to-another-range-maintaining-ratio | |
old_max = max_count | |
old_min = 1 | |
new_min = 60 | |
new_max = 255 | |
old_range = (old_max - old_min) | |
new_range = (new_max - new_min) | |
return (((val - old_min) * new_range) / old_range) + new_min | |
for page, info in refs.iteritems(): | |
for reftype, count in info.iteritems(): | |
midinotes.append([page, notes_for_type[reftype], volume(count), 1]) | |
# Add a track with those notes | |
mymidi.add_track(midinotes) | |
# chapters | |
chapternotes = [ | |
# section 1 | |
# p. 11 exergue | |
[11, 48, 255, 2], | |
# p. 15 chapter 1 | |
[15, 48, 255, 2], | |
# p. 41 chapter 2 (two note chord) | |
[41, 48, 255, 2], | |
[41, 52, 255, 2], | |
# p. 105 chapter 3 (three note chord) | |
[105, 48, 255, 2], | |
[105, 52, 255, 2], | |
[105, 55, 255, 2], | |
# section 2 | |
# p141 intro | |
[141, 48, 255, 2], | |
# p. 145 chapter 1 | |
[145, 48, 255, 2], | |
# p. 197 chapter 2 (two note chord) | |
[197, 48, 255, 2], | |
[197, 52, 255, 2], | |
# p. 227 chapter 3 (three note chord) | |
[227, 48, 255, 2], | |
[227, 52, 255, 2], | |
[227, 55, 255, 2], | |
# p. 363 chapter 4 (four note chord) | |
[363, 48, 255, 2], | |
[363, 52, 255, 2], | |
[363, 55, 255, 2], | |
[363, 59, 255, 2], | |
] | |
# add a new track with chapter markers | |
mymidi.add_track(chapternotes) | |
# Output the .mid file | |
mymidi.save_midi() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment