Hi Huskies_mlspl!
There is an app for that! I use it all the time!
Get Amphetamine from the Mac App Store (click here).
When you have downloaded it and opened it up, it will ask you some questions and give you tips.
When you have finished all that, there should be new little icon in your iMac's toolbar that looks like this:
Click on it and select "preferences."
At the top of the preferences window, select triggers,
then check "enable triggers."
Click the + sign at the bottom of the window.
Name your trigger (something like "Play Music" or "Music").
Next, where it says "Select a criterion to configure," select "app," and check the box next to "use app criterion." Now open the app you want to play music from (iTunes, Spotify, etc.)
then go back to the preferences window and select it from the list.
Before pressing save, you can select either "Allow display to sleep when this trigger activates a session" or "allow screen saver to run when this trigger activates a session," or both, or none!
(note that if the display goes to sleep the computer will still be on).
Then press save!
On the top of the preferences window you can click on any of the others and customize the settings to your liking.
Now whenever this app is open, your computer will not sleep, but it will sleep if the app has been quit.
I really hope this helps!