Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python 3 compatibility #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions collect-fingerprints-of-songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,33 @@
colored('channels=%d', 'white', attrs=['dark']), # channels
colored('%s', 'white', attrs=['bold']) # filename
)
print msg % (song_id, len(audio['channels']), filename)
print(msg % (song_id, len(audio['channels']), filename))

if song:
hash_count = db.get_song_hashes_count(song_id)

if hash_count > 0:
msg = ' already exists (%d hashes), skip' % hash_count
print colored(msg, 'red')
print(colored(msg, 'red'))

continue

print colored(' new song, going to analyze..', 'green')
print(colored(' new song, going to analyze..', 'green'))

hashes = set()
channel_amount = len(audio['channels'])

for channeln, channel in enumerate(audio['channels']):
msg = ' fingerprinting channel %d/%d'
print colored(msg, attrs=['dark']) % (channeln+1, channel_amount)
print(colored(msg, attrs=['dark']) % (channeln+1, channel_amount))

channel_hashes = fingerprint.fingerprint(channel, Fs=audio['Fs'], plots=config['fingerprint.show_plots'])
channel_hashes = set(channel_hashes)

msg = ' finished channel %d/%d, got %d hashes'
print colored(msg, attrs=['dark']) % (
print(colored(msg, attrs=['dark']) % (
channeln+1, channel_amount, len(channel_hashes)
)
))

hashes |= channel_hashes

Expand All @@ -67,7 +67,7 @@
values.append((song_id, hash, offset))

msg = ' storing %d hashes in db' % len(values)
print colored(msg, 'green')
print(colored(msg, 'green'))

db.store_fingerprints(values)

Expand Down
16 changes: 8 additions & 8 deletions libs/db_sqlite.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from db import Database
from config import get_config
import sqlite3
import sys
from itertools import izip_longest
import sqlite3
from libs.db import Database
from libs.config import get_config
from itertools import zip_longest
from termcolor import colored

class SqliteDatabase(Database):
Expand Down Expand Up @@ -66,11 +66,11 @@ def findAll(self, table, params):

def insert(self, table, params):
keys = ', '.join(params.keys())
values = params.values()
values = list(params.values())

query = "INSERT INTO songs (%s) VALUES (?, ?)" % (keys);
query = "INSERT INTO songs (%s) VALUES ('%s','%s')" % (keys, values[0], values[1]);

self.cur.execute(query, values)
self.cur.execute(query)
self.conn.commit()

return self.cur.lastrowid
Expand All @@ -79,7 +79,7 @@ def insertMany(self, table, columns, values):
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return (filter(None, values) for values
in izip_longest(fillvalue=fillvalue, *args))
in zip_longest(fillvalue=fillvalue, *args))

for split_values in grouper(values, 1000):
query = "INSERT OR IGNORE INTO %s (%s) VALUES (?, ?, ?)" % (table, ", ".join(columns))
Expand Down
8 changes: 6 additions & 2 deletions libs/fingerprint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import hashlib
import numpy as np
import matplotlib.mlab as mlab
# import matplotlib
# matplotlib.use('TKAgg')
import matplotlib.pyplot as plt

from termcolor import colored
Expand Down Expand Up @@ -95,7 +97,8 @@ def fingerprint(channel_samples, Fs=DEFAULT_FS,
local_maxima = get_2D_peaks(arr2D, plot=plots, amp_min=amp_min)

msg = ' local_maxima: %d of frequency & time pairs'
print colored(msg, attrs=['dark']) % len(local_maxima)
local_maxima = list(local_maxima)
print(colored(msg, attrs=['dark']) % len(local_maxima))

# return hashes
return generate_hashes(local_maxima, fan_value=fan_value)
Expand Down Expand Up @@ -164,5 +167,6 @@ def generate_hashes(peaks, fan_value=DEFAULT_FAN_VALUE):

# check if delta is between min & max
if t_delta >= MIN_HASH_TIME_DELTA and t_delta <= MAX_HASH_TIME_DELTA:
h = hashlib.sha1("%s|%s|%s" % (str(freq1), str(freq2), str(t_delta)))
full_code = "%s|%s|%s" %(str(freq1), str(freq2), str(t_delta))
h = hashlib.sha1(full_code.encode('utf-8'))
yield (h.hexdigest()[0:FINGERPRINT_REDUCTION], t1)
8 changes: 4 additions & 4 deletions libs/reader_file.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from reader import BaseReader
import os
import numpy as np
from reader import Reader
from pydub import AudioSegment
from pydub.utils import audioop
import numpy as np
from hashlib import sha1

class FileReader(BaseReader):
class FileReader(Reader):
def __init__(self, filename):
# super(FileReader, self).__init__(a)
self.filename = filename
Expand Down Expand Up @@ -37,7 +37,7 @@ def parse_audio(self):
data = np.fromstring(audiofile._data, np.int16)

channels = []
for chn in xrange(audiofile.channels):
for chn in range(audiofile.channels):
channels.append(data[chn::audiofile.channels])

fs = audiofile.frame_rate
Expand Down
11 changes: 8 additions & 3 deletions libs/reader_microphone.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import pyaudio
import numpy
import wave
from reader import BaseReader
from reader import Reader
from reader._search import Search
from reader._storage import Storage
from reader._parser import Parser
from reader._types import NameScheme

class MicrophoneReader(BaseReader):

class MicrophoneReader(Reader):
default_chunksize = 8192
default_format = pyaudio.paInt16
default_channels = 2
Expand All @@ -12,7 +17,7 @@ class MicrophoneReader(BaseReader):

# set default
def __init__(self, a):
super(MicrophoneReader, self).__init__(a)
super(MicrophoneReader, self).__init__(a, Storage, Search, Parser, NameScheme)
self.audio = pyaudio.PyAudio()
self.stream = None
self.data = []
Expand Down
45 changes: 23 additions & 22 deletions recognize-from-microphone.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import argparse

from argparse import RawTextHelpFormatter
from itertools import izip_longest
from itertools import zip_longest
from termcolor import colored
from libs.config import get_config
from libs.reader_microphone import MicrophoneReader
Expand Down Expand Up @@ -44,7 +44,7 @@
channels=channels)

