Marvin Hamlisch was one of the most successful composers of our time. He could sit down at the piano and rattle off a tune in a nanosecond. Or he could labor over a melody for days.

When he died at 68, he left evergreen songs such as “The Way We Were,” “Nobody Does It Better” and “Through the Eyes of Love.” He scored countless films, Broadway shows and TV themes. What he did and how he did it is the subject of “Marvin Hamlisch: What He Did for Love,” Dori Berinstein’s documentary for PBS’s “American Masters,” airing Friday.

He heard music in everything, said his wife of 23 years, Terre Blair Hamlisch. “If he heard the wind or a breeze in the trees, he could tell you what note it was. He could tell you the screech of a tire. He didn’t hear like we would hear a screech of a tire. He would hear it with the note, the exact note of it. ‘It’s an E flat. It’s this. It’s that.’ He could hear a fly like a 747,” she said.

Lucie Arnaz, who starred in Hamlisch’s “They’re Playing Our Song,” described him this way: “When he was a child . . . everything he heard sounded like a song to him, the sound of falling rain . . . and there was something about being able to do that that made music for him very organic. . . . I’m a singer, so when I sing I want to sing lyrics that are put to music the way you talk so that you’re not having a phrase that isn’t said that way, just because you’re singing it. And Marvin was so incredibly good at doing that, at getting into the emotion of what the song wanted to be, even if it didn’t have words, if he’s scoring a film,” she said.

Hamlisch was declared a genius when he was a child. By the time he was 31, he had earned four Grammys, an Emmy, three Oscars, a Tony and the Pulitzer Prize. He was prolific and industrious to a fault. According to Terre Hamlisch, “He used to say that it’s extremely difficult to write a good melody. I think that sometimes people confuse complexity, and it can be more subconscious. If something is being written, it can be more subconscious, and I think . . . that the most difficult thing is to write a good melody that isn’t subconscious and isn’t manipulating people, and that has to come from an extraordinarily organic place.”

Hamlisch scored the James Bond movie “The Spy Who Loved Me.” That happened by chance, said Berinstein. “We talked to (Hamlisch’s frequent collaborator) Carole Bayer Sager, who explained . . . that he told her that he was working on a Bond film. And she said, ‘You know, if I was going to make a score, write the lyrics for a Bond film, I’d call it “Nobody Does it Better.’ ” And Marvin right then and there sat down at the piano and wrote that melody right in that moment. So, I mean, that’s an incredible story, one of many that we have about that beautiful song.”

When I interviewed Hamlisch a few years ago, he said the source of his music was as much a mystery to him as to everybody else. “It’s hard to explain exactly where the music comes from,” he said.

“Sometimes you meet people who are multilingual and they’re able to very quickly go into any language. In the same way, I’ll be thinking about a scene, but the language I go into is the music. That’s my second language. I’ll be thinking about what I want [the songs] to say, but in the meantime it’s being translated musically,” he said.

Hamlisch wrote an autobiography because he wanted people to know there is something more important in life than success, he told me. As an example, he remembered returning to an empty house the night he won the Oscar for “The Way We Were.”

“I came home and I emptied the cat litter. I was so alone. There was something so alone. . . . All those wonderful moments are only moments in your life, but they are not the foundation on which you live,” he said.

“I was very alone and that success turned into going to Broadway — which was my first love — with ‘Chorus Line.’ And I went there and again had this major success, but the reviews were deadly for me. Just awful.”

Two flops followed, “Jean” and “Smile,” and Hamlisch fell into an even deeper depression. His parents had died; Michael Bennett (who created “A Chorus Line”) had died; and Edward Kleban, Hamlisch’s lyricist on “A Chorus Line,” had died.

“I never felt more isolated and more alone. The pillars of my family life were falling apart,” he said.

Then fate took a hand. His housekeeper and a friend’s housekeeper put Hamlisch in telephone touch with Terre Blair, who was 14 years younger and lived on the other side of the continent.

They carried on a phone romance for months and Hamlisch proposed without ever having met her.

Hamlisch credited Terre for revitalizing him. “If I had a conversation with God and said, ‘This is exactly what I need,’ He would’ve brought me this woman,” he said.

— McClatchy-Tribune

Marvin Hamlisch: What He Did For Love

(two hours) premieres Friday at 9 p.m. on WETA.