Control Plane is ideal for this sort contextual computing. It's a free, open source app in which you can configure a context. For example a context 'library'. In that context certain rules apply (i.e. mute device). The context can be automatically triggered by numerous things (i.e. sleep/wake event, connected wifi, running applications, ...)
It is useful in many other situations as well, I use this app extensively.
Here is how you can configure Control Plane to do what you asked for:
Create a new context and give it an appropriate name (i.e. muted)
Under Evidence Sources make sure Sleep/Wake Event is checked
- Under Rules add a new Sleep/Wake Event Rule and set it so when system goes to sleep it activates the muted context with the confidence slider set to 100%.
- Under Actions add a Mute action (Sound and Music Actions > Mute). Set it so that system is muted on arrival at context muted.
There you go. You could of course make the context switch to Muted more intelligent by using and combining different rules (each with its own confidence level).