From c124d53aff297e620b1441ac661872615e8b2f3c Mon Sep 17 00:00:00 2001 From: Tyrone-Zhao Date: Sat, 22 Sep 2018 14:12:11 +0800 Subject: [PATCH 1/4] change cursor's encoding to 'utf8' --- dejavu/database_sql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dejavu/database_sql.py b/dejavu/database_sql.py index 031bdcb..cf68c20 100755 --- a/dejavu/database_sql.py +++ b/dejavu/database_sql.py @@ -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) From b52b8ef7bd22f023790489e3e0eaac95ebbf3575 Mon Sep 17 00:00:00 2001 From: Tyrone-Zhao Date: Sat, 22 Sep 2018 14:24:32 +0800 Subject: [PATCH 2/4] change all cursor's encoding to 'utf8' --- dejavu.cnf.SAMPLE | 2 +- dejavu/database_sql.py | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) 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/database_sql.py b/dejavu/database_sql.py index cf68c20..18bb238 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) @@ -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,7 +207,7 @@ 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): @@ -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 From bfe0003551e8c745e425668ec8c5c15f0245e07a Mon Sep 17 00:00:00 2001 From: Tyrone-Zhao Date: Sat, 22 Sep 2018 15:32:11 +0800 Subject: [PATCH 3/4] change to print song_name and file_sha1 with 'utf8' encoding correctly --- dejavu.py | 3 ++- dejavu/__init__.py | 4 ++-- dejavu/database_sql.py | 4 ++-- dejavu/recognize.py | 1 + 4 files changed, 7 insertions(+), 5 deletions(-) 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 18bb238..e827260 100755 --- a/dejavu/database_sql.py +++ b/dejavu/database_sql.py @@ -214,7 +214,7 @@ class SQLDatabase(Database): """ 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() diff --git a/dejavu/recognize.py b/dejavu/recognize.py index b43a879..09c03d9 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 From df14366a944570ca2a40241285ccf87fa0788d1f Mon Sep 17 00:00:00 2001 From: Tyrone-Zhao Date: Sat, 22 Sep 2018 19:41:01 +0800 Subject: [PATCH 4/4] make the '--recognize mic 10' function avalible --- dejavu/recognize.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/dejavu/recognize.py b/dejavu/recognize.py index 09c03d9..269a82a 100755 --- a/dejavu/recognize.py +++ b/dejavu/recognize.py @@ -61,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 @@ -81,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 @@ -102,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()