Merge pull request #172 from Tyrone-Zhao/dev

Dev
This commit is contained in:
Will Drevo 2019-05-27 00:13:05 -07:00 committed by GitHub
commit 4d9b784c7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 19 deletions

View file

@ -2,7 +2,7 @@
"database": { "database": {
"host": "127.0.0.1", "host": "127.0.0.1",
"user": "root", "user": "root",
"passwd": "", "passwd": "12345678",
"db": "dejavu" "db": "dejavu"
} }
} }

View file

@ -88,6 +88,7 @@ if __name__ == '__main__':
song = djv.recognize(MicrophoneRecognizer, seconds=opt_arg) song = djv.recognize(MicrophoneRecognizer, seconds=opt_arg)
elif source == 'file': elif source == 'file':
song = djv.recognize(FileRecognizer, opt_arg) song = djv.recognize(FileRecognizer, opt_arg)
print(song) decoded_song = repr(song).decode('string_escape')
print(decoded_song)
sys.exit(0) sys.exit(0)

View file

@ -155,11 +155,11 @@ class Dejavu(object):
fingerprint.DEFAULT_OVERLAP_RATIO, 5) fingerprint.DEFAULT_OVERLAP_RATIO, 5)
song = { song = {
Dejavu.SONG_ID : song_id, Dejavu.SONG_ID : song_id,
Dejavu.SONG_NAME : songname, Dejavu.SONG_NAME : songname.encode("utf8"),
Dejavu.CONFIDENCE : largest_count, Dejavu.CONFIDENCE : largest_count,
Dejavu.OFFSET : int(largest), Dejavu.OFFSET : int(largest),
Dejavu.OFFSET_SECS : nseconds, 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 return song
def recognize(self, recognizer, *options, **kwoptions): def recognize(self, recognizer, *options, **kwoptions):

View file

@ -154,7 +154,7 @@ class SQLDatabase(Database):
This also removes all songs that have been added but have no This also removes all songs that have been added but have no
fingerprints associated with them. 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_SONGS_TABLE)
cur.execute(self.CREATE_FINGERPRINTS_TABLE) cur.execute(self.CREATE_FINGERPRINTS_TABLE)
cur.execute(self.DELETE_UNFINGERPRINTED) cur.execute(self.DELETE_UNFINGERPRINTED)
@ -167,7 +167,7 @@ class SQLDatabase(Database):
.. warning: .. warning:
This will result in a loss of data 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_FINGERPRINTS)
cur.execute(self.DROP_SONGS) cur.execute(self.DROP_SONGS)
@ -177,14 +177,14 @@ class SQLDatabase(Database):
""" """
Removes all songs that have no fingerprints associated with them. 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) cur.execute(self.DELETE_UNFINGERPRINTED)
def get_num_songs(self): def get_num_songs(self):
""" """
Returns number of songs the database has fingerprinted. 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) cur.execute(self.SELECT_UNIQUE_SONG_IDS)
for count, in cur: for count, in cur:
@ -195,7 +195,7 @@ class SQLDatabase(Database):
""" """
Returns number of fingerprints the database has fingerprinted. 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) cur.execute(self.SELECT_NUM_FINGERPRINTS)
for count, in cur: for count, in cur:
@ -207,14 +207,14 @@ class SQLDatabase(Database):
Set the fingerprinted flag to TRUE (1) once a song has been completely Set the fingerprinted flag to TRUE (1) once a song has been completely
fingerprinted in the database. fingerprinted in the database.
""" """
with self.cursor() as cur: with self.cursor(charset="utf8") as cur:
cur.execute(self.UPDATE_SONG_FINGERPRINTED, (sid,)) cur.execute(self.UPDATE_SONG_FINGERPRINTED, (sid,))
def get_songs(self): def get_songs(self):
""" """
Return songs that have the fingerprinted flag set TRUE (1). 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) cur.execute(self.SELECT_SONGS)
for row in cur: for row in cur:
yield row yield row
@ -223,7 +223,7 @@ class SQLDatabase(Database):
""" """
Returns song by its ID. 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,)) cur.execute(self.SELECT_SONG, (sid,))
return cur.fetchone() return cur.fetchone()
@ -231,14 +231,14 @@ class SQLDatabase(Database):
""" """
Insert a (sha1, song_id, offset) row into 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)) cur.execute(self.INSERT_FINGERPRINT, (hash, sid, offset))
def insert_song(self, songname, file_hash): def insert_song(self, songname, file_hash):
""" """
Inserts song in the database and returns the ID of the inserted record. 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)) cur.execute(self.INSERT_SONG, (songname, file_hash))
return cur.lastrowid return cur.lastrowid
@ -252,7 +252,7 @@ class SQLDatabase(Database):
# select all if no key # select all if no key
query = self.SELECT_ALL if hash is None else self.SELECT 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) cur.execute(query)
for sid, offset in cur: for sid, offset in cur:
yield (sid, offset) yield (sid, offset)
@ -272,7 +272,7 @@ class SQLDatabase(Database):
for hash, offset in hashes: for hash, offset in hashes:
values.append((hash, sid, offset)) values.append((hash, sid, offset))
with self.cursor() as cur: with self.cursor(charset="utf8") as cur:
for split_values in grouper(values, 1000): for split_values in grouper(values, 1000):
cur.executemany(self.INSERT_FINGERPRINT, split_values) cur.executemany(self.INSERT_FINGERPRINT, split_values)
@ -289,7 +289,7 @@ class SQLDatabase(Database):
# Get an iteratable of all the hashes we need # Get an iteratable of all the hashes we need
values = mapper.keys() values = mapper.keys()
with self.cursor() as cur: with self.cursor(charset="utf8") as cur:
for split_values in grouper(values, 1000): for split_values in grouper(values, 1000):
# Create our IN part of the query # Create our IN part of the query
query = self.SELECT_MULTIPLE query = self.SELECT_MULTIPLE

