diff --git a/dejavu.cnf.SAMPLE b/dejavu.cnf.SAMPLE index 3e980a9..cd677b0 100755 --- a/dejavu.cnf.SAMPLE +++ b/dejavu.cnf.SAMPLE @@ -2,7 +2,7 @@ "database": { "host": "127.0.0.1", "user": "root", - "passwd": "", + "passwd": "12345678", "db": "dejavu" } } \ No newline at end of file diff --git a/dejavu.py b/dejavu.py index 0bb4d0c..a7f74a1 100755 --- a/dejavu.py +++ b/dejavu.py @@ -88,6 +88,7 @@ if __name__ == '__main__': song = djv.recognize(MicrophoneRecognizer, seconds=opt_arg) elif source == 'file': song = djv.recognize(FileRecognizer, opt_arg) - print(song) + decoded_song = repr(song).decode('string_escape') + print(decoded_song) sys.exit(0) diff --git a/dejavu/__init__.py b/dejavu/__init__.py index 4f6e6e8..7cc3f3b 100755 --- a/dejavu/__init__.py +++ b/dejavu/__init__.py @@ -155,11 +155,11 @@ class Dejavu(object): fingerprint.DEFAULT_OVERLAP_RATIO, 5) song = { Dejavu.SONG_ID : song_id, - Dejavu.SONG_NAME : songname, + Dejavu.SONG_NAME : songname.encode("utf8"), Dejavu.CONFIDENCE : largest_count, Dejavu.OFFSET : int(largest), Dejavu.OFFSET_SECS : nseconds, - Database.FIELD_FILE_SHA1 : song.get(Database.FIELD_FILE_SHA1, None),} + Database.FIELD_FILE_SHA1 : song.get(Database.FIELD_FILE_SHA1, None).encode("utf8"),} return song def recognize(self, recognizer, *options, **kwoptions): diff --git a/dejavu/database_sql.py b/dejavu/database_sql.py index 031bdcb..e827260 100755 --- a/dejavu/database_sql.py +++ b/dejavu/database_sql.py @@ -154,7 +154,7 @@ class SQLDatabase(Database): This also removes all songs that have been added but have no fingerprints associated with them. """ - with self.cursor() as cur: + with self.cursor(charset="utf8") as cur: cur.execute(self.CREATE_SONGS_TABLE) cur.execute(self.CREATE_FINGERPRINTS_TABLE) cur.execute(self.DELETE_UNFINGERPRINTED) @@ -167,7 +167,7 @@ class SQLDatabase(Database): .. warning: This will result in a loss of data """ - with self.cursor() as cur: + with self.cursor(charset="utf8") as cur: cur.execute(self.DROP_FINGERPRINTS) cur.execute(self.DROP_SONGS) @@ -177,14 +177,14 @@ class SQLDatabase(Database): """ Removes all songs that have no fingerprints associated with them. """ - with self.cursor() as cur: + with self.cursor(charset="utf8") as cur: cur.execute(self.DELETE_UNFINGERPRINTED) def get_num_songs(self): """ Returns number of songs the database has fingerprinted. """ - with self.cursor() as cur: + with self.cursor(charset="utf8") as cur: cur.execute(self.SELECT_UNIQUE_SONG_IDS) for count, in cur: @@ -195,7 +195,7 @@ class SQLDatabase(Database): """ Returns number of fingerprints the database has fingerprinted. """ - with self.cursor() as cur: + with self.cursor(charset="utf8") as cur: cur.execute(self.SELECT_NUM_FINGERPRINTS) for count, in cur: @@ -207,14 +207,14 @@ class SQLDatabase(Database): Set the fingerprinted flag to TRUE (1) once a song has been completely fingerprinted in the database. """ - with self.cursor() as cur: + with self.cursor(charset="utf8") as cur: cur.execute(self.UPDATE_SONG_FINGERPRINTED, (sid,)) def get_songs(self): """ Return songs that have the fingerprinted flag set TRUE (1). """ - with self.cursor(cursor_type=DictCursor) as cur: + with self.cursor(cursor_type=DictCursor, charset="utf8") as cur: cur.execute(self.SELECT_SONGS) for row in cur: yield row @@ -223,7 +223,7 @@ class SQLDatabase(Database): """ Returns song by its ID. """ - with self.cursor(cursor_type=DictCursor) as cur: + with self.cursor(cursor_type=DictCursor, charset="utf8") as cur: cur.execute(self.SELECT_SONG, (sid,)) return cur.fetchone() @@ -231,14 +231,14 @@ class SQLDatabase(Database): """ Insert a (sha1, song_id, offset) row into database. """ - with self.cursor() as cur: + with self.cursor(charset="utf8") as cur: cur.execute(self.INSERT_FINGERPRINT, (hash, sid, offset)) def insert_song(self, songname, file_hash): """ Inserts song in the database and returns the ID of the inserted record. """ - with self.cursor() as cur: + with self.cursor(charset="utf8") as cur: cur.execute(self.INSERT_SONG, (songname, file_hash)) return cur.lastrowid @@ -252,7 +252,7 @@ class SQLDatabase(Database): # select all if no key query = self.SELECT_ALL if hash is None else self.SELECT - with self.cursor() as cur: + with self.cursor(charset="utf8") as cur: cur.execute(query) for sid, offset in cur: yield (sid, offset) @@ -272,7 +272,7 @@ class SQLDatabase(Database): for hash, offset in hashes: values.append((hash, sid, offset)) - with self.cursor() as cur: + with self.cursor(charset="utf8") as cur: for split_values in grouper(values, 1000): cur.executemany(self.INSERT_FINGERPRINT, split_values) @@ -289,7 +289,7 @@ class SQLDatabase(Database): # Get an iteratable of all the hashes we need values = mapper.keys() - with self.cursor() as cur: + with self.cursor(charset="utf8") as cur: for split_values in grouper(values, 1000): # Create our IN part of the query query = self.SELECT_MULTIPLE diff --git a/dejavu/recognize.py b/dejavu/recognize.py index b43a879..269a82a 100755 --- a/dejavu/recognize.py +++ b/dejavu/recognize.py @@ -1,3 +1,4 @@ +# encoding: utf-8 import dejavu.fingerprint as fingerprint import dejavu.decoder as decoder import numpy as np @@ -60,6 +61,7 @@ class MicrophoneRecognizer(BaseRecognizer): def start_recording(self, channels=default_channels, samplerate=default_samplerate, chunksize=default_chunksize): + print("* start recording") self.chunksize = chunksize self.channels = channels self.recorded = False @@ -80,12 +82,15 @@ class MicrophoneRecognizer(BaseRecognizer): self.data = [[] for i in range(channels)] def process_recording(self): + print("* recording") data = self.stream.read(self.chunksize) nums = np.fromstring(data, np.int16) + # print(nums) for c in range(self.channels): self.data[c].extend(nums[c::self.channels]) def stop_recording(self): + print("* done recording") self.stream.stop_stream() self.stream.close() self.stream = None @@ -101,8 +106,7 @@ class MicrophoneRecognizer(BaseRecognizer): def recognize(self, seconds=10): self.start_recording() - for i in range(0, int(self.samplerate / self.chunksize - * seconds)): + for i in range(0, int(self.samplerate / self.chunksize * int(seconds))): self.process_recording() self.stop_recording() return self.recognize_recording()