This study presents a probabilistic model of melody perception, which infers the key of a melody and also judges the probability of the melody itself. (A "melody" is defined here as a sequence of pitches, without rhythmic information.) The model uses Bayesian reasoning. A generative probabilistic model is proposed, based on three principles: 1) melodies tend to remain within a narrow pitch range; 2) note-to-note intervals within a melody tend to be small; 3) notes tend to conform to a distribution (or "key-profile") that depends on the key. The model is tested in three ways: on a key-finding task, on a melodic expectation task, and on an error-detection task.