
On Thu, Nov 12, 2009 at 5:31 PM, Michael Mossey
Hi, Thanks for your help and it looks like you identified some conditions that could be removed. There is one change necessary, I think.
closest a b = if (inPitch - a) < (b - inPitch) then a else b
Yes, of course. I just happens that in the test code (testMap2) it gives the same answer... Patrick
Patrick LeBoutillier wrote:
Michael,
Here's my stab at it, not sure if it's really better though:
findClosestPitch :: SampleMap -> Pitch -> ErrorT String Identity Pitch findClosestPitch samples inPitch = do when (M.null samples) $ throwError "Was given empty sample table." case M.splitLookup inPitch samples of (_, Just _, _) -> return inPitch (ml, _, mh) -> return $ approxPitch ml mh where approxPitch ml mh | M.null ml = fst . M.findMin $ mh approxPitch ml mh | M.null mh = fst . M.findMax $ ml approxPitch ml mh = closest (fst . M.findMax $ ml) (fst . M.findMin $ mh) where closest a b = min (inPitch - a) (b - inPitch)
I tried to separate the approximation part from the rest of the code, and used a bit of deduction to eliminate (hopefully correctly...) some of the testing conditions. Anyways, I had fun doing working on this, and I learned a bit about computerized music as well!
Thanks,
Patrick
-- ===================== Patrick LeBoutillier Rosemère, Québec, Canada