View file

@ -1,3 +1,4 @@
# encoding: utf-8
import dejavu.fingerprint as fingerprint import dejavu.fingerprint as fingerprint
import dejavu.decoder as decoder import dejavu.decoder as decoder
import numpy as np import numpy as np
@ -60,6 +61,7 @@ class MicrophoneRecognizer(BaseRecognizer):
def start_recording(self, channels=default_channels, def start_recording(self, channels=default_channels,
samplerate=default_samplerate, samplerate=default_samplerate,
chunksize=default_chunksize): chunksize=default_chunksize):
print("* start recording")
self.chunksize = chunksize self.chunksize = chunksize
self.channels = channels self.channels = channels
self.recorded = False self.recorded = False
@ -80,12 +82,15 @@ class MicrophoneRecognizer(BaseRecognizer):
self.data = [[] for i in range(channels)] self.data = [[] for i in range(channels)]
def process_recording(self): def process_recording(self):
print("* recording")
data = self.stream.read(self.chunksize) data = self.stream.read(self.chunksize)
nums = np.fromstring(data, np.int16) nums = np.fromstring(data, np.int16)
# print(nums)
for c in range(self.channels): for c in range(self.channels):
self.data[c].extend(nums[c::self.channels]) self.data[c].extend(nums[c::self.channels])
def stop_recording(self): def stop_recording(self):
print("* done recording")
self.stream.stop_stream() self.stream.stop_stream()
self.stream.close() self.stream.close()
self.stream = None self.stream = None
@ -101,8 +106,7 @@ class MicrophoneRecognizer(BaseRecognizer):
def recognize(self, seconds=10): def recognize(self, seconds=10):
self.start_recording() self.start_recording()
for i in range(0, int(self.samplerate / self.chunksize for i in range(0, int(self.samplerate / self.chunksize * int(seconds))):
* seconds)):
self.process_recording() self.process_recording()
self.stop_recording() self.stop_recording()
return self.recognize_recording() return self.recognize_recording()