def _randomSliceStructure(self): if self.sliceStructs is None: self.sliceStructs = QueryRunner.getAllSliceStructures() randomSelection = Util.GetRandomElementFromList(self.sliceStructs) return randomSelection
def testCleanReturnedList_One(self): list = (('element1', ), ) retList, retLength = Util.CleanReturnedList(list) self.assertEqual(retList, "element1") self.assertEqual(retLength, 1)
def buildSlice(self, part, bpm, key, profileId, useItemBased=None): if self.engine is None and useItemBased is not None: # select rec engine self.engine = recSelect.selectRecommendationEngine(profileId, useItemBased, self.writer) if part in self.generatedSlices: return self.generatedSlices[part] #get random sliceStructure for slice sliceStructure = self._randomSliceStructure() self.writer.PrintAndWrite("slice structure for " + str(part) + " : " + str(sliceStructure)) slice = Slice(bpm, key) dictionaryIndex = 1 for keyDict in sliceStructure: #the instrument track is empty, skip to the next instrument if sliceStructure[keyDict] is 0: dictionaryIndex += 1 continue tempInstrument = dictionaryIndex if tempInstrument is 2: tempKey = "None" else: tempKey = key recommend = self.engine.GetRecommendation( profileId=profileId, key=tempKey, bpm=bpm, instrumentID=tempInstrument, section=part ) if recommend is None: #the engine did not manage to get a recommendation self.writer.PrintAndWrite("could not make recommendation, picks a random result") randomResult = Util.GetRandomClip(bpm, tempKey, tempInstrument) if randomResult is None: return None else: recommend = randomResult self.writer.PrintAndWrite("** selected result ran", False) self.writer.PrintAndWrite(recommend) slice.addElement(recommend) dictionaryIndex += 1 self.writer.PrintAndWrite("slice:", False) self.writer.PrintAndWrite(slice.getElements(), False) self.writer.PrintAndWrite("", False) self.generatedSlices[part] = slice return slice
def testCleanReturnedList_Multiple(self): list = (('element1', ), ('element2', )) listTest = ["element1", "element2"] retList, retLength = Util.CleanReturnedList(list) self.assertEqual(retList, listTest) self.assertEqual(retLength, 2)
def testRandomElementFromList_LastElement(self): list = ["A", "B", "C"] element = "" while element is not "C": element = Util.GetRandomElementFromList(list) self.assertEqual(element, "C")
def getSongStructureList(songLength): if songLength == None or songLength == "": ErrorPrinter.invalidDBCallInput("Song structure") return None cursor = _createcursor() cursor.callproc("GetStructuresWithLength", (songLength, )) data = cursor.fetchall() cleanData, datalength = Util.CleanReturnedList(data) return cleanData, datalength
def SelectSongStructure(songLength): inputStr = str(songLength).lower() if not _validateInputString(inputStr): print("not valid") return "" list, length = _GetListOfStructuresFromDb(inputStr) if length > 1: element = Util.GetRandomElementFromList(list) return element else: return list
def getAllUserProfileIds(cursor=None): if cursor is None: cursor = _createcursor() cursor.callproc("GetAllUserProfileIds", ()) data = cursor.fetchall() cleanData, datalength = Util.CleanReturnedList(data) if datalength is 0: return None if datalength is 1: return [cleanData] return cleanData
def CreateSliceAudioFileForEvaluation(sliceIdList): print("exporting slice audio track") audioclip = AudioMerger.mergeClipsFromList(sliceIdList) if audioclip is None: return False print("audioclip: {0}".format(audioclip)) tempfilename = "slice_" + str(sliceIdList) filename = Util.GeneratePathAndFileNameForAudiofile(filename=tempfilename) exportSuccess = AudioMerger.exportAudio(audio=audioclip, filename=filename) print("export success: {0}".format(exportSuccess)) print() return True
def CreateSong(self): self.writer.PrintAndWrite("") self.writer.PrintAndWrite("__________________________________") self.writer.PrintAndWrite("_* CREATING SONG", False) elementDict = {} # song structure self.writer.PrintAndWrite("__* SELECTING STRUCTURE ", False) structure = SongStructureSelector.SelectSongStructure(self.length) self.writer.PrintAndWrite("Selected Structure: " + structure, False) # split song structure into parts elementList = Util.SplitStructureIntoParts(structure) # for each element in the list, get a slice for it for element in elementList: sliceID, slice = self._getSlice(element) elementDict[sliceID] = slice # moving old files PrimeMover.moveOldAudioFiles(self.writer) # creating song audiofile self._putSongToghether(structure, elementDict) # regestering ccrelations self._registerClipRelations(elementDict) # feedback self.writer.PrintAndWrite("", False) self.writer.PrintAndWrite("** TIME TO GIVE FEEDBACK** ", False) self.writer.PrintAndWrite("", False) #FeedbackRecorder.GetNumOfRandomFromClipRelations(self.profileKey, self.clipclipRelation, writer=self.writer, numRandom=3) FeedbackRecorder.GetFeedbackOnSong(self.profileKey, elementDict, writer=self.writer) FeedbackRecorder.GetFeedbackForKNumberOfSlices(self.profileKey, elementDict, writer=self.writer, numRandom=3) self.writer.PrintAndWrite("", False) self.writer.PrintAndWrite("** DONE ** ", False)
def CreateCCRelationAudioFileForEvaluation(ccID, filename=""): print("exporting clip-clip relation audio track") result = QueryRunner.getClipsFromClipClipLinkId(ccID) clip1 = result[0] clip2 = result[1] audioclip = AudioMerger.chainClips(clipId1=clip1, clipId2=clip2) if audioclip is None: return False print("audioclip: {0}".format(audioclip)) nameofFile = "ccrel" + str(filename) filename = Util.GeneratePathAndFileNameForAudiofile(filename=nameofFile) exportSuccess = AudioMerger.exportAudio(audio=audioclip, filename=filename) print("export success: {0}".format(exportSuccess)) print() return True
def _putSongToghether(self, structure, elementDict): self.writer.PrintAndWrite("", False) self.writer.PrintAndWrite("** PUTTING SONG TOGETHER ** ", False) song = None for element in structure: if element is None or elementDict[element] is None: self.writer.PrintAndWrite( "The song had missing parts, or parts that were None. Stopped." ) return None elements = elementDict[element].getElements() audioclip = None if len(elements) is 3: audioclip = AudioMerger.mergeClipsFromList(elements) elif len(elements) is 2: audioclip = AudioMerger.mergeClips(clipId1=elements[0], clipId2=elements[1]) else: return None if audioclip is None: return None if song is None: song = audioclip else: song = AudioMerger.AppendClip2ToClip1(song, audioclip) filename = Util.GeneratePathAndFileNameForAudiofile(isSong=True) exportSuccess = AudioMerger.exportAudio(song, filename=filename) if not exportSuccess: return None
def GetFinishedSongStructure(index): if index == None or index < 0: ErrorPrinter.invalidDBCallInput("Finished song structure") return None cursor = _createcursor() cursor.callproc("GetSongStructureHash", (index, )) data = cursor.fetchall() if data == (): ErrorPrinter.databaseReturnedNothing("Finished song structure") return "" cleanData, _ = Util.CleanReturnedList(data) #decode decoded = EncodeDecode.decodeMessage(cleanData) if decoded == None: print("unable to decode") return "" return decoded
def testRandomElementFromList_NormalUse(self): list = ["A", "B", "C"] element = Util.GetRandomElementFromList(list) self.assertIn(element, list)
def testCleanReturnedList_Empty(self): list = [] retList, retListLength = Util.CleanReturnedList(list) self.assertEqual(retList, []) self.assertEqual(retListLength, 0)
def testRandomElementFromList_OneElement(self): soloList = ["D"] soloElement = Util.GetRandomElementFromList(soloList) self.assertEqual(soloElement, "D")