msg = ' * started recording..'
print colored(msg, attrs=['dark'])
print(colored(msg, attrs=['dark']))

while True:
bufferSize = int(reader.rate / reader.chunksize * seconds)
Expand All @@ -54,10 +54,10 @@

if visualise_console:
msg = colored(' %05d', attrs=['dark']) + colored(' %s', 'green')
print msg % visual_peak.calc(nums)
print(msg % visual_peak.calc(nums))
else:
msg = ' processing %d of %d..' % (i, bufferSize)
print colored(msg, attrs=['dark'])
print(colored(msg, attrs=['dark']))

if not record_forever: break

Expand All @@ -68,19 +68,19 @@
reader.stop_recording()

msg = ' * recording has been stopped'
print colored(msg, attrs=['dark'])
print(colored(msg, attrs=['dark']))



def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return (filter(None, values) for values
in izip_longest(fillvalue=fillvalue, *args))
return (list(filter(None, values)) for values
in zip_longest(fillvalue=fillvalue, *args))

data = reader.get_recorded_data()

msg = ' * recorded %d samples'
print colored(msg, attrs=['dark']) % len(data[0])
print(colored(msg, attrs=['dark']) % len(data[0]))

# reader.save_recorded('test.wav')

Expand Down Expand Up @@ -108,40 +108,41 @@ def return_matches(hashes):
FROM fingerprints
WHERE upper(hash) IN (%s)
"""
query = query % ', '.join('?' * len(split_values))
list_leng = len(list(split_values))
query = query % ', '.join('?' * list_leng)

x = db.executeAll(query, split_values)
matches_found = len(x)

if matches_found > 0:
msg = ' ** found %d hash matches (step %d/%d)'
print colored(msg, 'green') % (
print(colored(msg, 'green') % (
matches_found,
len(split_values),
len(values)
)
))
else:
msg = ' ** not matches found (step %d/%d)'
print colored(msg, 'red') % (
print(colored(msg, 'red') % (
len(split_values),
len(values)
)
))

for hash, sid, offset in x:
# (sid, db_offset - song_sampled_offset)
yield (sid, offset - mapper[hash])
yield (sid, mapper[hash])

for channeln, channel in enumerate(data):
# TODO: Remove prints or change them into optional logging.
msg = ' fingerprinting channel %d/%d'
print colored(msg, attrs=['dark']) % (channeln+1, channel_amount)
print(colored(msg, attrs=['dark']) % (channeln+1, channel_amount))

matches.extend(find_matches(channel))

msg = ' finished channel %d/%d, got %d hashes'
print colored(msg, attrs=['dark']) % (
print(colored(msg, attrs=['dark']) % (
channeln+1, channel_amount, len(matches)
)
))

def align_matches(matches):
diff_counter = {}
Expand Down Expand Up @@ -181,23 +182,23 @@ def align_matches(matches):

total_matches_found = len(matches)

print ''
print('')

if total_matches_found > 0:
msg = ' ** totally found %d hash matches'
print colored(msg, 'green') % total_matches_found
print(colored(msg, 'green') % total_matches_found)

song = align_matches(matches)

msg = ' => song: %s (id=%d)\n'
msg += ' offset: %d (%d secs)\n'
msg += ' confidence: %d'

print colored(msg, 'green') % (
print(colored(msg, 'green') % (
song['SONG_NAME'], song['SONG_ID'],
song['OFFSET'], song['OFFSET_SECS'],
song['CONFIDENCE']
)
))
else:
msg = ' ** not matches found at all'
print colored(msg, 'red')
print(colored(msg, 'red'))
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
numpy>=1
matplotlib
termcolor
pyaudio
wave
pydub
reader
scipy
2 changes: 1 addition & 1 deletion tests/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
row = db.executeOne("SELECT 2+3 as x;")

assert row[0] == 5, "failed simple sql execution"
print ' * %s' % colored('ok', 'green')
print(' * %s' % colored('ok', 'green'))